Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

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

Search Engine Rank of your blog or websites