This page was last reviewed on Oct 5, 2022.
Dot Net Perls
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.
Control info. PictureBox displays an image graphically in the Windows Forms program. For complex programs that display images in a specific way, it is not ideal.
Steps. 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.
Then Drag the PictureBox icon to your window in the desired location. This creates a rectangular PictureBox instance.
Next In the next step of this tutorial, we assign an image to the PictureBox from the disk.
Here We use a PNG image in this code. But JPEG images and GIF images are also supported.
Detail We use the 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; }
Download. 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.
Also This example shows that the 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.
And We can zoom the image so that it maintains its original aspect ratio. The image does not appear distorted.
AutoSize CenterImage Normal StretchImage Zoom
Image. 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.
Height, width. 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.
Also It is easy to crop a PictureBox by setting its Width and Height to the desired values.
A summary. The PictureBox displays images of various types. It handles the drawing of the pixels, and provides properties for adjusting the dimensions of the control.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 5, 2022 (edit).
© 2007-2024 Sam Allen.