views:

352

answers:

7

I'm trying to convert bmp files in a folder to jpg, then delete the old files. The code works fine, except it can't delete the bmp's.

DirectoryInfo di = new DirectoryInfo(args[0]);
FileInfo[] files = di.GetFiles("*.bmp");
foreach (FileInfo file in files)
{
    string newFile = file.FullName.Replace("bmp", "jpg");
    Bitmap bm = (Bitmap)Image.FromFile(file.FullName);
    bm.Save(newFile, ImageFormat.Jpeg);
}
for (int i = 0; i < files.Length; i++)
    files[i].Delete();

The files aren't being used by another program/process like the error indicates, so I'm assuming the problem is here. But to me the code seems fine, since I'm doing everything sequentially. This is all that there is to the program too, so the error can't be caused by code elsewhere.

A: 

The Bitmap object encapsulates a file handle. You must call Dispose on the bitmap object after you have called Save.

You're seeing a problem because those Bitmap objects haven't been garbage collected yet.

Matt Brunell
+4  A: 

Try wrap bitmaps with using:

using (Bitmap bm = (Bitmap)Image.FromFile(file.FullName))
{
    bm.Save(newFile, ImageFormat.Jpeg);
}

This will dispose bitmap objects just after they were saved.

Andrew Bezzub
To add to this, why not delete the bitmap during the same loop structure, rather than looping twice?
KP
yeah, i was doing that before but i got the error so i moved it to another loop to see if that was the problem.
zxcvbnm
This typically does NOT solve the problem with Image.FromFile which will hang onto files after Dispose() until garbage collection.
Hightechrider
A: 

After bm.Save, you should release your bitmap object. Try adding bm.Dispose(); after bm.Save(newFile, ImageFormat.Jpeg)

Aaron
A: 

From MSDN:

Always call Dispose before you release your last reference to the Image. Otherwise, the resources it is using will not be freed until the garbage collector calls the Image object's Finalize method.

I am not sure what you are doing with these images after then, but sometimes you will need also to set the image reference to null then call GC.Collect().

Sameh Serag
Explicitly calling GC.Collect() is in general a bad idea.
Hightechrider
A: 

The best way to solve the issue with Image.FromFile wherein it leaves file handles open is to use Image.FromStream instead.

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
   using (Image original = Image.FromStream(fs))
   {
     ...

Using an explicit Dispose(), a using() statement or setting the value to null doesn't solve the issue until a garbage collection happens (and forcing a garbage collection to happen is generally a bad idea).

Hightechrider
A: 

@Hightechrider -> nice answer. This solved a similar problem that I was having. Thanks!

Trickoder
A: 

public static Image LoadImage( string fileFullName ) {

Stream fileStream = File.OpenRead( fileFullName );
Image image       = Image.FromStream( fileStream );

// PropertyItems seem to get lost when fileStream is closed to quickly (?); perhaps
// this is the reason Microsoft didn't want to close it in the first place.
PropertyItem[] items = image.PropertyItems;

fileStream.Close();

foreach ( PropertyItem item in items )
{
    image.SetPropertyItem( item );
}

return image;

}

Erlend Robaye