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