如何使用C#将实时视频捕获的一个图像/帧分配到另一个图片框

问题描述 投票:0回答:1

我有一个图片框,显示摄像机的实时视频。我有另一个图片框,我想在单击按钮时显示实时视频的一帧。

我尝试单击按钮时,第二个图片框中会显示一帧。为此,我尝试了下面的代码,但它提供了空引用错误- 位图图像 = (Bitmap)pictureBox1.Image.Clone(); pictureBox2.Image = 图像;

c# winforms camera
1个回答
0
投票

您遇到了 null 引用错误,因为当您尝试克隆它时,pictureBox1.Image 可能为 null。如果实时视频源尚未开始或者图像尚未正确分配给 pictureBox1,则可能会发生这种情况。

这是代码片段的修订版本

private void CaptureFrameButton_Click(object sender, EventArgs e)
{
    if (pictureBox1.Image != null)
    {
        Bitmap image = new Bitmap(pictureBox1.Image); // Clone the image
        pictureBox2.Image = image; // Display the cloned image
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.