simple drawing with a mouse - rogerstep

17-Jun-07 03:29:42
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
reply
 
 

simple drawing with a mouse - Diogo

17-Jun-07 04:14:43
I think you should try to create your Graphics instance in pbMouseDown 
and dispose it in pbMouseUp
reply
 

simple drawing with a mouse - Jeff Gaines

17-Jun-07 05:01:10
On 17/06/2007 in message


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.

--
Jeff Gaines
reply
 

simple drawing with a mouse - Bob Powell [MVP]

17-Jun-07 05:49:09
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.
reply
 

simple drawing with a mouse - Jared

17-Jun-07 06:08:14
http://www.microsoft.com/downloads/details.aspx?familyid=84bbefa4-7047-41df-8583-e3bdbf9d805f&displaylang=en
reply
 

simple drawing with a mouse - Robbe Morris - MVP C#

17-Jun-07 08:56:47
This has worked relatively well for me.

http://www.eggheadcafe.com/articles/dotnetcompactframework_capture_signature_to_file.asp

--
Robbe Morris
EggHeadCafe.com
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
reply
 

simple drawing with a mouse - KWienhold

20-Jun-07 05:12:48
As a sidenode:
Pen also implements IDisposable, so you should probably dispose you
Pen as well as your Graphics context.
reply
 

simple drawing with a mouse - Xishan Shigr

25-Jun-07 07:12:00
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 ;
}
reply
 

simple drawing with a mouse - Bob Powell [MVP]

05-Jul-07 03:58:30
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.
reply
 
Saving control sate to persistent medium
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class