Bitmap problems in VB.net

C

CSH

Hi all,

I've run into a small problem opening and saving bitmaps. Concider the
following code:

Dim oBM as Bitmap
Dim cFileName as String

... some code to get the filename etc...

oBM = New Bitmap(cFileName)
oPanel.image = oBM ' <- oPanel is a panel object on my form.

This works like I expect it to, but with a twist. When I try to save the
bitmap again like such:

oPanel.image.Save(cFileName)

It generated a runtime error since the file is still open/in use. The file
stays in use even after disposing of the object holding the bitmap. I have
to close my application to release the file. Saving to a different filename
does work btw, but that's not what I need at the moment.

Now I've tried it another way using a filestream:
Dim oBM as Bitmap
Dim cFileName as String
Dim s as FileStream

... some code to get the filename etc...

s = New FileStream(cFileName, FileMode.Open)
oBM = New Bitmap(s)
s.Close()
oPanel.image = oBM

After this my panel correctly shows the image and the file isn't in use
anymore (hurray). But when I try to save the bitmap to a file now I get a
"generic GDI+ error" not matter if I save to a (different) filename or try
it using a stream.

oPanel.image.Save(cFileName) '<-- this is where it crashes.

Any ideas on what I'm doing wrong here? Getting the image via the filestream
is working for me in the way that the file is not locked afterwards. But I
need to save that image again.

Rinze van Huizen
 
S

scorpion53061

saveBmp = new bitmap (width,height, PixelFormat.Format24bppRgb)

OR

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)

OR
 
C

csh

scorpion53061 said:
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)

Thanks scorpion, this did it for me. But leave out this line:
g.FillEllipse(Brushes.Red, 10, 10, 100, 100)
and it bombs with the generic GDI error.
So now I've just drawn an elipse with an immense radius. Which turns out
doesn't draw anything, but it somehow does the trick and lets me save the
image again.

That leaves me to wonder why I need to draw the elipse in order to be able
to save it again? My immideate problem is now solved, but this still leaves
me a bit confused.

Rinze van Huizen
 
S

scorpion53061

I am glad your immediate problem is solved.



csh said:
Thanks scorpion, this did it for me. But leave out this line:
g.FillEllipse(Brushes.Red, 10, 10, 100, 100)
and it bombs with the generic GDI error.
So now I've just drawn an elipse with an immense radius. Which turns out
doesn't draw anything, but it somehow does the trick and lets me save
the
image again.

That leaves me to wonder why I need to draw the elipse in order to be
able
to save it again? My immideate problem is now solved, but this still
leaves
me a bit confused.

Rinze van Huizen
 

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