C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C# Source Code



Scale an image to fit PictureBox dimension

This is a snippet to scale an image to fit within a PictureBox's current dimensions. It is two methods, one for generating the proper dimensions and one for resizing the image properly (including setting the InterpolationMode to clean the image up)

Submitted By: PsychoCoder
Actions:
Rating:
Views: 20,792

Language: C#

Last Modified: August 17, 2008
Instructions: Need a reference to

System.Drawing
System.Drawing.Drawing2D

Snippet


  1. //Generate new image dimensions
  2. public Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
  3. {
  4.     //double to hold the final multiplier to use when scaling the image
  5.     double multiplier = 0;
  6.  
  7.     //string for holding layout
  8.     string layout;
  9.  
  10.     //determine if it's Portrait or Landscape
  11.     if (currH > currW) layout = "portrait";
  12.     else layout = "landscape";
  13.  
  14.     switch (layout.ToLower())
  15.     {
  16.         case "portrait":
  17.             //calculate multiplier on heights
  18.             if (destH > destW)
  19.             {
  20.                 multiplier = (double)destW / (double)currW;
  21.             }
  22.  
  23.             else
  24.             {
  25.                 multiplier = (double)destH / (double)currH;
  26.             }
  27.             break;
  28.         case "landscape":
  29.             //calculate multiplier on widths
  30.             if (destH > destW)
  31.             {
  32.                 multiplier = (double)destW / (double)currW;
  33.             }
  34.  
  35.             else
  36.             {
  37.                 multiplier = (double)destH / (double)currH;
  38.             }
  39.             break;
  40.     }
  41.  
  42.     //return the new image dimensions
  43.     return new Size((int)(currW * multiplier), (int)(currH * multiplier));
  44. }
  45.  
  46. //Resize the image
  47. private void SetImage(PictureBox pb)
  48. {
  49.     try
  50.     {
  51.         //create a temp image
  52.         Image img = pb.Image;
  53.  
  54.         //calculate the size of the image
  55.         Size imgSize = GenerateImageDimensions(img.Width, img.Height, this.pictureBox1.Width, this.pictureBox1.Height);
  56.  
  57.         //create a new Bitmap with the proper dimensions
  58.         Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);
  59.  
  60.         //create a new Graphics object from the image
  61.         Graphics gfx = Graphics.FromImage(img);
  62.  
  63.         //clean up the image (take care of any image loss from resizing)
  64.         gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
  65.  
  66.         //empty the PictureBox
  67.         pb.Image = null;
  68.  
  69.         //center the new image
  70.         pb.SizeMode = PictureBoxSizeMode.CenterImage;
  71.  
  72.         //set the new image
  73.         pb.Image = finalImg;
  74.     }
  75.     catch (System.Exception e)
  76.     {
  77.         MessageBox.Show(e.Message);
  78.     }
  79. }
  80.  
  81. //Sample usage
  82. private void Form1_Load(object sender, EventArgs e)
  83. {
  84.     SetImage(pictureBox1);
  85. }

Copy & Paste


Comments

Tyb 2010-01-15 10:29:15

I used this excellent code after changing it to VB.Net and it worked great. I did find on issue in that I have to move these variables...... Private img As Image Private imgSize As Size Private finalImg As Bitmap Private gfx As Graphics to modual level. The app I am writing uses this in a form to view photos before deleting them and after running this code I could not delete the file because there was an open handle to the image hanging around. I had to move the variable because if you don't and you dispose the finalImag variable you will get an error because the picturebox loses the image. By moving it out to the module level the app seemed to clean up after itself better. Thanks, TyB

franklin_yah 2010-02-12 16:33:19

I used this code, but it doesn't scale correctly all the time. So here's some other code: public static System.Drawing.Size GenerateImageDimensions( int currW, int currH, int destW, int destH ) { //double to hold the final multiplier to use when scaling the image double multiplier = 0.0, normalizedImageWidth = currW, normalizedImageHeight = currH; if (currW >= destW || currH >= destH) { // either current width or height are greater than destination if (currW > currH) { // current width dominates the scaling multiplier = (double)currH / (float)currW; normalizedImageWidth = destW; normalizedImageHeight = Math.Min(destH, currH) * multiplier; } else { // current height dominates the scaling multiplier = (double)currW / (double)currH; normalizedImageWidth = Math.Min(destW, currW) * multiplier; normalizedImageHeight = destH; } //if } else { // both sides are less, scale out if (currW > currH) { // destination width dominates the scaling multiplier = (double)destW / (double)currW; } else { // destination height dominates the scaling multiplier = (double)destH / (double)currH; } //if normalizedImageWidth *= multiplier; normalizedImageHeight *= multiplier; } //if //return the new image dimensions return new System.Drawing.Size((int)normalizedImageWidth, (int)normalizedImageHeight); } //GenerateImageDimensions


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.