/// <summary> /// Resizes the image. /// </summary> /// <param name="sourceFile">The source file.</param> /// <param name="targetFile">The target file.</param> /// <param name="maxWidth">Width of the max.</param> /// <param name="maxHeight">Height of the max.</param> /// <param name="preserverAspectRatio">if set to <c>true</c> [preserver aspect ratio].</param> /// <param name="quality">The quality.</param> /// <returns></returns> public static bool ResizeImage(string sourceFile, string targetFile, int maxWidth, int maxHeight, bool preserverAspectRatio, int quality) { System.Drawing.Image sourceImage; try { sourceImage = System.Drawing.Image.FromFile(sourceFile); } catch (OutOfMemoryException) { // This is not a valid image. Do nothing. return(false); } // If 0 is passed in any of the max sizes it means that that size must be ignored, // so the original image size is used. maxWidth = maxWidth == 0 ? sourceImage.Width : maxWidth; maxHeight = maxHeight == 0 ? sourceImage.Height : maxHeight; if (sourceImage.Width <= maxWidth && sourceImage.Height <= maxHeight) { sourceImage.Dispose(); if (sourceFile != targetFile) { System.IO.File.Copy(sourceFile, targetFile); } return(true); } Size oSize; if (preserverAspectRatio) { // Gets the best size for aspect ratio resampling oSize = GetAspectRatioSize(maxWidth, maxHeight, sourceImage.Width, sourceImage.Height); } else { oSize = new Size(maxWidth, maxHeight); } System.Drawing.Image oResampled = null; Graphics oGraphics = null; try { oResampled = new Bitmap(oSize.Width, oSize.Height, PixelFormat.Format32bppArgb); // Creates a Graphics for the oResampled image oGraphics = Graphics.FromImage(oResampled); // The Rectangle that holds the Resampled image size Rectangle oRectangle; // High quality resizing if (quality > 80) { oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // If HighQualityBicubic is used, bigger Rectangle is required to remove the white border oRectangle = new Rectangle(-1, -1, oSize.Width + 1, oSize.Height + 1); } else { oRectangle = new Rectangle(0, 0, oSize.Width, oSize.Height); } // Place a white background (for transparent images). //oGraphics.FillRectangle(new SolidBrush(Color.White), oRectangle); // Draws over the oResampled image the resampled Image oGraphics.DrawImage(sourceImage, oRectangle); sourceImage.Dispose(); String extension = System.IO.Path.GetExtension(targetFile).ToLower(); if (extension == ".jpg" || extension == ".jpeg") { ImageCodecInfo oCodec = GetJpgCodec(); if (oCodec != null) { EncoderParameters aCodecParams = new EncoderParameters(1); aCodecParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); oResampled.Save(targetFile, oCodec, aCodecParams); } else { oResampled.Save(targetFile); } } else { switch (extension) { case ".gif": try { // Use a proper palette OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); using (Bitmap quantized = quantizer.Quantize(oResampled)) { quantized.Save(targetFile, System.Drawing.Imaging.ImageFormat.Gif); } } catch (System.Security.SecurityException) { // The calls to Marshal might fail in Medium trust, save the image using the default palette oResampled.Save(targetFile, System.Drawing.Imaging.ImageFormat.Png); } break; case ".png": oResampled.Save(targetFile, System.Drawing.Imaging.ImageFormat.Png); break; case ".bmp": oResampled.Save(targetFile, System.Drawing.Imaging.ImageFormat.Bmp); break; } } } finally { if (oGraphics != null) { oGraphics.Dispose(); } if (oResampled != null) { oResampled.Dispose(); } } return(true); }
public static bool ResizeImage(Stream sourceStream, Stream targetStream, ImageFormat imageFormat, int maxWidth, int maxHeight, bool preserverAspectRatio, int quality) { System.Drawing.Image sourceImage; try { sourceImage = System.Drawing.Image.FromStream(sourceStream); sourceStream.Position = 0; } catch (OutOfMemoryException) { // This is not a valid image. Do nothing. return false; } // If 0 is passed in any of the max sizes it means that that size must be ignored, // so the original image size is used. maxWidth = maxWidth == 0 ? sourceImage.Width : maxWidth; maxHeight = maxHeight == 0 ? sourceImage.Height : maxHeight; if (sourceImage.Width <= maxWidth && sourceImage.Height <= maxHeight) { sourceImage.Dispose(); sourceStream.CopyTo(targetStream); return true; } Size oSize; if (preserverAspectRatio) { // Gets the best size for aspect ratio resampling oSize = GetAspectRatioSize(maxWidth, maxHeight, sourceImage.Width, sourceImage.Height); } else oSize = new Size(maxWidth, maxHeight); System.Drawing.Image oResampled = null; Graphics oGraphics = null; try { oResampled = new Bitmap(oSize.Width, oSize.Height, PixelFormat.Format32bppArgb); // Creates a Graphics for the oResampled image oGraphics = Graphics.FromImage(oResampled); // The Rectangle that holds the Resampled image size Rectangle oRectangle; // High quality resizing if (quality > 80) { oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // If HighQualityBicubic is used, bigger Rectangle is required to remove the white border oRectangle = new Rectangle(-1, -1, oSize.Width + 1, oSize.Height + 1); } else oRectangle = new Rectangle(0, 0, oSize.Width, oSize.Height); // Place a white background (for transparent images). //oGraphics.FillRectangle(new SolidBrush(Color.White), oRectangle); // Draws over the oResampled image the resampled Image oGraphics.DrawImage(sourceImage, oRectangle); sourceImage.Dispose(); if (imageFormat == ImageFormat.Jpeg) { ImageCodecInfo oCodec = GetJpgCodec(); if (oCodec != null) { EncoderParameters aCodecParams = new EncoderParameters(1); aCodecParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); oResampled.Save(targetStream, oCodec, aCodecParams); } else oResampled.Save(targetStream, imageFormat); } else if (imageFormat == ImageFormat.Gif) { try { // Use a proper palette OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); using (Bitmap quantized = quantizer.Quantize(oResampled)) { quantized.Save(targetStream, System.Drawing.Imaging.ImageFormat.Gif); } } catch (System.Security.SecurityException) { // The calls to Marshal might fail in Medium trust, save the image using the default palette oResampled.Save(targetStream, System.Drawing.Imaging.ImageFormat.Png); } } else { oResampled.Save(targetStream, System.Drawing.Imaging.ImageFormat.Png); } } finally { if (oGraphics != null) oGraphics.Dispose(); if (oResampled != null) oResampled.Dispose(); } return true; }