简体   繁体   English

图片盒中的视频C#

[英]Video in picturebox c#

I have a problem retrieving my video in the picture box in my second form. 我在第二种形式的图片框中检索视频时遇到问题。 This is my code 这是我的代码

Main.cs Main.cs

  private void start_video()
    {

        while (!needClose)
        {
            video.capture();
            pictureBox1.Image = ImageFrame.video;
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        camera = new Camera();
        camera.Show();
    }

This is my class (of course there is a function that set video) 这是我的课程(当然有设置视频的功能)

  public class ImageFrame
  {
   public static Image video { get; set; }
  }

And this is my Camera.cs 这是我的Camera.cs

 public partial class Camera : Form
{
    private bool test = false;

    public Camera()
    {
        InitializeComponent();
        returnVideo();
    }

   private void returnVideo()
    {
        while (!test)
        {
            pictureBox2.Image = ImageFrame.video;
        }
    }

The video works fine in pictureBox1, but when I click on button1 to open the new form retrieving the same video of pictureBox1 it just freeze. 该视频在pictureBox1中可以正常工作,但是当我单击button1打开新窗体时,检索到pictureBox1的同一视频只是冻结了。 Why? 为什么? If I don't use the while it would just show a frame but I need the video (that is from the camera, so in live view). 如果我不使用那一会儿,它只会显示一帧,但是我需要视频(来自摄像机,因此是实时取景)。

This blocks the event-loop to run, so on every window change UI update will stop and therefore freeze. 这会阻止事件循环运行,因此在每次更改窗口时,UI更新都会停止并因此冻结。

    while (!test) // = while(true)
    {
        pictureBox2.Image = ImageFrame.video;
    }

Use a timer to update the picture every 50 msec or less. 使用计时器每隔50毫秒或更短时间更新一次图片。

EDIT: Some pseudo code: 编辑:一些伪代码:

timer1.Interval=50;
timer1.Tick+=timer1_Tick;
timer1.Start();

void timer1_Tick(..)
{
  pictureBox2.Image = ImageFrame.video;
}

This also reduces CPU load. 这也减少了CPU负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM