You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Form2中使用Form1的摄像头捕获事件并传递pictureBox数据

实现方案(基于WinForm场景)

以下两种方案覆盖不同项目规模的需求,都可以实现你要的功能:


方案1:公共属性传值(最简方案,适合小型项目)

不需要修改太多原有逻辑,快速实现需求:

  • 首先在Form1中定义公共属性暴露摄像头捕获的图像:
// Form1.cs 新增代码
public Image CapturedCameraImage => pictureBox1.Image;
  • 在Form2中通过构造函数接收Form1实例,需要时获取图像即可:
// Form2.cs 代码
private Form1 _mainForm;

// 修改Form2的构造函数
public Form2(Form1 mainForm)
{
    InitializeComponent();
    _mainForm = mainForm;
}

// 需要更新图像时调用该方法
public void RefreshCameraImage()
{
    if (_mainForm.CapturedCameraImage != null)
    {
        // 克隆图像避免两个窗体绑定同个资源,释放时抛异常
        pictureBox1.Image = (Image)_mainForm.CapturedCameraImage.Clone();
    }
}
  • 你直接在Form1的摄像头捕获事件逻辑里,调用form2实例.RefreshCameraImage()即可完成同步。

方案2:委托事件订阅(低耦合,推荐中大型项目使用)

不需要Form2持有Form1的实例引用,耦合度更低,后续维护成本小:

  • 在Form1中定义委托和事件,捕获到图像时主动推送:
// Form1.cs 代码
// 定义图像推送委托
public delegate void CameraImagePushedHandler(Image capturedImage);
// 定义推送事件
public event CameraImagePushedHandler CameraImagePushed;

// 在你原有摄像头捕获的回调方法中,新增事件触发逻辑
private void CameraCapture_Completed(object sender, EventArgs e)
{
    // 你原来给Form1的pictureBox1赋值的逻辑保留
    pictureBox1.Image = 你捕获到的摄像头图像;

    // 触发事件推送图像
    if (CameraImagePushed != null && pictureBox1.Image != null)
    {
        CameraImagePushed((Image)pictureBox1.Image.Clone());
    }
}
  • 在Form2中订阅Form1的推送事件即可自动更新:
// Form2.cs 代码
private void Form2_Load(object sender, EventArgs e)
{
    // 拿到当前运行的Form1实例
    var mainForm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
    if (mainForm != null)
    {
        mainForm.CameraImagePushed += MainForm_CameraImagePushed;
    }
}

private void MainForm_CameraImagePushed(Image capturedImage)
{
    // 兼容子线程捕获的场景,避免跨线程操作控件报错
    if (pictureBox1.InvokeRequired)
    {
        pictureBox1.Invoke(new Action(() => pictureBox1.Image = capturedImage));
        return;
    }
    pictureBox1.Image = capturedImage;
}

// 关闭Form2时取消订阅,避免内存泄漏
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    var mainForm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
    if (mainForm != null)
    {
        mainForm.CameraImagePushed -= MainForm_CameraImagePushed;
    }
}

通用注意事项

  • 不要直接传递pictureBox1.Image的原始引用,必须克隆后再传递,否则其中一个窗体释放图像资源时,另一个窗体访问会抛出ObjectDisposedException异常
  • 如果摄像头捕获逻辑在非UI线程执行,更新控件时必须用Invoke切换到UI线程,否则会报跨线程操作错误

内容的提问来源于stack exchange,提问作者Ezgi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 20:39:01