如何让画线更流畅?
我使用下面的代码来画线:
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black,3);
g.DrawLine(p,...);
// ...
为什么直线是锯齿形的,一点也不笔直光滑。怎样才能让它变得笔直、平滑呢?
I use the following code to draw line:
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black,3);
g.DrawLine(p,...);
// ...
Why the straight line is zigzag kind of, not straight and smooth at all. How could I make it straight and smoother?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要启用抗锯齿功能。将
Graphics.SmoothingMode
设置为AntiAlias
,如下所述:http://msdn.microsoft.com/en-us/library/system.drawing.graphics.smoothingmode.aspxYou need to enable anti-aliasing. Set
Graphics.SmoothingMode
toAntiAlias
as described here: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.smoothingmode.aspx重写窗体的 OnPaint() 方法或实现控件的 Paint 事件。使用传递的 e.Graphics 对象进行绘制。 它将被正确初始化以绘制抗锯齿线。并且可以双缓冲,因此不会闪烁。调用 Invalidate() 强制重绘。
在 99.9% 的情况下,使用 Control.CreateGraphics() 都是错误的。无论你画什么,都无法持久。当您最小化并恢复窗口时,它就会消失。或者当您将其部分移出屏幕并移回时。或者当您在 XP 和任何未启用 Aero 的计算机上重叠另一个窗口时。 CreateGraphics() 仅适用于帧速率大于 ~20 fps 的动画。
Override the OnPaint() method of your form or implement the Paint event of a control. Use the passed e.Graphics object to draw. It will be properly initialized to draw anti-aliased lines. And can be double-buffered so it doesn't flicker. Call Invalidate() to force a repaint.
Using Control.CreateGraphics() is wrong in 99.9% of all cases. Whatever you draw cannot persist. It will be gone when you minimize and restore the window. Or when you partly move it off the screen and back. Or when you overlap another window on yours on XP and any machine that doesn't have Aero enabled. CreateGraphics() is only suitable for animations at frame rates larger than ~20 fps.
与我们的需求完美匹配。
礼物 4 u:
使用 AntiAlias 后的结果图片
Perfect match with our need.
Gift 4 u:
Picture of the result with AntiAlias