The picturebox control of Windows Forms is used to show images in bitmap, GIF or JPEG format, or in icon format.
The image shown determines the property of the image that can be set at runtime or at design time. Alternatively, by setting the ImageLocation
property we can specify a frame and load it synchronously with the Load method or with the LoadAsync
method.
The control of the PictureBox is shown with no border by default. Use the BorderStyle
property to distinguish the image box from the rest of the form, although it contains no picture, to provide a standard or three-dimensional border. The PictureBox is not a selectable control that does not allow the focus to be received.
For Example:
The code example below shows how the WaitOnLoad
property is used. Paste a following code for this example in a Windows form that includes the pictureBox1 and a startLoadButton.
Code:
private void btnStartLoadButton_Click(object sender, EventArgs e)
{
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync(@"C:\UKAcademe.png");
}
Output:
Sets the property to true WaitOnLoad
means that the image is synchronously loaded. This causes the UI to be blocked until the image is loaded from the other input. If we load the image by default WaitOnLoad
and LoadAsync
, we will see the original image when the image we set is loaded and when we load it we can interact with it.
By setting the image
property to a valid picture, we can load an image on a design form and display it on a form. The table below shows the file types acceptable.
Type | File name extension |
Bitmap | .bmp |
Icon | .ico |
GIF | .gif |
Metafile | .wmf |
JEPG | .jpg |
To show a picture at the time of design
Image
Property, click the Ellipsis button to show the Open dialog box in the Properties window.
Select an image
property on the Properties window and right - click the image on the left of the image name. Choose Reset.
We can set the SizeMode
property on a form when using the Windows Form PictureBox control:
Note: Spreading an image (specifically a bitmap image) may lead to a loss of image quality. Metafiles, which are graphical lists for drawing images, are better suited than bit maps for stretching.
To set the SizeMode property at run time
Set Normal (default), AutoSize, CenterImage, and StretchImage to SizeMode.
Normally
Normally, the image will be placed in the top - left corner of the control. The lower and right edges of the image are cut off if it is larger than control.
CenterImage
the centering of the image inside the control ; The outer edges of the picture are cut if the image is larger than the control.
Auto Size
AutoSize means the control size is adjusted to the image size.
Stretch Image
StretchImage is the other way around and means that the image size is adjusted to the control size.
Code:
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
Complete Souce Code:
using System;
using System.Windows.Forms;
namespace PictureBoxControl
{
public partial class frmPictureBox : Form
{
public frmPictureBox()
{
InitializeComponent();
}
private void btnStartLoadButton_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync(@"C:\UKAcademe.png");
}
}
}