Monday, 8 September 2014

C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height

This function allows to resize the image. It prevents skewed images and  also vertically long images caused by trying to maintain the aspect ratio on images who's height is larger than their width

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
 System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

 // Prevent using images internal thumbnail
 FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
 FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

 if (OnlyResizeIfWider)
 {
  if (FullsizeImage.Width <= NewWidth)
  {
   NewWidth = FullsizeImage.Width;
  }
 }

 int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
 if (NewHeight > MaxHeight)
 {
  // Resize with height instead
  NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
  NewHeight = MaxHeight;
 }

 System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

 // Clear handle to original file so that we can overwrite it if necessary
 FullsizeImage.Dispose();

 // Save resized picture
 NewImage.Save(NewFile);
}

/Adnan



Note: This post is from my old blog dated Thursday, February 17, 2011, that unfortunately no longer exist  

About the Author

Adnan Zameer, Lead Developer at Optimizley UK, is a certified Microsoft professional, specializing in web app architecture. His expertise includes Optimizley CMS and Azure, showcasing proficiency in crafting robust and efficient solutions.

0 comments :

Post a Comment