Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Modify the Size or Placement of a Picture at Run Time (Windows Forms)

If you use the Windows Forms PictureBox control on a form, you can set the SizeMode property on it to:

  • Align the picture's upper left corner with the control's upper left corner

  • Center the picture within the control

  • Adjust the size of the control to fit the picture it displays

  • Stretch any picture it displays to fit the control

Stretching a picture (especially one in bitmap format) can produce a loss in image quality. Metafiles, which are lists of graphics instructions for drawing images at run time, are better suited for stretching than bitmaps are.

To set the SizeMode property at run time

  • Set SizeMode to Normal (the default), AutoSize, CenterImage, or StretchImage. Normal means that the image is placed in the control's upper-left corner; if the image is larger than the control, its lower and right edges are clipped. CenterImage means that the image is centered within the control; if the image is larger than the control, the picture's outside edges are clipped. AutoSize means that the size of the control is adjusted to the size of the image. StretchImage is the reverse, and means that the size of the image is adjusted to the size of the control.

    In the example below, the path set for the location of the image is the My Documents folder. This is done, because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application. The example below assumes a form with a PictureBox control already added.

    Private Sub StretchPic()
       ' Stretch the picture to fit the control.
       PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
       ' Load the picture into the control.
       ' You should replace the bold image 
       ' in the sample below with an icon of your own choosing.
       PictureBox1.Image = Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Image.gif")
    End Sub
    
    

    private void StretchPic(){
       // Stretch the picture to fit the control.
       PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
       // Load the picture into the control.
       // You should replace the bold image 
       // in the sample below with an icon of your own choosing.
       // Note the escape character used (@) when specifying the path.
       PictureBox1.Image = Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       + @"\Image.gif")
    }
    
    

    private void StretchPic(){
       // Stretch the picture to fit the control.
       pictureBox1.set_SizeMode(PictureBoxSizeMode.StretchImage);
       // Load the picture into the control.
       // You should replace "image.gif" in the sample below 
       // with an icon of your own choosing.
       pictureBox1.set_Image(Image.FromFile
       (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + "\\Image.gif"));
    }
    
    

    private:
       void StretchPic()
       {
          // Stretch the picture to fit the control.
          pictureBox1->SizeMode = PictureBoxSizeMode::StretchImage;
          // Load the picture into the control.
          // You should replace the bold image 
          // in the sample below with an icon of your own choosing.
          pictureBox1->Image = Image::FromFile(String::Concat(
             System::Environment::GetFolderPath(
             System::Environment::SpecialFolder::Personal),
             "\\Image.gif"));
       }
    

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.