简体   繁体   中英

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.

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM