Intermediate 10 min read Control #17

PictureBox Control

PictureBox displays images in your Windows Forms application. It supports many image formats and can load images from files, resources, or URLs.

What is a PictureBox?

PictureBox is used to display images. It supports most common formats: JPG, PNG, BMP, GIF, and ICO. You can resize, stretch, or zoom images using the SizeMode property.

Code Example

// Load image from file
pictureBox1.Image = Image.FromFile("C:\\Images\\photo.jpg");

// Load image from resources
pictureBox1.Image = Properties.Resources.MyImage;

// Using OpenFileDialog
private void btnLoadImage_Click(object sender, EventArgs e)
{
    using (var openDialog = new OpenFileDialog())
    {
        openDialog.Filter = "Image Files|*.jpg;*.png;*.bmp;*.gif";
        if (openDialog.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(openDialog.FileName);
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }
    }
}

Exercise

Task: Create an image viewer application.

  1. Add a PictureBox with Dock = Fill.
  2. Add a MenuStrip with: File → Open Image.
  3. Add Buttons for Zoom In, Zoom Out, and Fit.
  4. Add a StatusStrip showing image size.
  5. Allow drag and drop of images.
Key Takeaway

PictureBox makes displaying images easy. Use SizeMode to control how images are displayed and Image property to set the image.