hello,

i am using the Graphics.DrawLine method in C# and this is fine for thin lines but for thick lines it draws really badly. i have sample code to reproduce the problem below. just create a new c# forms application and add a picture box to the form. make the picture box the same size as the form and add empty MouseUp, MouseDown and MouseMove event handlers. then add this to form1.cs:

public partial class Form1 : Form
    {
        //var 
        Pen myPen;
        bool bMouseDown = false;
        Point prevPoint;
        Graphics g;

        //ctor
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {            
            bMouseDown = false;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            prevPoint = e.Location;
            bMouseDown = true;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (bMouseDown)
            {
                Point thisPoint = e.Location;

                if (prevPoint.X == 0 && prevPoint.Y == 0)
                {
                    prevPoint = thisPoint;
                    return;
                }

                g.DrawLine(myPen, thisPoint.X, thisPoint.Y, prevPoint.X, prevPoint.Y);
                prevPoint = thisPoint;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
            g = pictureBox1.CreateGraphics();

            myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            myPen.Color = Color.Black;
            myPen.Width = 5.0f;
        }
    }

as you can see pretty simple stuff. in every MouseMove event, i simply draw a line from the previous point (prevPoint) to the current point (thisPoint). the pen that i draw with is a solid black pen, 5 pixels wide.

if you run that code and use the mouse to free-draw a line, it looks ok. but now change the pen width to something like:

myPen.Width = 30.0f;

if you try it again you will see that the line is drawn horribley. what i need is for a nice thick black solid line and this is not what happens.

is there any other way to achieve what i am trying to do (free-draw a line of all thicknesses) ?

Edited 6 Years Ago by unclepauly: n/a

Set the SmootingMode of the Graphics object to AntiAlias or HighQuality.
Like so:
g.SmootingMode = System.Drawing.Drawing2D.SmootingMode.HighQuality;

thanks i have tried this but the problem is the same. i have just been through all the SmoothingModes with the same result.

any other ideas ?

the solution is:

Pen.SetLineCap(LineCap.Round,LineCap.Round,DashCap.Round);

for anyone else having this problem.

Comments
Great information!

Still have to investigate this, but this might be related to the problem I have in this thread http://www.daniweb.com/forums/thread201807.html.
Thanks for the tip!

Hello All,

I am struggling with the same issue. Setting pen.StartCap = LineCap.Round; and pen.EndCap = LineCap.Round; will only works if color is solid. If we set something like this
brush = new SolidBrush(Color.FromArgb(100, Color.Black)); // A transparent brush
pen = new Pen(brush, 20);

and then try to draw line then the result will remain bad,(Caps will display in line).
I am trying to make Marker brush like in Ms paint.

Regards,
Jayant Paliwal

This article has been dead for over six months. Start a new discussion instead.