Saving a PictureBox bitmap with changes drawn...

  • Thread starter Thread starter nobody
  • Start date Start date
N

nobody

How do I save a PictureBox bitmap with changes
drawn on the bitmap?
This doesn't do it:
PictureBox1.Image.Save("key30.bmp")
 
Hi,

Dim fs As New System.IO.FileStream("c:\key30.bmp", IO.FileMode.Open)
Dim bm As New Bitmap(fs)

fs.Close()

Dim g As Graphics = Graphics.FromImage(bm)
g.FillEllipse(Brushes.Red, 10, 10, 100, 100)

g.Dispose()

bm.Save("C:\key30.bmp", Imaging.ImageFormat.Bmp)

Ken
 
Armin said:
Do you really paint on the bitmap assigned to the Picturebox' Image property
or on the Picturebox?

Here's what I got....

Dim GraphicsFun As System.Drawing.Graphics
GraphicsFun = PictureBox1.CreateGraphics
Dim aFont As New System.Drawing.Font("MS Sans Serif", 11, _ FontStyle.Bold)
Dim drawBrush As New _ System.Drawing.SolidBrush(System.Drawing.Color.Black)
Dim drawFormat As New System.Drawing.StringFormat

GraphicsFun.DrawString("E", aFont, drawBrush, 20, 31, drawFormat)

'This draws on the picture, but when saved, it doesn't
'save what is drawn, just saves the original loaded from file.
 
nobody said:
Here's what I got....

Dim GraphicsFun As System.Drawing.Graphics
GraphicsFun = PictureBox1.CreateGraphics
Dim aFont As New System.Drawing.Font("MS Sans Serif", 11, _
FontStyle.Bold)
Dim drawBrush As New _
System.Drawing.SolidBrush(System.Drawing.Color.Black)
Dim drawFormat As New System.Drawing.StringFormat

GraphicsFun.DrawString("E", aFont, drawBrush, 20, 31, drawFormat)

'This draws on the picture, but when saved, it doesn't
'save what is drawn, just saves the original loaded from file.

You are directly drawing on the Picturebox, not on the image assigned to the
Image property. To draw on the image:

dim g as graphics
g = graphics.fromimage(PictureBox1.image)
g.DrawString("E", aFont, drawBrush, 20, 31, drawFormat)
g.dispose

If you didn't assign an image to the Image property, you must do it before.
 
Armin said:
You are directly drawing on the Picturebox, not on the image assigned to the
Image property. To draw on the image:

dim g as graphics
g = graphics.fromimage(PictureBox1.image)
g.DrawString("E", aFont, drawBrush, 20, 31, drawFormat)
g.dispose

If you didn't assign an image to the Image property, you must do it before.

I'm getting this error message below....

"An unhandled exception of type 'System.Exception' occurred in
system.drawing.dll
Additional information: A Graphics object cannot be created from an
image that has an indexed pixel format."
 
nobody said:
I'm getting this error message below....

"An unhandled exception of type 'System.Exception' occurred in
system.drawing.dll
Additional information: A Graphics object cannot be created from an
image that has an indexed pixel format."

The line it goes to when the error occurs is....
g.DrawString("E", aFont, drawBrush, 20, 31, drawFormat)
 
Back
Top