【IT168技术文档】
其中,为了使用DirectShow,我们除了需要windows.h外,还需要dshow.h,qedit.h和atlbase.h三个头文件,最后再加上一个strmiids.lib库文件。
接下来就开启Visual C# 2005 Express来做一个简单的界面程序。为什么选择C#来开发界面程序呢?原因很简单,因为C#很简单,同时Visual C# 2005 Express这样免费又功能强大的工具可以使用。
界面程序很简单,就下面这个样子:
C# 部分调用前面写好的DLL函数,实现DDshow的抓图。 MovieGrabberDLL.cs源代码如下:
窗口类MainForm.cs的源代码如下:using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Drawing; namespace MovieGrabberCSharp ...{ class MovieGrabberDLL ...{ [DllImport("MovieGrabberDLL.dll")] public static extern int fnMovieGrabberDLL(); [DllImport("MovieGrabberDLL.dll")] public static extern IntPtr GrabMovieFrame(string aPath, int grayColorCountThreshold); public static Bitmap GrabMovieFrameBitmap(string aPath,int grayColorCountThreshold) ...{ IntPtr hBitmap = GrabMovieFrame(aPath, grayColorCountThreshold); if(hBitmap == IntPtr.Zero) return null; return Bitmap.FromHbitmap(hBitmap); } public static Bitmap GrabMovieFrameBitmap(string aPath) ...{ return GrabMovieFrameBitmap(aPath, 8); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MovieGrabberCSharp ...{ public partial class MainForm : Form ...{ public MainForm() ...{ InitializeComponent(); } private void OpenMovieFilePathButton_Click(object sender, EventArgs e) ...{ OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) ...{ MovieFilePathTextBox.Text = dlg.FileName; } } private void GrabberButton_Click(object sender, EventArgs e) ...{ Bitmap bitmap = MovieGrabberDLL.GrabMovieFrameBitmap(MovieFilePathTextBox.Text); if (bitmap != null) ...{ MessageBox.Show("抓图成功!"); GrabberPictureBox.SizeMode = PictureBoxSizeMode.StretchImage; GrabberPictureBox.Image = bitmap; GrabberPictureBox.Invalidate(); GrabberPictureBox.Refresh(); } else ...{ MessageBox.Show("失败!"); } } private void ExitButton_Click(object sender, EventArgs e) ...{ this.Close(); } } }