simple drawing with a mouse

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
 
D

Diogo

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
 
J

Jeff Gaines

On 17/06/2007 in message
<[email protected]>
Anyone could point me in the right direction?

What about saving the points in mouse down/mouse up and painting in the
paint event. There is a Scribble sample in the help - it's C++ but may help.
 
B

Bob Powell [MVP]

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.
 
K

KWienhold

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.
 
G

Guest

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 ;
}
 
B

Bob Powell [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top