开发者

Why am I getting "the process cannot access the file * because it is being used by another process" with this code?

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开发者_JAVA技巧 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.


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.


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).


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;
}


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.


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


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜