如何在另一种形式中使用相机捕获一种形式的事件?

How to use camera capture event of a form in another form?

我正在创建一个新的UI,我想在form2上使用form1中的相机捕获事件(简而言之,我正在尝试将数据从Form1的pictureBox1传输到Form2的pictureBox1。)如何我能做到吗?

谢谢...

  1. 在表单2中,实现一个public方法接受一张图片并显示在图片框中
  2. 在表格1中,在图片框的PAINT事件中,调用表格2的上述方法

示例代码

表格 1

public partial class Form1 : Form
{
    //Object of Form 2 in Form 1
    Form2 oFrm2 = new Form2();
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        //Load an image in the picture box
        pictureBox1.Load(@"E:\Temp\SmartRetire.jpeg");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //Consume the PAINT event of the picture box to notify a change in image
        pictureBox1.Paint += PictureBox1_Paint;
        //Show a blank form2
        oFrm2.Show();
    }

    //Raised when the picture box image is changed
    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {
        //Show the image in form 2
        oFrm2.ShowImage(pictureBox1.Image);
    }
}

表格 2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    //Public method called from Form1 when the image is changed in picture box (in form1)
    public void ShowImage(Image oImage)
    {
        //Display the picture in form2
        pictureBox1.Image = oImage;
    }

}