Share via


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

Tasks

How to: Load a Picture Using the Designer (Windows Forms)

How to: Set Pictures at Run Time (Windows Forms)

Reference

PictureBox

PictureBox Control Overview (Windows Forms)

Other Resources

PictureBox Control (Windows Forms)