views:

380

answers:

2

I am drawing a series of Points using the Graphics class. I am reading from a Point array. For whatever reason the rendered image is upside down (flipped on the X axis). Is there a simple way to tell the Graphics class to draw "upside down" ? Many thanks.

+1  A: 

It sounds not like it's drawing upside down, but like it's being given the coordinates upside down.

Check that you're expecting coordinates with the right origin. 0,0 should be by default top-left of your screen.

EDIT: You should be able to compensate by changing Y on each point as you draw it to use the formula Y = height - Y.

McAden
A: 

You should note that the default orientation or direction of graphics in most drawing systems is from top of display to the bottom of the display. To compensate for this, you need to know the intended height of your viewing area, and then subtract your coordinates from that height. This will reverse the orientation, and set your figures relative to the bottom of the viewing area.

Let's say you have two points specified as <x,y> and a viewport that is 300 (w) x 200 (h).

{{0, 0}, {100, 200}}

When drawing in the viewing area, <x, y> will get mapped to <x, 200-y>. This gives us the following points.

{{0, 200}, {100, 0}}

There are also mechanisms to switch the mapping mode to Cartesian style coordinates, which will probably match your source data, but is usually avoided because it will remap everything else that has already compensated for the default orientation.

The SetMapMode() function in Windows can be used for this purpose if this is what you really wish.

SetMapMode() @ MSDN

Here's the P/Invoke signature of the function. (from pinvoke.net: setmapmode (gdi32) )

[DllImport("gdi32.dll")]
static extern int SetMapMode(IntPtr hdc, int fnMapMode);

edit:

There's one more way to accomplish this, which I personally haven't tried.

Graphics.TransformPoints Method @ MSDN

This allows you to specify coordinate systems, and the Graphics object will do the mapping for you. In your case, you may wish to call it with CoordinateSpace.World or CoordinateSpace.Page as the first argument, and CoordinateSpace.Device as the second.

meklarian
TransformPoints makes life simple. THANKS!
GDIHelp