视频图像处理系列索引 || Arcgis/Engine/Server开发索引 || Web Map Gis开发索引 || jquery表格组件 JQGrid索引
WPF MVVM模式开发实现简明教程索引 || ArcGIS Runtime WPF(.net C#)开发简明教程索引

Emgu.CV 播放视频-本地文件/RTSP流

using Emgu.CV;

using System;

using System.Drawing;

using System.Threading;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        Emgu.CV.Capture cap;

        public Form1()

        {

            InitializeComponent();

            //cap = new Emgu.CV.Capture("F:\\test.avi");

            cap = new Emgu.CV.Capture("rtsp://192.168.1.6.............");

            cap.ImageGrabbed += capture_ImageGrabbed;

            cap.Start();

 

        }

 

        private delegate void SetPicVideo(Bitmap val);//跨线程修改图片框

        private object lockObj = new object();

        private Thread SetPicVideoThread;

        Bitmap bmpVideo = null;

        private void capture_ImageGrabbed(object sender, EventArgs e)

        {

            try

            {

                Mat frame = new Mat();

                //lock (lockObj)

                {

                    if (cap != null)

                    {

                       

                        if (!cap.Retrieve(frame))

                        {

                            frame.Dispose();

                            return;

                        }

 

                        if (frame.IsEmpty)

                            return;

 

                        bmpVideo =  frame.Bitmap;

                        SetPicVideoThread = new Thread(new ThreadStart(setPicVideo));

                        SetPicVideoThread.IsBackground = true;

                        SetPicVideoThread.Start();

                    }

                }

 

                //frame.Dispose();

            }

            catch (Exception ex)

            {

 

            }

        }

 

        void SetPic(Bitmap val)

        {

            if (val != null)

            {

                this.pictureBox1.Image = val;

            }

        }

 

        private void setPicVideo()

        {

            if (pictureBox1.InvokeRequired)

            {

                SetPicVideo d = new SetPicVideo(SetPic);

                object[] arg = new object[] { bmpVideo };//要传入的参数值

                this.Invoke(d, arg);

            }

            else

            {

                SetPic(bmpVideo);

            }

        }

 

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            cap.Stop();

        }

    }

}

 

posted @ 2016-04-26 09:54  jhlong  阅读(4921)  评论(8编辑  收藏  举报
海龙的博客 jhlong@cnblogs 版权所有© 转载请注明链接.有用请推荐一下
代码全部经过本人测试,但不保证复制粘贴就正常运行,更不保证能解决你的问题,请结合前后代码及描述理解后修改和使用