Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:52:01+00:00 2026-06-14T03:52:01+00:00

i’m trying to do a little paint program in c#. so far everything works

  • 0

i’m trying to do a little paint program in c#. so far everything works fine, only thing is that when i move the mouse fast enough, gaps appear where there should be a solid line. i have tried everything from double buffering to decreasing the interval of the mouse_move event (i actually didn’t find any method to do this, i think it would also be bad for other processes on the system^^)

could you point me in the right direction here? i tried overriding the paint method of the panel, but when i try this nothing seems to happen.

here’s the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    public partial class Form1 : Form
    {

        bool paint;
        SolidBrush color;
        //size of brush
        int pinselGröße;
        List<Point> pointListe;

        public Form1 ()
        {
            InitializeComponent ();
            pointListe = new List<Point>();
            paint = false;
            color = new SolidBrush ( Color.Black );
            //get brush size from combobox 
            pinselGröße = Convert.ToInt32 ( nudBrushSize.Value );
        }

        private void btnExit_Click ( object sender, EventArgs e )
        {
            this.Close ();
        }

        private void btnClear_Click ( object sender, EventArgs e )
        {
            Graphics gfx = pnlCanvas.CreateGraphics ();
            gfx.Clear ( pnlCanvas.BackColor );
        }

        private void pnlCanvas_MouseDown ( object sender, MouseEventArgs e )
        {
            paint = true;
            Graphics grfx = pnlCanvas.CreateGraphics ();
            //draw a rectangle with brush "color" and pinselGröße as the brush size
            grfx.FillRectangle ( color, e.X, e.Y, pinselGröße, pinselGröße );  
        }

        private void pnlCanvas_MouseMove ( object sender, MouseEventArgs e )
        {
            if ( paint )
            {
                //Graphics grfx = pnlCanvas.CreateGraphics();  
                ////put old position of mouse into variable
                //int altePosX = e.X;
                //int altePosY = e.Y;
                ////grfx.FillEllipse ( color, e.X, e.Y, pinselGröße, pinselGröße );
                //grfx.FillRectangle(color, e.X, e.Y, pinselGröße, pinselGröße);
                //grfx.Dispose();
                pointListe.Add(e.Location);
                pnlCanvas.Invalidate();
            }
        }

        private void pnlCanvas_Paint(PaintEventArgs e)
        {

            e.Graphics.DrawLines(new Pen(color), pointListe.ToArray());
        }

        private void pnlCanvas_MouseUp ( object sender, MouseEventArgs e )
        {
            paint = false;
        }

        private void nudBrushSize_ValueChanged ( object sender, EventArgs e )

            //when value of combobox changes, read value into brush size variable
            pinselGröße = Convert.ToInt32 ( nudBrushSize.Value );
        }

        private void cmbColor_SelectedIndexChanged ( object sender, EventArgs e )
        {            
            int index = cmbColor.SelectedIndex;
            color.Dispose ();
            switch ( index )
            {
                case 0:
                    {
                        color = new SolidBrush ( Color.Black );
                        break;
                    }
                case 1:
                    {
                        Console.WriteLine ( "Geht" );
                        color = new SolidBrush ( Color.Red );
                        break;
                    }
                case 2:
                    {
                        color = new SolidBrush ( Color.Blue );
                        break;
                    }
                case 3:
                    {
                        color = new SolidBrush ( Color.Green );
                        break;
                    }
            }
        }


    }
}

when i do it this way:

private void pnlCanvas_MouseMove ( object sender, MouseEventArgs e )
        {
            if ( paint )
            {
                Graphics grfx = pnlCanvas.CreateGraphics();
                ////put old position of mouse into variable
                int altePosX = e.X;
                int altePosY = e.Y;
                //grfx.FillEllipse ( color, e.X, e.Y, pinselGröße, pinselGröße );
                grfx.FillRectangle(color, e.X, e.Y, pinselGröße, pinselGröße);
                grfx.Dispose();
                //pointListe.Add(e.Location);
                //pnlCanvas.Invalidate();
            }
        }

        //private void pnlCanvas_Paint(PaintEventArgs e)
        //{
        //    Console.Write("mjsda2");
        //    e.Graphics.DrawLines(new Pen(color), pointListe.ToArray());
        //}

i get this:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:52:03+00:00Added an answer on June 14, 2026 at 3:52 am

    I wasn’t sure which we were going for in drawing modes, so here is two versions:

    Also worth noting, your paint event handler had the wrong signature, and thus might not have been hooked up to the pnlCanvas.

    When doing paint code you should (almost) never need to call CreateGraphics – its usually a sign of “you’re doing it wrong”.

    This will let you draw lines by clicking points:

    public partial class Form1 : Form
    {
    
        SolidBrush color;
        List<Point> pointListe;
        Point _mousePoint;
    
        public Form1()
        {
            InitializeComponent();
            pointListe = new List<Point>();
            color = new SolidBrush(Color.Black);
        }
    
        private void btnClear_Click(object sender, EventArgs e)
        {
            pointListe.Clear();
            pnlCanvas.Invalidate();
        }
    
        private void pnlCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            pointListe.Add(e.Location);
        }
    
        private void pnlCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            _mousePoint = e.Location;
            pnlCanvas.Invalidate();
        }
    
        private void pnlCanvas_Paint(object sender, PaintEventArgs e)
        {
            if (pointListe.Count > 1)
            {
                e.Graphics.DrawLines(new Pen(color), pointListe.ToArray());
            }
    
            if (pointListe.Any())
            {
                e.Graphics.DrawLine(new Pen(color), pointListe.Last(), _mousePoint);
            }
        }
    
    }
    

    And this will draw one continuous line:

    public partial class Form1 : Form
    {
    
        SolidBrush color;
        List<List<Point>> _lines;
        Boolean _mouseDown;
    
        public Form1()
        {
            InitializeComponent();
            _lines = new List<List<Point>>();
            color = new SolidBrush(Color.Black);
            _mouseDown = false;
        }
    
        private void btnClear_Click(object sender, EventArgs e)
        {
            _lines.Clear();
            pnlCanvas.Invalidate();
        }
    
        private void pnlCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            _mouseDown = true;
            _lines.Add(new List<Point>());
        }
    
        private void pnlCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (_mouseDown)
            {
                _lines.Last().Add(e.Location);
                pnlCanvas.Invalidate();
            }
        }
    
        private void pnlCanvas_MouseUp(object sender, MouseEventArgs e)
        {
            _mouseDown = false;
        }
    
        private void pnlCanvas_Paint(object sender, PaintEventArgs e)
        {
            foreach (var lineSet in _lines)
            {
                if (lineSet.Count > 1)
                {
                    e.Graphics.DrawLines(new Pen(color), lineSet .ToArray());
                }   
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.