6

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

______________
| ^ |
| | |
| o----->|
| |
|____________|

.NET 希望它位于左上角。

_____________>
| |
| |
| |
| |
V____________|

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

有谁知道如何在 C# 中使用 Graphics 对象来做到这一点?

Graphics.TranslateTransform 不会这样做,因为它会使坐标倒置。结合此 Graphics.ScaleTransform(1,-1) 也不令人满意,因为这会使文本看起来颠倒。

4

3 回答 3

1

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

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); }
}
于 2009-06-23T07:06:07.733 回答
1

您可以在绘制文本时继续使用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;
于 2012-01-31T14:59:40.853 回答
0

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

于 2009-06-23T02:02:47.957 回答