Changing size of Images

The following function changes the size of an image object and optionally changes the ratio of the image or crops the image. Perfectly for creating thumbnails.

/// <summary> 
/// Changes the size of an image. 
/// </summary> 
/// <param name="image">The image.</param> 
/// <param name="newWidth">The new width of the image.</param> 
/// <param name="newHeight">The new height of the image.</param> 
/// <param name="resizeOptions">Options that define how to resize the image.</param> 
/// <param name="qualityOptions">Options that define the rendering quality.</param> 
/// <returns>New Image</returns> 
public static Image ChangeSize(this Image image, int newWidth, int newHeight, ImageResizeOptions resizeOptions = ImageResizeOptions.KeepRatio, ImageQualityOptions qualityOptions = ImageQualityOptions.Default) 
{ 
  if (image == null) 
    throw new ArgumentNullException(nameof(image)); 
  
  Rectangle rect; 
  
  if (resizeOptions == ImageResizeOptions.Stretch) 
  { 
    rect = new Rectangle(0, 0, newWidth, newHeight); 
  } 
  else 
  { 
    if (image.Height <= newHeight && image.Width <= newWidth) 
      return new Bitmap(image); 
  
    double originalSize = resizeOptions == ImageResizeOptions.Crop ? Math.Min(image.Width, image.Height) : Math.Max(image.Width, image.Height); 
    double newSize = resizeOptions == ImageResizeOptions.Crop ? Math.Min(newWidth, newHeight) : Math.Max(newWidth, newHeight); 
    double ratio = newSize / originalSize; 
    double newImageHeight = (float)image.Height * ratio; 
    double newImageWidth = (float)image.Width * ratio; 
    int deltaX = 0; 
    int deltaY = 0; 
  
    if (resizeOptions == ImageResizeOptions.Crop) 
    { 
      if (newWidth != newImageWidth) 
        deltaX = Convert.ToInt32((newWidth - newImageWidth) / 2); 
      if (newHeight != newImageHeight) 
        deltaY = Convert.ToInt32((newHeight - newImageHeight) / 2); 
      rect = new Rectangle(deltaX, deltaY, Convert.ToInt32(newImageWidth), Convert.ToInt32(newImageHeight)); 
    } 
    else 
      rect = new Rectangle(0, 0, Convert.ToInt32(newImageWidth), Convert.ToInt32(newImageHeight)); 
  } 
  
  Bitmap b = new Bitmap(rect.Width, rect.Height); 
  using (Graphics g = Graphics.FromImage(b)) 
  { 
    switch (qualityOptions) 
    { 
      case ImageQualityOptions.Fast: 
        g.CompositingQuality = CompositingQuality.HighSpeed; 
        g.InterpolationMode = InterpolationMode.Low; 
        g.SmoothingMode = SmoothingMode.HighSpeed; 
        break; 
      case ImageQualityOptions.HighQuality: 
        g.CompositingQuality = CompositingQuality.HighQuality; 
        g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
        g.SmoothingMode = SmoothingMode.HighQuality; 
        break; 
    } 
    g.DrawImage(image, rect); 
  } 
  return b; 
} 
  
public enum ImageResizeOptions 
{ 
  KeepRatio, // Keeps the aspect ratio of the original image 
  Stretch,   // Does not observe the ration, but stretches the image to fill the target size. 
  Crop       // Keeps the ratio and resizes the smaller side of the image to fill the target and crops the larger side. 
} 
  
public enum ImageQualityOptions 
{ 
  Default, 
  Fast, 
  HighQuality 
}

The function can be called with different optional parameters. Some of them are optional.

newWidth and newHeight define the maximum size of the new image.

Set resizeOptions = Stretch to stretch the image instead of keeping the original image ratio.

Set resizeOptions = Crop to crop the image at the edge. This will help filling the image size of the new image if stretch is set to false.

the qualityOptions parameter defines the quality of the image rendering. A higher quality setting is slower.

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.