I�m working with images in C#. Loading from file system, resizing, then saving (overwriting.) This is all good up until the save over operation. I�ve tried a couple different ways of doing this but it either throws an exception or it destroys my image. Any ideas?
WORKING APPROACH (but not what I want to do)
//This works because there is no save-over operation attempted here.
System.Drawing.Bitmap b1 = new System.Drawing.Bitmap(FullFilePath);
b1.SetResolution(72,72);
b1.Save(FullFilePath+�.copy.jpg�,System.Drawing.Imaging.ImageFormat.Jpeg);
FIRST APPROACH
//access filesystem via Bitmap Class Methods
System.Drawing.Bitmap b1 = new System.Drawing.Bitmap(FullFilePath);
b1.SetResolution(72,72);
//Line 424 below, where program errors
b1.Save(FullFilePath,System.Drawing.Imaging.ImageFormat.Jpeg);
ERROR:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, Encoder Parameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at System.Drawing.Image.Save(String filename) �:line 424
SECOND APPROACH
//access filesystem via IO Stream
System.IO.FileStream UploadedFile = new System.IO.FileStream(FullFilePath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
System.Drawing.Bitmap OrigImage = new System.Drawing.Bitmap(UploadedFile);
Console.WriteLine("Width: " + OrigImage.Width);
OrigImage.SetResolution(72,72);
OrigImage.Save(UploadedFile,System.Drawing.Imaging.ImageFormat.Jpeg);
ERROR:
The code executes, but it mangles the image � You can see the top quarter of the image but the bottom is all blacked out, and my image viewer reports it is still 100 dpi
Has any one figured out how to do this?
-Rob108ducks