473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Writing bitmap properties?

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.
Oct 1 '07 #1
5 2082
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.

"David Veeneman" <da****@nospam.comwrote in message
news:uZ**************@TK2MSFTNGP06.phx.gbl...
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.

Oct 1 '07 #2
On Oct 1, 1:48 pm, "David Veeneman" <dav...@nospam.comwrote:
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

Oct 1 '07 #3
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.
Oct 1 '07 #4
David Veeneman wrote:
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
Oct 2 '07 #5
Thanks--that was the problem.
Oct 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Erwin | last post by:
At the moment I'm using a report which contains an indicator to show if a Service group of the company isn't working well or is working perfectly. This indicator is a "*" which looks like a traffic...
2
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular...
0
by: Joaquin Corchero | last post by:
Hi, I have this code which it should change the EXIF file properties of images, but there is something that doesn't work, any ideas??? Private Sub ShowBitMapProp(ByVal sFile As String) Dim...
5
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
2
by: Mad Scientist Jr | last post by:
I have a bitmap (32 pixels high, 8192 pixels wide) that contains 255 images, each 32 pixels wide, that I would like to chop up into individual 32x32 bitmap files. Rather than spending hours in...
6
by: Dave Harvey | last post by:
I have developed a standard .NET control which displays medical images and works fine in applications, but increasingly, my customers are wishing to use it in an ASP.NET environment, so I am...
5
by: active | last post by:
I need to know how many colors in the color table are actually used. Do I need to use Windows API? I been searching the Bitmap doc and can't find a property that would help. Is there a way...
0
by: raylopez99 | last post by:
I have a minor problem locating an image file to load as a Bitmap. A resource file "NudeSusan.jpeg" (which I think is actually a bmp file, originally taken from a jpeg of model Susan Spears) is...
6
by: bradyounie | last post by:
I'm writing a program that displays a user-supplied Bitmap and then writes text fields to it. These "text fields" are things that the user can move around on the image, but to render them...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.