Writing bitmap properties?

D

David Veeneman

I'm opening a JPG file to read and write its EXIF properties. I'm using very
simple code to open the file:

myBitmap = new Bitmap(filePath);

Reading and writing the properties is working fine, but when I go to save
the file, I'm getting an exception that reads "A generic error occurred in
GDI+." Here's the code I'm using to save the file back to the same name it
was opened from:

myBitmap.Save(filePath, ImageFormat.Jpeg);

I tried saving to a different file name, and that save works with no
problems. That suggests a file-locking problem.

Presumably, my app is putting a lock on the JPG file when I open it. How can
I release the lock, so I can write modified properties back to the file?
Thanks in advance for your help.
 
M

Michael Phillips, Jr.

Presumably, my app is putting a lock on the JPG file when I open it. How
can I release the lock, so I can write modified properties back to the
file?

Open the image using a FileStream.
 
J

Jay Riggs

I'm opening a JPG file to read and write its EXIF properties. I'm using very
simple code to open the file:

myBitmap = new Bitmap(filePath);

Reading and writing the properties is working fine, but when I go to save
the file, I'm getting an exception that reads "A generic error occurred in
GDI+." Here's the code I'm using to save the file back to the same name it
was opened from:

myBitmap.Save(filePath, ImageFormat.Jpeg);

I tried saving to a different file name, and that save works with no
problems. That suggests a file-locking problem.

Presumably, my app is putting a lock on the JPG file when I open it. How can
I release the lock, so I can write modified properties back to the file?
Thanks in advance for your help.

David,

Check this out: http://www.bobpowell.net/imagefileconvert.htm


-Jay
 
D

David Veeneman

Thanks! I'm pretty sure that's where the problem lies.

For the benefit of anyone else reading this thread, GDI+ apparently keeps an
image file open so long as the Image is used, even if the file is opened
with a stream reader and the stream reader is properly closed and disposed.
The workaround is to save the image file under a different name, close and
dispose of the image, delete the original, rename the copy with the original
name, and reload the renamed copy. Kind of clunky, but it seems to work.
More details are in the Bob Powell article.
 
P

Peter Duniho

David said:
Thanks! I'm pretty sure that's where the problem lies.

For the benefit of anyone else reading this thread, GDI+ apparently keeps an
image file open so long as the Image is used, even if the file is opened
with a stream reader and the stream reader is properly closed and disposed.
The workaround is to save the image file under a different name, close and
dispose of the image, delete the original, rename the copy with the original
name, and reload the renamed copy. Kind of clunky, but it seems to work.
More details are in the Bob Powell article.

For what it's worth, I have worked around similar issues by simply
copying the opened bitmap to a new Bitmap instance (one created from
scratch rather than by reading from a file), and then discarding the
original. This is different technique that also has the effect of
releasing the opened file, allowing it to be overwritten.

Personally, I think it's a simpler way to do it than dealing with
writing a second file. Not difficult, but you'll need to use something
like the Path.GetTempFileName() and managing the extra copies of the
image seem to me to be more awkward using that technique.

Just an alternative to consider.

Pete
 

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