PictureBox
This is a rectangular region for an image. It supports many image formats. It has an adjustable size. It can access image files from your disk or from the Internet.
PictureBox
displays an image graphically in the Windows Forms program. For complex programs that display images in a specific way, it is not ideal.
The PictureBox
provides a way to display an image. It can handle many different image formats. To add a PictureBox
, go to the Designer view and open the Toolbox.
PictureBox
icon to your window in the desired location. This creates a rectangular PictureBox
instance.PictureBox
from the disk.Form_Load
event. To create this event handler, double-click on the enclosing window in the Designer view.using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Construct an image object from a file in the local directory.
// ... This file must exist in the solution.
Image image = Image.FromFile("BeigeMonitor1.png");
// Set the PictureBox image property to this image.
// ... Then, adjust its height and width properties.
pictureBox1.Image = image;
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
}
}
}
ImageLocation
This sets the image data from a file. If you use the AutoSize
enum
with the ImageLocation
property, your PictureBox
will be sized correctly.
private void Form1_Load(object sender, EventArgs e)
{
// Set the ImageLocation property to a file on the disk.
// ... Set the size mode so that the image is correctly sized.
pictureBox1.ImageLocation = "BeigeMonitor1.png";
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
We use the ImageLocation
instance property to specify the URL of an image to download and place in the PictureBox
. We can use the website icon file for this site.
PictureBox
supports the ICO format for images.private void Form1_Load(object sender, EventArgs e)
{
// Set the ImageLocation property to a URL.
// ... This example image is tiny and is the icon for Dot Net Perls.
pictureBox1.ImageLocation = "http://www.dotnetperls.com/favicon.ico";
}
PictureBoxSizeMode
With the PictureBoxSizeMode.AutoSize
enum
, we can center the image both horizontally and vertically. We can stretch the image so that it fills the entire enclosing box.
AutoSize CenterImage Normal StretchImage Zoom
The Image type is a class
that encapsulates information about an image, including its data in bytes. It provides an abstract
data type for handling an image in a clear way.
We assign the Height and Width properties of the Image. This forces the PictureBox
to resize correctly. If you omit these statements, the image may be cropped.
PictureBox
by setting its Width and Height to the desired values.The PictureBox
displays images of various types. It handles the drawing of the pixels, and provides properties for adjusting the dimensions of the control.