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:

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:
And this will draw one continuous line: