When using the code below I sometimes get the error "A generic error occurred in GDI+. at System.Drawing.Image.Save". I've started to believe that it's because I delete the source image before saving the newly created image. Is that the case? When is it really safe to delete a source Image file then?
The sub class:
thumbnailImage = GetThumb();
thumbnailImage.Save(fullDestinationFilename);
The base class:
public virtual Image GetThumb() {
using (var image = Image.FromFile(sourceFile)) {
thumbImage = Crop(image, BrowserWidth, BrowserHeight));
}
File.Delete(sourceFile);
return thumbImage;
}
private static Image Crop(Image image, int width, int height) {
var croppedImage = new Bitmap(width, height);
using (var g = Graphics.FromImage(croppedImage)){
g.DrawImage(image, 0, 0, image.Width, image.Height);
return croppedImage;
}
}