绘图时控制的翻转坐标

我在控件上绘制图形,但0,0位于控件的左上角。 有没有办法翻转坐标,使0,0位于控件的左下角?

如果您使用的是WinForms,那么您可能会发现可以使用Graphics.ScaleTransform翻转Y轴:

private void ScaleTransformFloat(PaintEventArgs e) { // Begin graphics container GraphicsContainer containerState = e.Graphics.BeginContainer(); // Flip the Y-Axis e.Graphics.ScaleTransform(1.0F, -1.0F); // Translate the drawing area accordingly e.Graphics.TranslateTransform(0.0F, -(float)Height); // Whatever you draw now (using this graphics context) will appear as // though (0,0) were at the bottom left corner e.Graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40); // End graphics container e.Graphics.EndContainer(containerState); // Other drawing actions here... } 

如果要使用常规坐标系进行其他绘图,则只需要包含开始/结束容器调用。 有关图形容器的更多信息, 请访问MSDN 。

正如Tom在评论中所提到的,这种方法要求Height值具有正确的值。 如果您尝试此操作并且看不到任何内容,请确保调试器中的值正确。

不,但是使用控件的Size (或Height )属性,可以很容易地计算翻转坐标:只需绘制到Height-y

这是一个简单的UserControl,演示了如何执行此操作:

 public partial class UserControl1 : UserControl { public UserControl1() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { e.Graphics.ScaleTransform(1.0F, -1.0F); e.Graphics.TranslateTransform(0.0F, -(float)Height); e.Graphics.DrawLine(Pens.Black, new Point(0, 0), new Point(Width, Height)); base.OnPaint(e); } } 

不是我知道但是如果你使用(x,Control.Height-y)你会得到同样的效果。

简而言之,不管怎么说,如果我在控件上画了很多,我有一些function可以帮助我:

 Point GraphFromRaster(Point point) {...} Point RasterFromGraph(Point point) {...} 

通过这种方式,我将所有转换保存在一个地方,不用担心像y - this.Height这样的事情。这个代码分散了。