Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving graphics

Status
Not open for further replies.

zenox0

Programmer
Mar 28, 2005
14
0
0
CA
Hey all,
I have created a custom control. During my repaint function I am drawing a grid. My thoughts were that instead of drawing the grid I could just save the graphics after the first time, and then redraw the saved image.

I tried two seperate things. First was using
graphics.Save ( ) / graphics.Restore ( savedGraphics )
When I was using that my graphics ended up being completely blank.

I also tried......
Bitmap tempGraphics = new Bitmap ( this.width, this.Height, graphics );

graphics.DrawImage ( Image.FromBitmap ( tempGraphics ) );

The drawimage code I put here isnt the exact code. It was something like Image.FromHBitmap. I dont have it with me at current tho. I also tried saving the created bitmap to a file but it came out blank (tempGraphics.Save ( "test.bmp" ); It was the right width and height however it was just a blank file.

I always saved the graphics once everything was drawn then toggle a flag. Then when drawing again check the flag. If its set its suppost to load the image rather than redraw. Can anyone help me out? Maybe give an example of saving a Graphics object to a file? or even just make an Image or Bitmap of it? Thanks.
 
What you are talking about is similar to double buffering.

using System.Drawing;
...
private Bitmap _Buffer;
...

//I suggest doing this in a constructor somewhere
_Buffer = new Bitmap(width, height);
Graphics g = Graphics.FromImage(_Buffer);
//draw stuff
//ex: g.FillRectangle(Brushes.Green, 0, 0, 100, 100);
g.Dispose();

...

protected override void OnPaint(PaintEventArgs e)
{
//Draw stuff

e.Graphics.DrawImageUnscaled(_Buffer, new Point(x,y));
//or yourGraphicsObject.DrawImageUnscaled(...); if you are drawing to another graphics object

//Draw stuff
}


Happy Coding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top