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?
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.