Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the code I use. It supports rotation, and also sets the image resolution to the JPEG standards of 72dpi@24-bit color (by default GDI+ saves images at 96dpi@32-bit color). It also fixes the black/gray border problem that some people experience when resizing images.</p> <pre><code>/// &lt;summary&gt; /// Resizes and rotates an image, keeping the original aspect ratio. Does not dispose the original /// Image instance. /// &lt;/summary&gt; /// &lt;param name="image"&gt;Image instance&lt;/param&gt; /// &lt;param name="width"&gt;desired width&lt;/param&gt; /// &lt;param name="height"&gt;desired height&lt;/param&gt; /// &lt;param name="rotateFlipType"&gt;desired RotateFlipType&lt;/param&gt; /// &lt;returns&gt;new resized/rotated Image instance&lt;/returns&gt; public static Image Resize(Image image, int width, int height, RotateFlipType rotateFlipType) { // clone the Image instance, since we don't want to resize the original Image instance var rotatedImage = image.Clone() as Image; rotatedImage.RotateFlip(rotateFlipType); var newSize = CalculateResizedDimensions(rotatedImage, width, height); var resizedImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppArgb); resizedImage.SetResolution(72, 72); using (var graphics = Graphics.FromImage(resizedImage)) { // set parameters to create a high-quality thumbnail graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; // use an image attribute in order to remove the black/gray border around image after resize // (most obvious on white images), see this post for more information: // http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx using (var attribute = new ImageAttributes()) { attribute.SetWrapMode(WrapMode.TileFlipXY); // draws the resized image to the bitmap graphics.DrawImage(rotatedImage, new Rectangle(new Point(0, 0), newSize), 0, 0, rotatedImage.Width, rotatedImage.Height, GraphicsUnit.Pixel, attribute); } } return resizedImage; } /// &lt;summary&gt; /// Calculates resized dimensions for an image, preserving the aspect ratio. /// &lt;/summary&gt; /// &lt;param name="image"&gt;Image instance&lt;/param&gt; /// &lt;param name="desiredWidth"&gt;desired width&lt;/param&gt; /// &lt;param name="desiredHeight"&gt;desired height&lt;/param&gt; /// &lt;returns&gt;Size instance with the resized dimensions&lt;/returns&gt; private static Size CalculateResizedDimensions(Image image, int desiredWidth, int desiredHeight) { var widthScale = (double)desiredWidth / image.Width; var heightScale = (double)desiredHeight / image.Height; // scale to whichever ratio is smaller, this works for both scaling up and scaling down var scale = widthScale &lt; heightScale ? widthScale : heightScale; return new Size { Width = (int) (scale * image.Width), Height = (int) (scale * image.Height) }; } </code></pre>
    singulars
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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