Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Overwriting image metadata

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Kelley - 19 May 2008 15:45 GMT
I need to read the metadata of an image, display it on the screen and allow
the user to change it and then re-write the metadata back into the image.
The issue I am having is if I try to rename or delete the existing image
before re-writing the image file I get an error stating that the image is in
use.  I am using Image.FromFile() to read the file.  I have tried disposing
of the image and running the garbage collector (GC.Collect()), but it still
doesn't work.  Is there a way to actually close the file so I can
delete/rename it?

Thanks,

Tim
Peter Duniho - 19 May 2008 19:20 GMT
> I need to read the metadata of an image, display it on the screen and  
> allow
[quoted text clipped - 8 lines]
> doesn't work.  Is there a way to actually close the file so I can
> delete/rename it?

Disposing the Image instance you created using FromFile() should do it.  
Can you post a concise-but-complete code sample that demonstrates what you  
tried and how it fails to work?

Pete
Tim Kelley - 20 May 2008 14:21 GMT
Here is the code snippet:

Image targetImg;

targetImg = Image.FromFile("O:\\xxxx\\xx\\xxxx\\xxx\\xxxx\\0004.jpg");

targetImg.Dispose();

GC.Collect();

targetImg = null;

Thanks for your help,

Tim

>> I need to read the metadata of an image, display it on the screen and
>> allow
[quoted text clipped - 14 lines]
>
> Pete
Barry Kelly - 20 May 2008 17:07 GMT
> Here is the code snippet:

A snippet isn't a complete example. Your code doesn't show any metadata
editing, renaming or deleting, and doesn't compile when pasted into a
file and passed to csc.

> Image targetImg;
>
> targetImg = Image.FromFile("O:\\xxxx\\xx\\xxxx\\xxx\\xxxx\\0004.jpg");

GDI+ (which Image is part of) does indeed keep a handle to the file on
disk open for as long as the image isn't disposed, when you load from a
file, as I recall.

> targetImg.Dispose();
>
> GC.Collect();

This isn't recommended, and won't have an effect on your problem here.
For one thing, you've already disposed the image, so you won't be
getting the benefit of finalization (for which
GC.WaitForPendingFinalizers() would be a better choice, anyhow), and
secondly, the targetImg object is still rooted, as you don't null it out
until after calling the GC, so it itself won't be collected.

> targetImg = null;

Here's a complete example which successfully opens, disposes and renames
an image:

---8<---
using System;
using System.IO;
using System.Drawing;

class App
{
   static void Main(string[] args)
   {
       Image img = Image.FromFile(args[0]);
       img.Dispose();
       File.Move(args[0], args[0] + ".2");
   }
}
--->8---

Here's the results of the code running on my system, showing that the
image was successfully renamed:

---8<---
/c/proj/rename-after-open$ d
OpenAndRename.cs  OpenAndRename.exe  test.jpg
/c/proj/rename-after-open$ ./OpenAndRename.exe test.jpg
/c/proj/rename-after-open$ d
OpenAndRename.cs  OpenAndRename.exe  test.jpg.2
/c/proj/rename-after-open$
--->8---

Now, if you want to rename or delete the image while it's still open,
then (IIRC) you'll have to load the image's data into memory first
(consider using MemoryStream), then load the image from the
MemoryStream, so that GDI+ doesn't have any way to keep the file on disk
open. Alternatively, consider using a FileStream with appropriate
sharing flags (FileShare enumeration in overloaded FileStream
constructor) and pass that to Image.FromStream.

-- Barry

Signature

http://barrkel.blogspot.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.