C# Bitmaps: Resize then saveover

What you may have to do is save the image to a new file name,
then use the shared methods of the System.IO.File class to
delete the original, then move the copy over the original. The
bitmap should be disposed of before this.

This code would come after the "working approach":
C#:
b1.Dispose(); // Clear up any resources

System.IO.File.Delete(FullFilePath); // Delete the original
System.IO.File.Move(FullFilePath + ".copy.jpg", FullFilePath) // Move the copy over
 
re: Image overwrite

Hi,

Ive just written this code for the same functionality. The catch is that it does not preserve metadata like exif headers.

public static void ResizeImageFile(string path, int width, int height)
{
Image in_img = Image.FromFile(path);
Image out_img = ResizeImage(in_img,width,height);
in_img.Dispose();
System.IO.File.Delete(path);
out_img.Save(path ,ImageFormat.Jpeg);
}

public static Image ResizeImage(Image img, int width, int height)
{
Bitmap b;
Image i;
Graphics g ;
b = new Bitmap(width, height); i = Image.FromHbitmap(b.GetHbitmap());
g = Graphics.FromImage(i);
g.DrawImage(img,0f,0f,width, height);
g.Dispose();
img.Dispose();
return i;
}

regards, Guido
 
Back
Top