simple drawing with a mouse

  • Thread starter Thread starter rogersteph
  • Start date Start date
R

rogersteph

Hello,

I'm trying to create a small program, where a user can draw a simple
line with the mouse. Imagine a signature application where you can
write your name with the mouse. Or a tablet. Just like in paint.

I've tried something like this:


private void pbMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.shouldPaint = true;
}
}

private void pbMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
shouldPaint = false;
}
}

private void pbMouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)
{
Graphics graph = pictureBox1.CreateGraphics();
Pen pen = new Pen(Color.Black);
graph.DrawRectangle(pen, e.X, e.Y, 1, 1);
graph.Dispose();
}
}

This works, however it's too slow. That is if you move the mouse fast
like you would do if you were writing lots of points are skipped and
it's not a solid line but some random dots.

I guess the MouseMove Event is not fired fast enough or it's not
possible to process so many events because the drawing operation takes
too much time.

Anyone could point me in the right direction?

Thanks
 
Anyone could point me in the right direction?

I think you should try to create your Graphics instance in pbMouseDown
and dispose it in pbMouseUp
 
The art to this is in the correct implementation of the event-driven system.

You should NEVER draw in a mouse move event.

When the mouse goes down, begin accumulating the points at which the mouse
was located.

As the mouse moves, accumulate more points and signal the screen dirty.

As the mouse goes up, store all the points in the sequence and signal the
screen dirty.

In the Paint routine, draw all the point arrays you've stored as a line,
including, if any, the one currently under construcition.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hello,

I'm trying to create a small program, where a user can draw a simple
line with the mouse. Imagine a signature application where you can
write your name with the mouse. Or a tablet. Just like in paint.

I've tried something like this:

private void pbMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.shouldPaint = true;
}

}

private void pbMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
shouldPaint = false;
}

}

private void pbMouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)
{
Graphics graph = pictureBox1.CreateGraphics();
Pen pen = new Pen(Color.Black);
graph.DrawRectangle(pen, e.X, e.Y, 1, 1);
graph.Dispose();
}

}

This works, however it's too slow. That is if you move the mouse fast
like you would do if you were writing lots of points are skipped and
it's not a solid line but some random dots.

I guess the MouseMove Event is not fired fast enough or it's not
possible to process so many events because the drawing operation takes
too much time.

Anyone could point me in the right direction?

Thanks

As a sidenode:
Pen also implements IDisposable, so you should probably dispose you
Pen as well as your Graphics context.
 
Try This Out Man!!!

private System.Drawing.Drawing2D.GraphicsPath mousepath;
private bool Draw = false ;

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (Draw == true)
{
mousepath.AddLine(new Point(e.X, e.Y), new Point(e.X,
e.Y));

Graphics g = this.CreateGraphics();
Pen p = Pens.DarkCyan;
g.DrawPath(p, mousepath);

}
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mousepath = new System.Drawing.Drawing2D.GraphicsPath();
Point startpoint = new Point(MousePosition.X, MousePosition.Y);
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;

this.Cursor = Cursors.Cross;
Draw = true;

}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Draw = false ;
}
 
I invite you to visit the GDI+ FAQ to discover why this is a horribly
bad example.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top