制作+;y向上移动原点C#System.Drawing.Graphics

制作+;y向上移动原点C#System.Drawing.Graphics,c#,drawing,system.drawing,C#,Drawing,System.drawing,我希望原点位于窗口的中心 ______________ | ^ | | | | | o----->| | | |____________| ______________ | ^ | | | | |o------>| | | |____________| .NET希望它位于左上角 _____________> | | | | |

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

______________ | ^ | | | | | o----->| | | |____________| ______________ | ^ | | | | |o------>| | | |____________| .NET希望它位于左上角

_____________> | | | | | | | | V____________| _____________> | | | | | | | | 五____________| 网络公司和我正在努力相处

有人知道如何在C#中仅使用图形对象来实现这一点吗


Graphics.TranslateTransform不会执行此操作,因为它会使坐标倒置。组合此图形。ScaleTransform(1,-1)也不能令人满意,因为这会使文本显示为倒置。

尝试创建高度为负的图形对象。我不知道具体的C#library,但这个技巧在GDI的最新版本中有效。

一个解决方案是使用TranslateTransform属性。然后,不使用Point/PointF结构,您可以创建自己的FlippedPoint/FlippedPointF结构,该结构具有指向Point/PointF的隐式强制转换(但通过强制转换,坐标会被翻转):


您可以继续使用
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;
// 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;