Alex Diamond
Alex Diamond

Reputation: 586

WinForms C# - Proper method to free draw?

I'm attempting to re-create something similar to Microsoft's Paint application. I'm using the Bitmap technique to render my drawings in order to have them be persistent. At 4 pixels x 4 pixels, it's unnoticeable, but as you go higher in the width, it's extremely noticeable, especially when you curve. I have a picture to show you what it looks like and some snippet.

picture

        private void Form1_Load(object sender, EventArgs e)
    {
        //Initializing Graphics and Bitmap
        pnlMain.BackgroundImage = new Bitmap(pnlMain.Width, pnlMain.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        Doodle = Graphics.FromImage(pnlMain.BackgroundImage);
        Doodle.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        Doodle.Clear(Color.White);
     }

private void pnlMain_MouseDown(object sender, MouseEventArgs e)
    {
        start = e.Location;
        if (cboDrawType.SelectedIndex == 0)
        {
        Painting = true;
        }

        private void pnlMain_MouseUp(object sender, MouseEventArgs e)
    {
        Painting = false;
    }

        private void pnlMain_MouseMove(object sender, MouseEventArgs e)
    {
      if (Painting == true && cboDrawType.SelectedIndex == 0)
        {
            end = e.Location;
            Doodle.DrawLine(ChosenPen, start, end);
            pnlMain.Refresh();
        }
      start = end;
    }

        private void nudSize_ValueChanged(object sender, EventArgs e)
    {
        ChosenPen.Width = Convert.ToUInt16(nudSize.Value);
    }

NOTE: start and end are new Point() variable members. Sorry if the code is a bit messy, I had to cut it out brutally. I'm using a pen even though it says Brush in the picture. Doodle is my declared graphics

Upvotes: 4

Views: 2068

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26436

You should specify the StartCap and EndCap properties:

pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;

The default is Flat, which turns short lines with a high pen width into silly stripes.

Upvotes: 5

Related Questions