Resizes an image : Image « 2D Graphics « C# / C Sharp






Resizes an image

       
using System.Drawing;
using System.IO;

namespace WebPages.Bll
{
    public class GraphicUtilities
    {
        const int DEFAULT_THUMBNAIL_PIXEL_WIDTH = 64;
        const int DEFAULT_THUMBNAIL_PIXEL_HEIGHT = 64;

        /// <summary>
        /// Creates a thumbnail image using the default thumbnail size. Image aspect ratio is respected
        /// </summary>
        /// <param name="sourceImage">Source image</param>
        /// <returns>The thumbnail image</returns>
        public Stream CreateThumbnail(Stream sourceImage)
        {
            return ResizeImage(sourceImage, true, DEFAULT_THUMBNAIL_PIXEL_WIDTH, DEFAULT_THUMBNAIL_PIXEL_HEIGHT);
        }

        /// <summary>
        /// Resizes an image
        /// </summary>
        /// <param name="sourceImage">Source image to process</param>
        /// <param name="keepAspectRatio">If true then image aspect ratio is respected, if false the image may be stretched to fit the given width and height</param>
        /// <param name="maxPixelWidth">New image width</param>
        /// <param name="maxPixelHeight">New image height</param>
        /// <returns>The resized image</returns>
        public Stream ResizeImage(Stream sourceImage, bool keepAspectRatio, int maxPixelWidth, int maxPixelHeight)
        {
            var orig = new Bitmap(sourceImage);
            int width;
            int height;

            if (keepAspectRatio)
            {
                if (orig.Width > orig.Height)
                {
                    width = maxPixelWidth;
                    height = maxPixelWidth * orig.Height / orig.Width;
                }
                else
                {
                    height = maxPixelHeight;
                    width = maxPixelHeight * orig.Width / orig.Height;
                }
            }
            else
            {
                width = maxPixelWidth;
                height = maxPixelHeight;
            }

            var thumbnailImage = new Bitmap(width, height);

            using (var graphic = Graphics.FromImage(thumbnailImage))
            {
                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphic.DrawImage(orig, 0, 0, width, height);
                var stream = new MemoryStream();
                thumbnailImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                stream.Seek(0, SeekOrigin.Begin);
                return stream;
            }
        }
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Load Image from an image file with Exception handler
2.Image.FromStream: load image from stream
3.Shrink ImageShrink Image
4.Shear ImageShear Image
5.Clone ImageClone Image
6.Get Image Resolution and Image size and Display sizeGet Image Resolution and Image size and Display size
7.Draw image based on its sizeDraw image based on its size
8.Set image resolution and paint itSet image resolution and paint it
9.Load image and displayLoad image and display
10.Fill Ellipse with image based Texture BrushFill Ellipse with image based Texture Brush
11.Image Save
12.Image Open
13.Thumbnail Image
14.Draw on Pixel-Size Image
15.Draw text on an Image
16.Partial Image Rotate
17.Partial Image Stretch
18.Draw Partial Image
19.Image At Points (Draw part of the image)
20.Image Reflection
21.Image Scale Isotropic
22.Image Scale To Rectangle
23.Center Pixel-Size Image by using Image.Width and Image.Height
24.Center an Image (VerticalResolution, HorizontalResolution)
25.Load image from a URL(Web) and draw it
26.Scribble with Bitmap
27.Image Flipping and Rotating
28.Image.GetThumbnailImage
29.Image Zoom
30.Create Thumbnail
31.Image Resize
32.Get Bitmap Source
33.Get Bytes From BitmapSource
34.Load Image
35.Create Icon from Image
36.Get Gravatar Image
37.Changing image opacity, Resize image, crop image and gray scale image
38.Resize Image
39.Resize image to fit
40.Image Filter
41.Save Window/Canvas to Image
42.Image Cache
43.Saves the image to a bytes array. Used to store an image into DB.
44.Get the "energy" of a pixel