I've got users uploading a file, which I then want resize, make a thumbnail
out of, then delete. The image and thumbnail are working fine, but when I
try to delete the image, I get an error: "The process cannot access the file
"c:\inetpub\wwwroot\pictures\1\21_orig.jpg" because it is being used by
another process. "
I'm using the Image.Dispose() method before trying to delete it; how do I
free up this image?
Thanks in advance,
Duncan
---/ snip /---
private void SaveImage_Click(object sender, System.EventArgs e)
{
// Create data access layer
dal Dal = new dal();
// Create global params
config Config = new config();
string errorMessage = "";
string fileName = Image.PostedFile.FileName;
int extensionPos = 0;
string extension = "";
// Error checking
if (ImageTitle.Text == "")
{
errorMessage += "<li>You have not entered a title</li>";
}
if (fileName == "" && _CurrentImage_ID == 0)
{
errorMessage += "<li>You have not entered the file to upload</li>";
}
// We have a file - make sure it's of the right format!
if (fileName != "")
{
extensionPos = fileName.LastIndexOf(".") + 1;
if (extensionPos > 0)
{
extension = fileName.Substring(extensionPos, fileName.Length -
extensionPos);
}
if (extension != "gif" && extension != "jpg" && extension != "jpeg" &&
extension != "png")
{
errorMessage += "<li>Invalid file uploaded - Your image must be either GIF,
JPG, JPEG or PNG</li>";
}
}
// Do save or error
if (errorMessage == "")
{
if (fileName == "")
{
// It's an edit without a new image
// Get the existing image extension
extension = CurrentImage.Text;
// Write it!
Dal.PhotoAddEdit(_CurrentImage_ID, _CurrentPhotoAlbum_ID, ImageTitle.Text,
extension);
}
else
{
// It's an add, or an edit with a new image
int iNew_ID = Dal.PhotoAddEdit(_CurrentImage_ID, _CurrentPhotoAlbum_ID,
ImageTitle.Text, extension);
string newFileName = Config.ImageUploadDirectory +
_CurrentPhotoAlbum_ID.ToString() + "\\" + iNew_ID + "_orig." + extension;
string finalName = Config.ImageUploadDirectory +
_CurrentPhotoAlbum_ID.ToString() + "\\regular\\" + iNew_ID + "_orig." +
extension;
// Write the file to disk
Image.PostedFile.SaveAs(newFileName);
// Get the image and size
System.Drawing.Image image = System.Drawing.Image.FromFile(newFileName);
double height = image.Height;
double width = image.Width;
// Get the format
System.Drawing.Imaging.ImageFormat currentFormat;
if (extension == "gif")
{
currentFormat = System.Drawing.Imaging.ImageFormat.Gif;
}
else if (extension == "png")
{
currentFormat = System.Drawing.Imaging.ImageFormat.Png;
}
else
{
currentFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
// Get the new size
double newHeight = 0;
double newWidth = 0;
double scale = 0;
// Work out which way we're scaling this for the thumbnail
if (height > width)
{
scale = 100 / height;
newHeight = 100;
newWidth = width * scale;
}
else
{
scale = 100 / width;
newHeight = height * scale;
newWidth = 100;
}
// Create the thumbnail
System.Drawing.Image thumbnailImage =
image.GetThumbnailImage(System.Convert.ToInt32(new Width),
System.Convert.ToInt32(newHeight), new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),
IntPtr.Zero);
// Write it!
thumbnailImage.Save(Config.ImageUploadDirectory +
_CurrentPhotoAlbum_ID.ToString() + "\\thumb\\" + iNew_ID + "." + extension,
currentFormat);
thumbnailImage.Dispose();
// If the main image > 800 x 600, we need to resize this, too
if (width > 800 || height > 600)
{
if (height > width)
{
scale = 600 / height;
newHeight = 600;
newWidth = width * scale;
}
else
{
scale = 800 / width;
newHeight = height * scale;
newWidth = 800;
}
System.Drawing.Image resizedImage =
image.GetThumbnailImage(System.Convert.ToInt32(new Width),
System.Convert.ToInt32(newHeight), new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),
IntPtr.Zero);
// Write the fucker!
resizedImage.Save(finalName, currentFormat);
resizedImage.Dispose();
}
else
{
File.Copy(newFileName, finalName);
} // 800 x 600
// Get rid of the image
Image.Dispose();
// Get rid of the "original"
File.Delete(newFileName);
}
// All saved - redirect!
Response.Redirect("managephotos.aspx?photoAlbum_ID =" +
_CurrentPhotoAlbum_ID);
}
else
{
ErrorMessage.Text = "The following errors have occurred: <ul>" +
errorMessage + "</ul>Click <a
href=\"javascript:window.history.go(-1);\">here</a> to go back and correct
them.";
ShowPanel(ErrorPanel);
}
}
public bool ThumbnailCallback()
{
return false;
}