Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - flip coordinates when drawing to control

I am drawing a graph on a control, but 0,0 is at the top-left hand corner of the control. Is there a way to flip the coordinates so that 0,0 is at the lower left corner of the control?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are using WinForms, then you might find that you can flip the Y-Axis using Graphics.ScaleTransform:

private void ScaleTransformFloat(PaintEventArgs e)
{
    // Begin graphics container
    GraphicsContainer containerState = e.Graphics.BeginContainer();

    // Flip the Y-Axis
    e.Graphics.ScaleTransform(1.0F, -1.0F);

    // Translate the drawing area accordingly
    e.Graphics.TranslateTransform(0.0F, -(float)Height);

    // Whatever you draw now (using this graphics context) will appear as
    // though (0,0) were at the bottom left corner
    e.Graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40);

    // End graphics container
    e.Graphics.EndContainer(containerState);

    // Other drawing actions here...
}

You only need to include the begin/end container calls if you want to do additional drawing using the regular coordinate system as well. More information on graphics containers is available on MSDN.

As mentioned by Tom in the comments, this approach requires the Height value to be available with a correct value. If you try this and see nothing being drawn, ensure that value is correct in a debugger.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...