c# save overwrite image error -
im trying save file using im getting gdi+ error because im trying save on source file im using. solutions?
example:
private void form1_load(object sender, eventargs e) { bitmap sourceimage = new bitmap("images/sourceimage.jpg"); sourceimage = cropbitmap(sourceimage, 0, 0, sourceimage.width, 50); sourceimage.save("images/sourceimage.jpg", imageformat.jpeg); } public bitmap cropbitmap(bitmap bitmap, int cropx, int cropy, int cropwidth, int cropheight) { rectangle rect = new rectangle(cropx, cropy, cropwidth, cropheight); bitmap cropped = bitmap.clone(rect, bitmap.pixelformat); return cropped; }
see the documentation constructor. section reads:
the file remains locked until bitmap disposed.
you must dispose sourceimage
before saving new one. so, use different variables:
var sourceimage = new bitmap("images/sourceimage.jpg"); var croppedimage = cropbitmap(sourceimage, 0, 0, sourceimage.width, 50); sourceimage.dispose(); croppedimage.save("images/sourceimage.jpg", imageformat.jpeg); croppedimage.dispose();
Comments
Post a Comment