The process cannot access the file x because it is being used by another process.
I'm trying to write an automatic image cropper when a user uploads an image to the media section. I'm using this function in the Media.AfterSave and Media.AfterNew events. When the code fires I'm getting the error mentioned in the title.
Here is the code I'm using.
private void CropImage(Media media, int width)
{
//Check where dealing with an image
//if(media.ContentType.Alias.Equals("image"))
{
string fileUrl = HttpContext.Current.Server.MapPath(
media.getProperty("umbracoFile").Value.ToString());
//Make the cropped image
System.Drawing.Bitmap img = Image.ResizeByWidth(
new System.Drawing.Bitmap(fileUrl), 500);
//Delete original image
File.Delete(fileUrl);
//Save cropped image
img.Save(fileUrl);
}
}
}
The process cannot access the file x because it is being used by another process.
I'm trying to write an automatic image cropper when a user uploads an image to the media section. I'm using this function in the Media.AfterSave and Media.AfterNew events. When the code fires I'm getting the error mentioned in the title.
Here is the code I'm using.
Forgot to add what line triggers the error :) (Can't edit my post....)
The errors comes with the File.Delete(fileUrl) line.
I think that's because of the line above File.Delete(): new System.Drawing.Bitmap(fileUrl)
What if you try this:
private void CropImage(Media media, int width){
//Check where dealing with an image
//if(media.ContentType.Alias.Equals("image")) {
string fileUrl = HttpContext.Current.Server.MapPath(media.getProperty("umbracoFile").Value.ToString());
//Make the cropped image
System.Drawing.Bitmap image = new System.Drawing.Bitmap(fileUrl);
System.Drawing.Bitmap croppedImage = Image.ResizeByWidth(new System.Drawing.Bitmap(fileUrl), 500);-
image.Dispose();
//Delete original image
File.Delete(fileUrl);
//Save cropped image
croppedImage.Save(fileUrl);
croppedImage.Dispose();
//}
}
@kipusoep, thank you, that worked!
Awesome! :-)
I just saw there were 2 minor errors in my script:
is working on a reply...