   [.net] Backbuffering user control [Solved]
  I'm making a user control that I paint myself.  It sometimes takes a few milliseconds, so I need backbuffering.  I just figured my paint event would look like this:OnPaint(EventInfo e)  Bitmap b = new Bitmap(Image);//copy dimensions of the control  Graphics g = new Graphics(b);//INVALID CODE  g.DrawStuff();  e.Graphics.CopyFrom(g);The problem is that I don't know how to get a graphics object that will write to a bitmap.  Also I don't think that first line is really a good idea.  How is backbuffering supposed to be done?~BenDilts( void );[Edited by - BeanDog on December 3, 2005 10:21:43 AM]
  1. To get a garphics from an image (or a bitmap) use the Graphics.FromImage() method.2. You really should not have to do back buffering yourself.  Turn on double buffering in your control by seting the style flags with Setstyle().  Note that Setstyle is protected so you will have to do it in your own subclass of Control or Form or UserControl or whatever.public MyControl(){//constructor  this.Setstyle(Controlstyles.DoubleBuffer,true);  this.Setstyle(Controlstyles.UserPaint,true);  this.Setstyle(Controlstyles.AllPaintingInWmPaint,true);}Hope this helps.   ;  the term is double buffering   ;  If you really need to manually create a buffer, here's how:private Bitmap bufferBitmap;private Graphics bufferGraphics;public void CreateBackBuffer(int bufferWidth, int bufferHeight){   //Create the proper image objects to hold the actual pixel data   bufferBitmap = new Bitmap(bufferWidth, bufferHeight);   //Wrap a graphics object around your new bitmap   bufferGraphics = Graphics.FromImage(bufferBitmap);}protected override void OnPaint(PaintEventArgs e){   //Some point later...draw something   bufferGraphics.Clear(Color.Green);   //Some more drawing...   //Flip the back buffer to the surface of the control at (0,0)   e.Graphics.DrawImageUnscaled(bufferBitmap, 0, 0);}You should really use the built-in double buffering, but this is a way to maually implement.  The idea is to create a Graphics object from a Bitmap object.  To work with the buffer, you draw to it with the bufferGraphics object and then blit the bufferBitmap object to the screen using one of the DrawImage methods on the control's Graphics object.Hope this helps :)[Edited by - DaWanderer on December 3, 2005 7:18:38 PM];Quote:Original post by DaWandererIf you really need to manually create a buffer, here's how:*** Source Snippet Removed ***You should really use the built-in double buffering, but this is a way to maually implement.  The idea is to create a Graphics object from an Image object, which is created from a Bitmap object.  To work with the buffer, you draw to it with the bufferGraphics object and then blit the bufferImage object to the screen using one of the DrawImage methods on the control's Graphics object.Hope this helps :)Since Bitmap extends Image, why did you create an Image object from the HBitmap from the Bimap so that you could create the Graphics object? You could have created the Graphics object from the Bitmap itself.;Quote:Original post by capn_midnightSince Bitmap extends Image, why did you create an Image object from the HBitmap from the Bimap so that you could create the Graphics object? You could have created the Graphics object from the Bitmap itself.hmmmmm.....for fun? :P  I didn't really think about that one.