Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize an Image C#

Tags:

c#

image

resize

As Size, Width and Height are Get() properties of System.Drawing.Image;
How can I resize an Image object at run-time in C#?

Right now, I am just creating a new Image using:

// objImage is the original Image Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171)); 
like image 650
inutan Avatar asked Dec 17 '09 14:12

inutan


People also ask

How do I resize an image in code?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


2 Answers

This will perform a high quality resize:

/// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="image">The image to resize.</param> /// <param name="width">The width to resize to.</param> /// <param name="height">The height to resize to.</param> /// <returns>The resized image.</returns> public static Bitmap ResizeImage(Image image, int width, int height) {     var destRect = new Rectangle(0, 0, width, height);     var destImage = new Bitmap(width, height);      destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);      using (var graphics = Graphics.FromImage(destImage))     {         graphics.CompositingMode = CompositingMode.SourceCopy;         graphics.CompositingQuality = CompositingQuality.HighQuality;         graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;         graphics.SmoothingMode = SmoothingMode.HighQuality;         graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;          using (var wrapMode = new ImageAttributes())         {             wrapMode.SetWrapMode(WrapMode.TileFlipXY);             graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);         }     }      return destImage; } 
  • wrapMode.SetWrapMode(WrapMode.TileFlipXY) prevents ghosting around the image borders -- naïve resizing will sample transparent pixels beyond the image boundaries, but by mirroring the image we can get a better sample (this setting is very noticeable)
  • destImage.SetResolution maintains DPI regardless of physical size -- may increase quality when reducing image dimensions or when printing
  • Compositing controls how pixels are blended with the background -- might not be needed since we're only drawing one thing.
    • graphics.CompositingMode determines whether pixels from a source image overwrite or are combined with background pixels. SourceCopy specifies that when a color is rendered, it overwrites the background color.
    • graphics.CompositingQuality determines the rendering quality level of layered images.
  • graphics.InterpolationMode determines how intermediate values between two endpoints are calculated
  • graphics.SmoothingMode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) -- probably only works on vectors
  • graphics.PixelOffsetMode affects rendering quality when drawing the new image

Maintaining aspect ratio is left as an exercise for the reader (actually, I just don't think it's this function's job to do that for you).

Also, this is a good article describing some of the pitfalls with image resizing. The above function will cover most of them, but you still have to worry about saving.

like image 103
mpen Avatar answered Sep 21 '22 11:09

mpen


Not sure what is so difficult about this, do what you were doing, use the overloaded Bitmap constructor to create a re-sized image, the only thing you were missing was a cast back to the Image data type:

public static Image resizeImage(Image imgToResize, Size size) {     return (Image)(new Bitmap(imgToResize, size)); }  yourImage = resizeImage(yourImage, new Size(50,50)); 
like image 27
Matt Avatar answered Sep 23 '22 11:09

Matt