PictureBox in C#: Usage Guide
The PictureBox control in C# is used for displaying images. It allows images to be displayed in a form, where they can be loaded from local files or dynamically generated by the program. The PictureBox control offers properties and methods to control how the image is displayed, such as the SizeMode property for setting the display mode (stretch, center, fit), and the Image property for setting the image to be displayed.
Here are some common usage examples:
- Display images of local files.
pictureBox1.Image = Image.FromFile("路径");
- Display images in the resource file:
pictureBox1.Image = Properties.Resources.图片名;
- Adjust the image display settings:
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; // 拉伸
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; // 居中
- Dynamically generate and display images.
Bitmap bmp = new Bitmap(100, 100);
using (Graphics g = Graphics.FromImage(bmp))
{
// 在图片上绘制内容
g.FillRectangle(Brushes.Red, 0, 0, 100, 100);
}
pictureBox1.Image = bmp;
Using the methods above, it is easy to display images in a C# Windows Form application and control the way the images are shown using the PictureBox control.