我正在绘制控件上的图形,但是0,0位于控件的左上角。有没有办法翻转坐标,使0,0位于控件的左下角?绘图控制时的翻转坐标
9
A
回答
13
如果您正在使用的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...
}
你只需要,如果你想要做更多的图纸包含的开始/结束容器调用使用常规坐标系统。有关图形容器的更多信息是available on MSDN。
正如汤姆在评论中提到的那样,这种方法要求Height
值具有正确的值。如果您尝试此操作并且看不到任何内容正在绘制,请确保调试器中的值是正确的。
0
不,但使用控件的Size
(或Height
)属性,很容易计算翻转的坐标:只需绘制到Height-y
即可。
0
不是说我知道,但如果你使用(x,Control.Height-y),你会得到相同的效果。
-1
总之
没有,但是如果我在控制借鉴了很多我有几个功能,帮助我:
Point GraphFromRaster(Point point) {...}
Point RasterFromGraph(Point point) {...}
这样,我把所有的转换在一个地方,不担心这样的事情y - this.Height
散乱的代码。
+0
为什么投下来? – Pondidum
1
这里有一个简单的用户控件演示如何做到这一点:
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);
}
}
相关问题
- 1. 在翻转的Java坐标中绘制
- 2. ImageMagick绘制翻译坐标
- 3. 如何在上下文中绘制时翻转坐标?
- 4. 带有翻转绘图坐标的NSView高度方向
- 5. 绘制图像坐标
- 6. 坐标绘制大厅图
- 7. 翻转NSView鼠标坐标
- 8. ggplot2 boxplot的翻转坐标
- 9. Numpy翻转坐标系
- 10. 如何翻转SVG坐标?
- 11. UIView/UIButton坐标翻转
- 12. 翻转用glVertex2f()绘制的图形?
- 13. 在OpenGL在绘图时转换像素坐标到标准化的坐标3.0
- 14. 更大的视图控制器内的翻转视图控制器将翻转主视图控制器
- 15. 绘制线的坐标
- 16. 如何将窗口的坐标转换为控制坐标
- 17. 在MATLAB中绘制极坐标图像
- 18. 在java中绘制极坐标图
- 19. Java /图形:绘制三维坐标?
- 20. 如何绘制坐标点到图像
- 21. 在地图上绘制GPS坐标
- 22. WPF控制翻转
- 23. 如何翻转NSView的坐标系?
- 24. 坐标X Y绘制Android
- 25. 从坐标绘制矩形
- 26. Microsoft Chart绘制点坐标
- 27. 绘制真实坐标
- 28. JLabel text Y绘制坐标
- 29. 在绘制Python中的对数坐标图时出现问题
- 30. 用openpyxl绘制折线图 - 坐标轴/绘图问题
您正在使用的WinForms或WPF?你可能想更新你问题上的标签。 –