PictureBox
When using controls in C# , you can use it in the following detailed ways:
-
Drop a control on a form
PictureBox
:
In Visual Studio's Form Designer, drag and drop aPictureBox
control from the toolbox onto your form. -
Set
PictureBox
the properties of the :Image
: Set or getPictureBox
the image to be displayed on the .SizeMode
: SetPictureBox
the display mode of the image, such as stretching, adaptive, etc.BorderStyle
: SetsPictureBox
the border style of the .
-
Handled
PictureBox
events for :- Typically,
PictureBox
there is no specific event. You can use events of other controls or custom events to handlePictureBox
operations related to .
- Typically,
Here is an example showing how to use PictureBox
the control:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PictureBoxExample
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnLoadImage_Click(object sender, EventArgs e)
{
// 加载图像到 PictureBox
string imagePath = "image.jpg"; // 图像的路径
Image image = Image.FromFile(imagePath);
pictureBox.Image = image;
}
private void btnClearImage_Click(object sender, EventArgs e)
{
// 清除 PictureBox 中的图像
pictureBox.Image = null;
}
}
}
In the above example, we created a form application called "MainForm" and placed a PictureBox
control and two buttons. When the "Load Image" button is clicked, the image will be loaded from the specified path and displayed in PictureBox
. When the Clear Image button is clicked, PictureBox
the image in is cleared.
Hope this example can help you understand and use PictureBox
the detailed method of the control. If you have any further questions, please feel free to ask!