File Not Released After Thumbnail Created

J

Jeff S

In an ASP.NET Web application, I enable users to upload a file for use in a
photo album. After uploading the file, some code executes (see it below)
that creates a thumbnail image from the original. The problem is that the
original is not released; when other code tries to delete it (using
File.Delete), an Exception is thrown: "The process cannot access the file...
because it is being used by another process". I get the same error when
trying to delete the same file via Windows Explorer. If I upload multiple
files (each of which gets a thumbnail generated automatically by the code
below), then only the *last* file uploaded cannot be deleted. Note: the
thumbnail file can be deleted - the problem is with the original image which
remains somehow "in use".

What's keeping the originally uploaded file "in use"? What can I do to
release control of the file?

try {
string strFilename = pathToFullSizeImage;
int dotPlace = strFilename.LastIndexOf(".");
string fileType = strFilename.Substring((dotPlace + 1),
(strFilename.Length - (dotPlace + 1))).ToUpper();

int iWidth = 90;
int iHeight = 90;

System.Drawing.Image objThumbnail =
System.Drawing.Image.FromFile(strFilename).GetThumbnailImage(iWidth,
iHeight, null, IntPtr.Zero);

// Set the ContentType appropriately
if (fileType == "JPG" || fileType == "JPEG") {
objThumbnail.Save(pathToThumbnailImage, ImageFormat.Jpeg);
}
else {
objThumbnail.Save(pathToThumbnailImage, ImageFormat.Gif);
}
objThumbnail.Dispose();
}

Thanks!
 
J

Jeff S

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top