/// <summary>
/// 打开视频文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnVideo_Click(object sender, EventArgs e)
{
OpenFileDialog opd = new OpenFileDialog();//对话框
opd.Filter = "AVI(*.avi)|*.avi|MP4(*.mp4)|*.mp4|WMV(*.wmv)|*.wmv|3GP(*.3gp)|*.3gp|MKV(*.mkv)|*.mkv|RM(*.rm)|*.rm|RMVB(*.rmvb)|*.rmvb|FLV(*.flv)|*.flv|VOB(*.vob)|*.vob|MPEG(*.mpeg)|*.mpeg|MOV(*.mov)|*.mov";//设置过滤条件
if (opd.ShowDialog() == DialogResult.OK)//用户是否单击了“打开”按钮
{
string file = opd.FileName;
c = new Capture(file);
aa();
}
}
public delegate void PlayVideoDelegate();
public delegate void RefreshPictureBoxDelegate(Bitmap frame);
Emgu.CV.Capture c;
public void aa()
{
PlayVideoDelegate play = this.PlayVideoFile;
IAsyncResult status = play.BeginInvoke(null, null);
}
/// <summary>
/// 播放视频文件
/// </summary>
private void PlayVideoFile()
{
while (true)
{
Image<Bgr, byte> frame = c.QueryFrame();
if (frame != null)
{
Image<Gray, byte> grayFrame = frame.Convert<Gray, byte>();
grayFrame.Resize(this.Width, this.Height, INTER.CV_INTER_CUBIC);
RefreshPictureBoxDelegate fresh = this.RefreshPictureBox;
try
{
this.BeginInvoke(fresh, grayFrame.ToBitmap());
}
catch (ObjectDisposedException ex)
{
Thread.CurrentThread.Abort();
}
}
else
{
break;
}
}
}
/// <summary>
/// 刷新UI线程的pixtureBox的方法。
/// </summary>
/// <param name="frame">要刷新的帧。</param>
private void RefreshPictureBox(Bitmap frame)
{
this.picBoxView.Image = frame;
}
------解决思路----------------------
while (true)
{
Image<Bgr, byte> frame = c.QueryFrame();
if (frame != null)
{
Image<Gray, byte> grayFrame = frame.Convert<Gray, byte>();
grayFrame.Resize(this.Width, this.Height, INTER.CV_INTER_CUBIC);
RefreshPictureBoxDelegate fresh = this.RefreshPictureBox;
try
{
this.BeginInvoke(fresh, grayFrame.ToBitmap());
}
catch (ObjectDisposedException ex)
{
Thread.CurrentThread.Abort();
}
}
else
{
break;
}
Thread.Sleep(50);//每秒20帧
}