Detailed usage of pictureBox control in C#

PictureBoxWhen using controls in C# , you can use it in the following detailed ways:

  1. Drop a control on a form PictureBox:
    In Visual Studio's Form Designer, drag and drop a PictureBoxcontrol from the toolbox onto your form.

  2. Set PictureBoxthe properties of the :

    • Image: Set or get PictureBoxthe image to be displayed on the .
    • SizeMode: Set PictureBoxthe display mode of the image, such as stretching, adaptive, etc.
    • BorderStyle: Sets PictureBoxthe border style of the .
  3. Handled PictureBoxevents for :

    • Typically, PictureBoxthere is no specific event. You can use events of other controls or custom events to handle PictureBoxoperations related to .

Here is an example showing how to use PictureBoxthe 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 PictureBoxcontrol 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, PictureBoxthe image in is cleared.

Hope this example can help you understand and use PictureBoxthe detailed method of the control. If you have any further questions, please feel free to ask!

Guess you like

Origin blog.csdn.net/xiaogongzhu001/article/details/131080527