Code.GeekInterview.com
 
Code Samples C#
 

Resize Image


Code ResourceAuthor: Abhishek1950  

Difficulty Level: Intermediate

Published: 13th Feb 2010   Read: 3827 times  

Filed in: C#
Add Comment


 

 

Sponsored Links


 

 
This is a snippet for resizing an image based standard image size. Change the StandartH constant value according to your need.
 

Sample Code
  1.  
  2. /// <summary>
  3.         /// Method for resizing an image
  4.         /// </summary>
  5.         /// <param name="img">The image to resize</param>
  6.         /// <returns></returns>
  7.         public Image Resize(Image img)
  8.         {
  9.             const int StandartH = 1400;            
  10.  
  11.             //get the height and width of the image
  12.             int originalW = (img.Width * (int)img.VerticalResolution)/72;
  13.             int originalH = (img.Height * (int)img.HorizontalResolution)/72;
  14.  
  15.             //No need to change the size of this image
  16.             if (originalW <= StandartH) return img;
  17.  
  18.             int percentage =100 - ((StandartH * 100) / originalW);
  19.  
  20.             //get the new size based on the percentage change
  21.             int resizedW = originalW - ((originalW * percentage) / 100);
  22.             int resizedH = originalH -  ((originalH * percentage) / 100);
  23.  
  24.             //create a new Bitmap the size of the new image
  25.             Bitmap bmp = new Bitmap(resizedW, resizedH);
  26.             bmp.SetResolution(72, 72);
  27.             //create a new graphic from the Bitmap
  28.             Graphics graphic = Graphics.FromImage((Image)bmp);
  29.             graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
  30.             //draw the newly resized image
  31.             graphic.DrawImage(img, 0, 0, resizedW, resizedH);
  32.             //dispose and free up the resources
  33.             graphic.Dispose();
  34.             //return the image
  35.             return (Image)bmp;
  36.         }
  37.  
Copyright GeekInterview.com


Next Article: Storing Images and PDF Files In Oracle


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. V.S.SANDILYA5226
2. venki_madesh4759
3. Abhishek19503828

Active Coders

# Coder NameCodes
1. venki_madesh1
2. V.S.SANDILYA1
3. Abhishek19501

Refined Tags