Saving a PictureBox bitmap with changes drawn...

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")
 
K

Ken Tucker [MVP]

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
 
N

nobody

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.
 
A

Armin Zingler

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.
 
N

nobody

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."
 
N

nobody

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)
 
N

nobody

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

Back to Delphi we go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top