spspli
spspli

Reputation: 3338

How to make drawLine smoother?

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?

Upvotes: 7

Views: 12559

Answers (3)

BWF CHU
BWF CHU

Reputation: 1

Perfect match with our need.

Gift 4 u:

Graphics g = e.Graphics;
    
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

Picture of the result with AntiAlias

Upvotes: -1

Hans Passant
Hans Passant

Reputation: 941455

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.

Upvotes: 9

Agnel Kurian
Agnel Kurian

Reputation: 59466

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

Upvotes: 12

Related Questions