views:

1074

answers:

6

I am doing a lot of image processing in GDI+ in .NET in an ASP.NET application.

I frequently find that Image.FromFile() is keeping a file handle open.

Why is this? What is the best way to open an image without the file handle being retained.

  • NB: I'm not doing anything stupid like keeping the Image object lying around - and even if I was I woudlnt expect the file handle to be kept active
A: 

I would have to point my finger at the Garbage Collector. Leaving it around is not really the issue if you are at the mercy of Garbage Collection.

This guy had a similar complaint... and he found a workaround of using a FileStream object rather than loading directly from the file.

public static Image LoadImageFromFile(string fileName)
{
    Image theImage = null;

    fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    {
        byte[] img;
        img = new byte[fileStream.Length];
        fileStream.Read(img, 0, img.Length);
        fileStream.Close();
        theImage = Image.FromStream(new MemoryStream(img));
        img = null;
    }

...

It seems like a complete hack...

ojblass
what the frick is the downvote for?
ojblass
+1  A: 

I have had the same problem and resorted to reading the file using

return Image.FromStream(new MemoryStream(File.ReadAllBytes(fileName)));

Steven
Not sure why my answer was voted down. It will read without keeping the handle open.
Steven
A: 

Make sure you are Disposing properly.

using (Image.FromFile("path")) {}

The using expression is shorthand for

IDisposable obj;
try { }
finally 
{
    obj.Dispose();
}

@Rex in the case of Image.Dispose it calls GdipDisposeImage extern / native Win32 call in it's Dispose().

IDisposable is used as a mechanism to free unmanaged resources (Which file handles are)

Chad Grant
In the case of Image, what all does Dispose actually do? Does it release file system handles, unmanaged memory, etc.?
Rex M
+3  A: 

Image.FromFile keeps the file handle open until the image is disposed. From the MSDN:

"The file remains locked until the Image is disposed."

Use Image.FromStream, and you won't have the problem.

using(var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
    return Image.FromStream(fs);
}

Edit: (a year and a bit later)

The above code is dangerous as it is unpredictable, at some point in time (after closing the filestream) you may get the dreaded "A generic error occurred in GDI+". I would amend it to:

Bitmap tmpImage, returnImage;

using(var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
    tmpImage = Image.FromStream(fs);
    returnImage = new Bitmap(tmpImage);
    tmpImage.Dispose();
}

return returnImage;
Kris Erickson
I think the FileStream also needs to be cleaned up.
Rex M
Absolutely needs to be cleaned up. Thanks for fixing up the post.
Kris Erickson
+4  A: 

I went through the same journey as a few other posters on this thread. Things I noted:

  1. Using Image.FromFile does seem unpredictable on when it releases the file handle. Calling the Image.Dispose() did not release the file handle in all cases.

  2. Using a FileStream and the Image.FromStream method works, and releases the handle on the file if you call Dispose() on the FileStream or wrap the whole thing in a Using {} statement as recommended by Kris. However if you then attempt to save the Image object to a stream, the Image.Save method throws an exception "A generic error occured in GDI+". Presumably something in the Save method wants to know about the originating file.

  3. Steven's approach worked for me. I was able to delete the originating file with the Image object in memory. I was also able to save the Image to both a stream and a file (I needed to do both of these things). I was also able to save to a file with the same name as the originating file, something that is documented as not possible if you use the Image.FromFile method (I find this weird since surely this is the most likely use case, but hey.)

So to summarise, open your Image like this:

Image img = Image.FromStream(newMemoryStream(File.ReadAllBytes(path)));

You are then free to manipulate it (and the originating file) as you see fit.

Jamie
clever :-) thanks
Simon_Weaver
The webapp I have in production that uses this technique appears to leak memory. I have it running on 6 webservers and after a few days the relevant ASP.NET process is using > 2gb of memory. I have hacked in a fix by setting the relevant application pool to recycle after it hits 500mb of memory usage or every day at 2am. I don't have time to investigate this at present, but when I do I will post here with my solution.
Jamie
@Jamie - Did you find/fix your problem?
Nifle
@Nifle - sadly not! I seem to remember reading in the MSDN that use of the GDI+ classes is not supported in services / web applications and I can see why. The memory usage is incredibly high. Ironically, part of my problem is that I've done things 'properly' with a data model that encapsulates all of the logic - this makes it a little more difficult to tear this stuff down after it's been used.Our solution is to use a seperate application server for this functionality - the system was SOA anyway so it's not too much pain for us.
Jamie
With regards to your number 2 point, it could be because you're using a non-seekable stream which is why the memory stream works. For saving you can try (pseudocode): a using(filestream) { using(new memorystream) { i.Save(memstream); memstream.WriteTo(fs); } }
Graphain
A: 

As mentioned above the Microsoft work around causes a GDI+ error after several images have been loaded. The solution for me as mentioned above by steven is

picTemp.Image = Image.FromStream(New System.IO.MemoryStream(My.Computer.FileSystem.ReadAllBytes(strFl)))

(Solution is for VB)