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

S

SStory

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
 
C

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
 
T

Tom Shelton

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

Armin Zingler

SStory said:
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
 
J

james

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
 
H

Herfried K. Wagner [MVP]

* "SStory said:
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()
///
 
S

SStory

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
 
S

SStory

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
 
C

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
 

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

Similar Threads


Top