English 中文(简体)
C# GDI Invert Graphics flipped on the X axis
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签