Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a bitmap after setting interpolation with graphics class

This code resizes an image and saves it to disk.

using (var medBitmap = new Bitmap(fullSizeImage, newImageW, newImageH))
{
     medBitmap.Save(HttpContext.Current.Server.MapPath("~/Media/Items/Images/" + itemId + ".jpg"),
                    ImageFormat.Jpeg);
}

But if I want to use the graphics class to set the interpolation, how do I save it? The graphics class has a save method, but it doesn't take any parameters. How do I save it to disk like the bitmap? Heres a modified code snippet:

using (var medBitmap = new Bitmap(fullSizeImage, newImageW, newImageH))
{
     var g = Graphics.FromImage(medBitmap);
     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
     //What do I do now?
     medBitmap.Save(HttpContext.Current.Server.MapPath("~/Media/Items/Images/" + itemId + ".jpg"),
                    ImageFormat.Jpeg);
}

I just need to set the interpolation and then save it to disk.

like image 295
The Muffin Man Avatar asked Oct 11 '22 22:10

The Muffin Man


2 Answers

Call DrawImage on the Graphics object to update the bitmap:

using (var medBitmap = new Bitmap(fullSizeImage, newImageW, newImageH))
{
  using (var g = Graphics.FromImage(medBitmap))
  {
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(medBitmap, 0, 0);
  }
  medBitmap.Save(HttpContext.Current.Server.MapPath("~/Media/Items/Images/" + itemId + ".jpg"), ImageFormat.Jpeg);
}
like image 146
hemp Avatar answered Oct 14 '22 03:10

hemp


Create a new Bitmap with the size you want and set the interpolationMode. Then use Graphics.DrawImage to draw the full sized image into the new bitmap.

like image 22
Richard Schneider Avatar answered Oct 14 '22 02:10

Richard Schneider