469,654 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,654 developers. It's quick & easy.

Issue with overwriting a bitmap in VB.NET??? (seems to be locked)

I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release
any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane
Nov 20 '05 #1
9 7955
Cor
Hi Shane,

I am not really getting what you are after, but I think that you can have a
look for the "memorystream", that is for playing with byte areas, but I use
it only for bitmaps.

Cor
I use a bitmap class new bitmap(filepath)
this should and does load my jpg into memory.
I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the original.
If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Nov 20 '05 #2
On 2004-01-30, SStory <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote:
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release
any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.


You might want to open the file and read it into a byte array - then
create a memory stream from the array. Then you can close the file and create
the bitmap from the memory stream....

Dim reader As New FileStream(filepath, FileAccess.Read)
Dim bytes(reader.Length - 1) As Byte

reader.Read(bytes, 0, bytes.Length)
reader.Close()

Dim memory As New MemoryStream(bytes)
Dim bmp As New BitMap(memory)
memory.Close()

....

bmp.Save(filepath, Imaging.ImageFormat.Jpeg)

This is untested - so you may have to play around a bit...

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
"You're very sure of your facts, " he said at last, "I
couldn't trust the thinking of a man who takes the Universe
- if there is one - for granted. "
Nov 20 '05 #3
"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> schrieb
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg)
to save it; overwriting the original.

If this outfile path is different there is no problem but when it is
the same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has
it open.

Should the bitmap constructor open a file, read it in, close it and
release any locks?

If so why I am having problems trying to overwrite the file?


The file is not unlocked. Instanciate from a filestream instead:

dim fs as new filestream(filepath, filemode.open)
bmp = new bitmap(fs)
fs.close

Though, I don't know why you are getting the GDI+ error.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
You need to copy the Bitmap( mybitmap) to another NEW Bitmap in-memory and
dispose of the orignal and then save it to the same file name using the new
bitmap.
GDI+ locks the the file you open until you dispose of the original in-memory
copy. Once you do that, you should be able to over-write the old file, using
the same file name.
james

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane

Nov 20 '05 #5
* "SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> scripsit:
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release
any locks?


Use something like this to load the file:

\\\
Dim fs As FileStream = New FileStream("C:\WINDOWS\Angler.bmp", FileMode.Open)
Dim bmp As Bitmap = New Bitmap(fs)
..
..
..
bmp.Dispose()
fs.Close()
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Thanks, James,

This is an interesting idea.

I found out that it was being locked until I got rid of it but didn't think
about a in memory copy. I just made it make a backup of the file delete the
original, save as original filename and delete the backup file.

The in memory idea might be better though.

What do you mean by dispose of? Call .Dispose and set to nothing?
Does that unlock the file?
Or must I delete the file from disk(dispose)?

Thanks,

Shane
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
You need to copy the Bitmap( mybitmap) to another NEW Bitmap in-memory and
dispose of the orignal and then save it to the same file name using the new bitmap.
GDI+ locks the the file you open until you dispose of the original in-memory copy. Once you do that, you should be able to over-write the old file, using the same file name.
james

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to

save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and

release
any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane


Nov 20 '05 #7
Thanks to all for the replies.

Before getting these, I discovered that the Bitmap object must lock the file
and so made a backup file of the original, wrote out the modified bitmap to
the original filename and then deleted the bkp.

Thanks for the ideas of in memory bitmaps that were given. I appreciate
them.

Shane

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane

Nov 20 '05 #8
Cor
Hi Shane,

Although I told you to look at the memorystream I would first try the
complete solution you have got from Herfried. (Including the dispose).

Cor
Nov 20 '05 #9
Great!

Thanks I will,

Shane

"Cor" <no*@non.com> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Hi Shane,

Although I told you to look at the memorystream I would first try the
complete solution you have got from Herfried. (Including the dispose).

Cor

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by James Dean | last post: by
2 posts views Thread by alex | last post: by
3 posts views Thread by CSH | last post: by
7 posts views Thread by Dennis | last post: by
4 posts views Thread by Michael C | last post: by
2 posts views Thread by Michael Maes | last post: by
reply views Thread by Andy_Khosravi | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.