开发者

How to make drawLine smoother?

开发者 https://www.devze.com 2023-02-22 18:04 出处:网络
I use the following code to draw line: Graphics g = this.CreateGraphics(); Pen p = new 开发者_C百科Pen(Color.Black,3);

I use the following code to draw line:

Graphics g = this.CreateGraphics();
Pen p = new 开发者_C百科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?


You need to enable anti-aliasing. Set Graphics.SmoothingMode to AntiAlias as described here: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.smoothingmode.aspx


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号