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();
}
}
}