CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Smoother drawing pen

    Hello

    I have a C#/GDI+ paint application (winform) in which I draw different shapes with different colors and pen sizes. Now, the shape drawing I have handled, but using a 'free pen' as you would in MS Paint, I have also had it done but it is rather ugly (see pic).

    Code:
    if (crtanje)
                    {
                        debljina = float.Parse(debljina_box.Text);
                        Graphics gr = Graphics.FromImage(bit);
                        gr.SmoothingMode = SmoothingMode.HighQuality;
                        olovka = new Pen(boja, debljina);
                        gr.DrawLine(olovka, new Point(prethodnoX ?? e.X, prethodnoY ?? e.Y), new Point(e.X, e.Y));
                        panel1.CreateGraphics().DrawImageUnscaled(bit, new Point(0, 0));
                        prethodnoX = e.X;
                        prethodnoY = e.Y;
                    }
    (Meaning of most variables can be deduced but I can explain if necessary.)

    My app works that firstly I draw images on a bitmap (called 'bit' in this case), which are then created on a panel and then the panel paint event shows what I have drawn. But in this case, the pen is quite ugly, I assume because of the procedure: create graphics-draw line-draw on panel takes some time in between steps.

    Is there a way to make this smoother?

    Name:  a.jpg
Views: 743
Size:  43.0 KB

  2. #2
    Join Date
    Apr 2010
    Posts
    131

    Re: Smoother drawing pen

    It is difficult to tell from the information you provided, but it sounds like your main UI thread is too busy updating the bitmap to capture mouse movement points (it can only do one thing at a time). If the problem is as you say - that the line-draw takes time, I would create a multi-thread environment:

    1) Main thread captures mouse movement, stores point values in a queue
    2) off-thread watcher method checks queue every X milliseconds
    3) if watcher has points to process, it does the math, creates the updated bitmap, and send it to the main thread for updating
    4) Main UI thread then only has to be "paused" every X milliseconds as it is directly updating the bitmap, freeing it to capture more mouse movement points

    If you want to post your project, I can help you with that.

  3. #3
    Join Date
    Apr 2010
    Posts
    131

    Re: Smoother drawing pen

    Here is an example of pen drawing on a picturebox (PictureBox1) that continues to collect points:
    Code:
    Queue<Point> points = new Queue<Point>();
            Point lastPoint = new Point(0, 0);
            bool drawOn = false;
            private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.Click += new EventHandler(pictureBox1_Click);
                pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
                Bitmap img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.Image = img;
            }
    
            void pictureBox1_Click(object sender, EventArgs e)
            {
                drawOn = drawOn ? false : true;
                if (drawOn)
                {
                    lastPoint = new Point(MousePosition.X, MousePosition.Y);
                }
            }
            void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (drawOn)
                {
                    points.Enqueue(new Point(e.X, e.Y));
                    updateImage();
                }
            }
            void updateImage()
            {
                Image img = pictureBox1.Image;
                Graphics g = Graphics.FromImage(img);
                Pen p = new Pen(Color.Black, 5f);
                while (points.Count > 0)
                {
                    Point pt = points.Dequeue();
                    g.DrawLine(p, lastPoint, pt);
                    lastPoint = pt;
    
                }
                pictureBox1.Image = img;
            }
    Last edited by mrgr8avill; September 9th, 2012 at 06:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured