Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This above answer is great but i did some fine-tuning and implemented the "keep ratio" of the image so that we don't end up with stretched images.</p> <pre><code> using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web.Helpers; public static class ResizePng { private static IDictionary&lt;string, ImageFormat&gt; _transparencyFormats = new Dictionary&lt;string, ImageFormat&gt;(StringComparer.OrdinalIgnoreCase) { { "png", ImageFormat.Png }, { "gif", ImageFormat.Gif } }; public static WebImage ResizePreserveTransparency(this WebImage image, int width, int height) { ImageFormat format = null; if (!_transparencyFormats.TryGetValue(image.ImageFormat, out format)) { return image.Resize(width, height); } //keep ratio ************************************* double ratio = (double)image.Width / image.Height; double desiredRatio = (double)width / height; if (ratio &gt; desiredRatio) { height = Convert.ToInt32(width / ratio); } if (ratio &lt; desiredRatio) { width = Convert.ToInt32(height * ratio); } //************************************************ using (Image resizedImage = new Bitmap(width, height)) { using (Bitmap source = new Bitmap(new MemoryStream(image.GetBytes()))) { using (Graphics g = Graphics.FromImage(resizedImage)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(source, 0, 0, width, height); } } using (MemoryStream ms = new MemoryStream()) { resizedImage.Save(ms, format); return new WebImage(ms.ToArray()); } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload