Make + y UP,Move Origin C#System.Drawing.Graphics

我希望原点位于我窗口的中心。


 ______________
 |  ^ |
 |  |  |
 |  Ø-----> |
 |  |
 | ____________ |

.NET希望它位于左上角。


 _____________>
 |  |
 |  |
 |  |
 |  |
 V ____________ |

点网和我正在努力相处..

有没有人知道如何在C#中使用Graphics对象?

Graphics.TranslateTransform不会这样做,因为它会使坐标翻转过来。 结合这个Graphics.ScaleTransform(1,-1)并不令人满意,因为这会使文本显示为颠倒。

一种解决方案是使用TranslateTransform属性。 然后,您可以创建自己的FlippedPoint / FlippedPointF结构,而不是使用Point / PointF结构,这些结构具有对Point / PointF的隐式转换(但是通过强制转换它们可以翻转coords):

public struct FlippedPoint { public int X { get; set; } public int Y { get; set; } public FlippedPoint(int x, int y) : this() { X = x; Y = y; } public static implicit operator Point(FlippedPoint point) { return new Point(-point.X, -point.Y); } public static implicit operator FlippedPoint(Point point) { return new FlippedPoint(-point.X, -point.Y); } } 

您可以继续使用ScaleTransform(1, -1)并在绘制文本时临时重置当前转换:

 // Convert the text alignment point (x, y) to pixel coordinates PointF[] pt = new PointF[] { new PointF(x, y) }; graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, pt); // Revert transformation to identity while drawing text Matrix oldMatrix = graphics.Transform; graphics.ResetTransform(); // Draw in pixel coordinates graphics.DrawString(text, font, brush, pt[0]); // Restore old transformation graphics.Transform = oldMatrix; 

尝试创建负高度的图形对象。 我并不特别了解C#库,但这个技巧适用于最新版本的GDI。