Jump to content
  • Advertisement
Sign in to follow this  
BeanDog

[.net] Backbuffering user control [Solved]

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by DaWanderer
If 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.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
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.


hmmmmm.....for fun? :P I didn't really think about that one.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!