HighTechTalks DotNet Forums  

Double Buffering to draw lines

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Double Buffering to draw lines in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Rithesh Pai
 
Posts: n/a

Default Double Buffering to draw lines - 09-15-2005 , 06:18 AM






Hi all,
I had been been trying for the double buffering technique to draw
line on movement of mouse on pictureBox.The following is the piece of code:



private void pictureBox1_MouseMove(object sender,MouseEventArgs
e)

{

if(e.Button==MouseButtons.Left)

{



Bitmap backbuffer=new
Bitmap(pictureBox1.Width,pictureBox1.Height);

Graphics bckgphcs=Graphics.FromImage(backbuffer);



Graphics picgphcs=pictureBox1.CreateGraphics();



SolidBrush redbrush=new SolidBrush(Color.Red);

Pen redpen=new Pen(redbrush,2);



endpoint.X=e.X;endpoint.Y=e.Y;



bckgphcs.DrawImage(pictureBox1.Image,0,0);

bckgphcs.DrawLine(redpen,startpoint,endpoint);



picgphcs.DrawImage(backbuffer,0,0);



bckgphcs.Dispose();

picgphcs.Dispose();



backbuffer.Dispose();

redbrush.Dispose();redpen.Dispose();

}



It works well when I try for the first time.As I do it again and again,my
CPU resources shoot upto 100% and line drawing becomes slow.



Is there any way out????



regards

Rithesh



Reply With Quote
  #2  
Old   
Frank Hileman
 
Posts: n/a

Default Re: Double Buffering to draw lines - 09-15-2005 , 07:32 AM






Firstly, if you are to do custom drawing, don't use a PictureBox -- use a
custom control. Secondly, do not dispose the back buffer every-time. If it
has not changed size you can reuse it.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor

"Rithesh Pai" <rithesh (AT) sninform (DOT) com> wrote

Quote:
Hi all,
I had been been trying for the double buffering technique to draw
line on movement of mouse on pictureBox.The following is the piece of
code:



private void pictureBox1_MouseMove(object sender,MouseEventArgs
e)

{

if(e.Button==MouseButtons.Left)

{



Bitmap backbuffer=new
Bitmap(pictureBox1.Width,pictureBox1.Height);

Graphics bckgphcs=Graphics.FromImage(backbuffer);



Graphics picgphcs=pictureBox1.CreateGraphics();



SolidBrush redbrush=new SolidBrush(Color.Red);

Pen redpen=new Pen(redbrush,2);



endpoint.X=e.X;endpoint.Y=e.Y;



bckgphcs.DrawImage(pictureBox1.Image,0,0);

bckgphcs.DrawLine(redpen,startpoint,endpoint);



picgphcs.DrawImage(backbuffer,0,0);



bckgphcs.Dispose();

picgphcs.Dispose();



backbuffer.Dispose();

redbrush.Dispose();redpen.Dispose();

}



It works well when I try for the first time.As I do it again and again,my
CPU resources shoot upto 100% and line drawing becomes slow.



Is there any way out????



regards

Rithesh





Reply With Quote
  #3  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: Double Buffering to draw lines - 09-15-2005 , 02:26 PM



You're breaking all the rules here.

#1 Don't use GetGraphics to obtain a graphics object to draw onto.

#2 Don't draw in event handlers that are not meant for drawing.

#3 Don't create objects that use large amounts of memory in event handlers
that occur many times a second.

#4 Don't use PictureBox for anything other than displaying a static picture.



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





"Rithesh Pai" <rithesh (AT) sninform (DOT) com> wrote

Quote:
Hi all,
I had been been trying for the double buffering technique to draw
line on movement of mouse on pictureBox.The following is the piece of
code:



private void pictureBox1_MouseMove(object sender,MouseEventArgs
e)

{

if(e.Button==MouseButtons.Left)

{



Bitmap backbuffer=new
Bitmap(pictureBox1.Width,pictureBox1.Height);

Graphics bckgphcs=Graphics.FromImage(backbuffer);



Graphics picgphcs=pictureBox1.CreateGraphics();



SolidBrush redbrush=new SolidBrush(Color.Red);

Pen redpen=new Pen(redbrush,2);



endpoint.X=e.X;endpoint.Y=e.Y;



bckgphcs.DrawImage(pictureBox1.Image,0,0);

bckgphcs.DrawLine(redpen,startpoint,endpoint);



picgphcs.DrawImage(backbuffer,0,0);



bckgphcs.Dispose();

picgphcs.Dispose();



backbuffer.Dispose();

redbrush.Dispose();redpen.Dispose();

}



It works well when I try for the first time.As I do it again and again,my
CPU resources shoot upto 100% and line drawing becomes slow.



Is there any way out????



regards

Rithesh






Reply With Quote
  #4  
Old   
Jeff
 
Posts: n/a

Default Re: Double Buffering to draw lines - 09-17-2005 , 11:24 AM



If you are open to looking at alternatives to Picturebox
Take a look at our MetaDraw component ( www.Bennet-Tec.com)
With MetaDraw it's easy
Take a look at the following web page
www.btis.com/btproducts/MetaDraw/WebSamples/Drawing/md3_demo.htm
This shows MetaDraw in use for end-user drawing within a web browser
Of course the same thing can be done with MetaDraw in your EXE
application.
Note that you can View Source on this web page to see the underlying
code
and how easy this is.
With MetaDraw you just set an EditMode property to specify the result
of user mouse actions ( drawing lines, rectangles, other shapes,
selecting or moving shapes, etc) Lot's more. Just as important as
ease of use, MetaDraw will also allow you to capture the drawn shapes
as individual elements and then when you save and reload you can
continue to recognize and modify the shapes. You'll also get higher
quality printed output and smaller saved files.

I hope this is helpful to you.


* * Please include a copy of this message with your reply

Jeff Bennett
Jeff (AT) Bennet-Tec (DOT) Com

* Bennet-Tec Information Systems, Inc
* 50 Jericho Tpk, Jericho, NY 11753
* Phone 516 997 5596, Fax - 5597
* RELIABLE Components Make You Look Sharp!
* TList/Pro * ALLText HT/Pro * MetaDraw *
* Custom Software Development Services Too.
* WWW.Bennet-Tec.Com

=================== ===================


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.