C#,每5秒钟从网络摄像机连续保存帧

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

[运行基于GUI的代码后,它实际上会打开网络摄像头供稿并每5s保存一次帧,在随机运行时间(可能是5分钟或20分钟)后,会出现一个异常,''另一个对象正在使用该对象处理''可能与当前帧的保存有关。有什么想法会引起问题,应该进行哪些代码修改?

public partial class Form1 : Form
    {
        private FilterInfoCollection CaptureDevices;
        private VideoCaptureDevice videoSource;
        bool blncapturing;

        public Form1()
        {
            InitializeComponent();
            timer1.Enabled = false;
            timer1.Interval = 5000;
            blncapturing = false;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            videoSource = new VideoCaptureDevice(CaptureDevices[comboBox1.SelectedIndex].MonikerString);
            videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
            videoSource.Start();
            timer1.Enabled = true;
        }


        private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            if (pictureBox1.Image != null)
                ((IDisposable)pictureBox1.Image).Dispose();


            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

            CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo Device in CaptureDevices)
            {
                comboBox1.Items.Add(Device.Name);
            }
            comboBox1.SelectedIndex = 0;
            videoSource = new VideoCaptureDevice();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            videoSource.Stop();
            pictureBox1.Image = null;
            pictureBox1.Invalidate();
            pictureBox2.Image = null;
            pictureBox2.Invalidate();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Capturing();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (videoSource.IsRunning == true)
            {
                videoSource.Stop();
            }
            Application.Exit(null);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            Capturing();
            timer1.Start();
        }

        private void Capturing()
        {
            try
            {
                if (blncapturing == false)
                {
                    blncapturing = true;

                    pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();

                    string strGrabFileName = String.Format("C:\\Users\\echristo\\Desktop\\trial\\Snapshot_{0:yyyyMMdd_hhmmss.fff}.bmp", DateTime.Now);
                    pictureBox2.Image.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg) ;
                    blncapturing = false;

                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);                
            }


        }
c# image video webcam aforge
1个回答
0
投票

如果从不同线程访问对象,则需要一个锁。当您从其他线程访问控件时,也请使用invoke。

© www.soinside.com 2019 - 2024. All rights reserved.