954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

usiing DrawLine method in .net to draw a free-draw line - horrible when line is thick

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

unclepauly
Light Poster
42 posts since Dec 2006
Reputation Points: 16
Solved Threads: 0
 

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

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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 ?

unclepauly
Light Poster
42 posts since Dec 2006
Reputation Points: 16
Solved Threads: 0
 

the solution is:

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

for anyone else having this problem.

unclepauly
Light Poster
42 posts since Dec 2006
Reputation Points: 16
Solved Threads: 0
 

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!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
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

jayantpaliwal
Newbie Poster
4 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: