C#,枚举系统中可用的视频捕捉设备 如题。在网上搜索了一下,很多人提出DirectShow可以实现。DirectShow在枚举设备时需要Filter的参数,但这些帖子却没有说明这个参数是什么。用其它方式可以解决也非常感谢,请不吝赐教。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary>视频输入设备信息</summary> internal class Device : IDisposable { #region API [DllImport("ole32.dll")] static extern int CreateBindCtx(int reserved, out IBindCtx ppbc); [DllImport("ole32.dll", CharSet = CharSet.Unicode)] static extern int MkParseDisplayName(IBindCtx pbc, string szUserName, ref int pchEaten, out IMoniker ppmk); #endregion #region GUID static readonly Guid SystemDeviceEnum = new Guid(0x62BE5D10, 0x60EB, 0x11D0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86); static readonly Guid VideoInputDevice = new Guid(0x860BB310, 0x5D01, 0x11D0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86); #endregion /// <summary>枚举本机上的视频设备</summary> public static IEnumerable<Device> Devices { get { IMoniker[] ms = new IMoniker[5]; ICreateDevEnum enumD = Activator.CreateInstance(Type.GetTypeFromCLSID(SystemDeviceEnum)) as ICreateDevEnum; IEnumMoniker moniker; Guid g = VideoInputDevice; if (enumD.CreateClassEnumerator(g, out moniker, 0) == 0) { while (true) { int r = moniker.Next(1, ms, IntPtr.Zero); if (r != 0 || ms[0] == null) break; yield return new Device(ms[0]); ms[0] = null; } } } } /// <summary>根据设备路径创建设备</summary> /// <param name="path">设备路径</param> /// <returns>视频设备</returns> public static Device Create(string path) { IBindCtx bindCtx = null; IMoniker moniker = null; int n = 0; if (CreateBindCtx(0, out bindCtx) == 0) { int hr = MkParseDisplayName(bindCtx, path, ref n, out moniker); hr.ThrowExceptionForHR(); Marshal.ReleaseComObject(bindCtx); bindCtx = null; } if (moniker != null) return new Device(moniker); else return null; } /// <summary>设备名称</summary> public string Name { get; private set; } /// <summary>设备路径</summary> public string DevicePath { get; private set; } public IMoniker Moniker { get; private set; } private Device(IMoniker moniker) { Moniker = moniker; DevicePath = GetMonikerString(moniker); Name = GetName(moniker); } internal IBaseFilter CreateFilter() { object filterObject = null; IBindCtx bindCtx = null; if (CreateBindCtx(0, out bindCtx) == 0) { Guid filterId = typeof(IBaseFilter).GUID; Moniker.BindToObject(null, null, ref filterId, out filterObject); Marshal.ReleaseComObject(bindCtx); } return filterObject as IBaseFilter; } private string GetMonikerString(IMoniker moniker) { string str; moniker.GetDisplayName(null, null, out str); return str; } private string GetName(IMoniker moniker) { Object bagObj = null; IPropertyBag bag = null; try { Guid bagId = typeof(IPropertyBag).GUID; moniker.BindToStorage(null, null, ref bagId, out bagObj); bag = (IPropertyBag)bagObj; object val = ""; int hr = bag.Read("FriendlyName", out val, null); if (hr != 0) Marshal.ThrowExceptionForHR(hr); string ret = (string)val; if ((ret == null) || (ret.Length < 1)) throw new ApplicationException(); return ret; } catch (Exception) { return ""; } finally { bag = null; if (bagObj != null) { Marshal.ReleaseComObject(bagObj); bagObj = null; } } } public override string ToString() { return Name; } #region IDisposable 成员 public void Dispose() { Marshal.ReleaseComObject(Moniker); GC.SuppressFinalize(this); } #endregion ~Device() { Dispose(); } } Study WPF 如何把datagridview中数据保存到数据库中 C#图片问题 最近想学winform,但是朋友说winform淘汰了,应该学WPF....ture? C#队列算法,求解!!! C#里的线程问题 winform里dataGrid1.DataBind();怎么报错? 如何点击label 进入下一form页面? 怎样防止往文本框中复制进字符(我已编写代码使文本框只能输入数字)? DataGrid 害死我了!!!!怎么没有DataBind()方法? Visual Studio 2008 开发环境中复制行的快捷键是什么? 怎样提高一个类引用另外一个类的属性的效率
/// <summary>视频输入设备信息</summary>
internal class Device : IDisposable
{
#region API
[DllImport("ole32.dll")]
static extern int CreateBindCtx(int reserved, out IBindCtx ppbc); [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
static extern int MkParseDisplayName(IBindCtx pbc, string szUserName, ref int pchEaten, out IMoniker ppmk);
#endregion #region GUID
static readonly Guid SystemDeviceEnum = new Guid(0x62BE5D10, 0x60EB, 0x11D0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86); static readonly Guid VideoInputDevice = new Guid(0x860BB310, 0x5D01, 0x11D0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86);
#endregion /// <summary>枚举本机上的视频设备</summary>
public static IEnumerable<Device> Devices
{
get
{
IMoniker[] ms = new IMoniker[5];
ICreateDevEnum enumD = Activator.CreateInstance(Type.GetTypeFromCLSID(SystemDeviceEnum)) as ICreateDevEnum;
IEnumMoniker moniker;
Guid g = VideoInputDevice;
if (enumD.CreateClassEnumerator(g, out moniker, 0) == 0)
{
while (true)
{
int r = moniker.Next(1, ms, IntPtr.Zero);
if (r != 0 || ms[0] == null)
break;
yield return new Device(ms[0]);
ms[0] = null;
}
}
}
} /// <summary>根据设备路径创建设备</summary>
/// <param name="path">设备路径</param>
/// <returns>视频设备</returns>
public static Device Create(string path)
{
IBindCtx bindCtx = null;
IMoniker moniker = null;
int n = 0; if (CreateBindCtx(0, out bindCtx) == 0)
{
int hr = MkParseDisplayName(bindCtx, path, ref n, out moniker);
hr.ThrowExceptionForHR();
Marshal.ReleaseComObject(bindCtx);
bindCtx = null;
}
if (moniker != null)
return new Device(moniker);
else
return null;
} /// <summary>设备名称</summary>
public string Name { get; private set; } /// <summary>设备路径</summary>
public string DevicePath { get; private set; } public IMoniker Moniker { get; private set; } private Device(IMoniker moniker)
{
Moniker = moniker;
DevicePath = GetMonikerString(moniker);
Name = GetName(moniker);
} internal IBaseFilter CreateFilter()
{
object filterObject = null;
IBindCtx bindCtx = null; if (CreateBindCtx(0, out bindCtx) == 0)
{
Guid filterId = typeof(IBaseFilter).GUID;
Moniker.BindToObject(null, null, ref filterId, out filterObject);
Marshal.ReleaseComObject(bindCtx);
}
return filterObject as IBaseFilter;
} private string GetMonikerString(IMoniker moniker)
{
string str;
moniker.GetDisplayName(null, null, out str);
return str;
} private string GetName(IMoniker moniker)
{
Object bagObj = null;
IPropertyBag bag = null; try
{
Guid bagId = typeof(IPropertyBag).GUID;
moniker.BindToStorage(null, null, ref bagId, out bagObj);
bag = (IPropertyBag)bagObj; object val = "";
int hr = bag.Read("FriendlyName", out val, null);
if (hr != 0)
Marshal.ThrowExceptionForHR(hr); string ret = (string)val;
if ((ret == null) || (ret.Length < 1))
throw new ApplicationException(); return ret;
}
catch (Exception)
{
return "";
}
finally
{
bag = null;
if (bagObj != null)
{
Marshal.ReleaseComObject(bagObj);
bagObj = null;
}
}
} public override string ToString()
{
return Name;
} #region IDisposable 成员 public void Dispose()
{
Marshal.ReleaseComObject(Moniker);
GC.SuppressFinalize(this);
} #endregion ~Device()
{
Dispose();
}
}