我想在我的.net程序中调用摄象头大体应该怎么做

wuxiao_v 2008-04-06 01:12:46
只需要给一个大体的方向就可以,我对.net很陌生但是有其他编程经验
...全文
64 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tellyes 2008-04-09
  • 打赏
  • 举报
回复
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] //引用非托管动态链接库
public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); //这句里面比较难理解的是[MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName,意思是将托管的string类型看作是非托管的VBByRefStr
wuxiao_v 2008-04-09
  • 打赏
  • 举报
回复



[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);


谁能帮我解释一下这段代码大体什么意思
hopewoo 2008-04-06
  • 打赏
  • 举报
回复
我好像给太多了...
hopewoo 2008-04-06
  • 打赏
  • 举报
回复

public partial class Form1 : Form
{


#region API
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool DestroyWindow(int hndw);
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("vfw32.dll")]
public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
[DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capSetCallbackOnFrame(int hwnd, string s);
#endregion


public struct videohdr_tag
{
public byte[] lpData;
public int dwBufferLength;
public int dwBytesUsed;
public int dwTimeCaptured;
public int dwUser;
public int dwFlags;
public int[] dwReserved;

}

IntPtr vhandle;
private const int WM_CAP_EDIT_COPY = 0x41E;
private const int WM_CAP_SET_SCALE = 0x435;
private const int WM_CAP_SET_PREVIEWRATE = 0x434;
private const int WM_CAP_SET_PREVIEW = 0x432;
private void OpenCapture()
{

int intWidth = 160;
int intHeight = 120;
int intDevice = 0;
string refDevice = intDevice.ToString();
hHwnd = capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 160, 120, vhandle.ToInt32(), 0);
//this.LbSysMsg.Text = "";
//this.LbSysMsg.Text += "驱动:" + refDevice;
if (SendMessage(hHwnd, 0x40a, intDevice, 0) > 0)
{
SendMessage(this.hHwnd, WM_CAP_SET_SCALE, -1, 0);
SendMessage(this.hHwnd, WM_CAP_SET_PREVIEWRATE, 0x42, 0);
SendMessage(this.hHwnd, WM_CAP_SET_PREVIEW, -1, 0);
SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);
}
else
{
DestroyWindow(this.hHwnd);
}
}


private int hHwnd;
public Form1()
{

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{
vhandle = this.Handle;
OpenCapture();
}





}
boblaw 2008-04-06
  • 打赏
  • 举报
回复
或者DirectX
boblaw 2008-04-06
  • 打赏
  • 举报
回复
可以使用VFW API

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧