cagin
cagin

Reputation: 5920

Image file is being used by another process

I want to delete my image from folder. But it throws "it is being used by another process."

Here is my code:

fuProductImage.SaveAs(fileFolderPathTemp + fuProductImage.FileName);

 Bitmap orgImage = new Bitmap(fileFolderPathTemp + fuProductImage.FileName);
 ResizeAndSaveImages(orgImage, fileFolderPathLarge, fuProductImage.FileName, 66, 66);

 File.Delete(fileFolderPathTemp + fuProductImage.FileName);

What should I do to delete this file?

Upvotes: 4

Views: 1174

Answers (1)

Uwe Keim
Uwe Keim

Reputation: 40736

As of request, my suggestion is to put line two and three into a using block like:

fuProductImage.SaveAs(fileFolderPathTemp + fuProductImage.FileName);

using ( Bitmap orgImage = 
        new Bitmap(fileFolderPathTemp + fuProductImage.FileName) )
{
    ResizeAndSaveImages(
        orgImage, 
        fileFolderPathLarge, 
        fuProductImage.FileName, 
        66, 
        66);
}

File.Delete(fileFolderPathTemp + fuProductImage.FileName);

The reason why this should help is that the using ensures that the orgImage is correctly being disposed, thus freeing memory and releasing file handles before you call the File.Delete() function.

Upvotes: 6

Related Questions