How do I save a pic AND graphics drawn?

05-30-2006, 01:14 PM
I have a picturebox that I am saving its image to a file. It works fine, but if I draw any graphics on the picturebox, they are not saved, just the picture is. Is there a way to save both?Basicaly, I use this to create graphics at different times:

Dim GFX as graphics = picturebox1.CreateGraphics
gfx.drawEllipse(MyPen, 10,10,10,10)

then later, I want to save the image and graphics together. I can save, but no graphics appear. If I use this, i get a blank image:

dim bmp as new bitmap(Picturebox1.width, picturebox1.height, imaging.pixelformat.formay32bbpaRGB)
Dim g as Graphics = Graphics.FromImage(bmp)

05-30-2006, 06:42 PM
When you get a graphics object it draws onto one surface only.

You are drawing onto the surface of a control. The control also has an Image property that was drawn first, the two drawings are on different surfaces though. It's like the sheets of transparent film used by animators. The property is on one sheet, the drawing on the suface is on another. You lay the two sheets on top of each other - so the drawing appears on top of the Image property. However the two sheets are still distinct items, and saving one does not save the other!.

You should note that the drawing you do with the graphics object you got with createGraphics disappears when you move another form across yours, or minimise the form, or move it offscreen. That is because the paint event fires to repaint the form, it will also redraw the image in the image property. The drawing you did with me.createGraphics is not refreshed though.

So, you need a different approach, I think here it might be easiest to get your graphics from the image being displayed:
Dim Gfx as Graphics = Graphics.FromImage(Picturebox1.Image)
gfx.drawellipse(myPen, 10,10,10,10)
' now the image in the property is updated, but it has not been redrawn yet:
' dispose of gfx explicitly - otherwise you may get memory problems.

This solves the problems, as you are drawing onto the image that is in the property - so it will be saved. It will also be drawn when the paint event fires, so you won't lose any drawing.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum