http://blog.sina.com.cn/s/blog_645919c40100gm1k.html
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace mycamera
{
public partial class Form1 : Form
{
//定义常量
private const int WM_USER = 0x400;
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
private int hHwnd;
private const int port = 2000;
public Form1()
{
InitializeComponent();
}
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;
}
public delegate bool CallBack(int hwnd, int lParam);
//引用API AVICAP32调用摄像头
[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);
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenCapture();
}
private void button2_Click(object sender, EventArgs e)
{
closecapture();
}
private void button3_Click(object sender, EventArgs e)
{
cutimg();
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
cutavi();
}
private void button6_Click(object sender, EventArgs e)
{
stopavi();
}
//定义打开摄像头函数
private void OpenCapture()
{
//定义视频窗口的长宽
int intWidth = this.panel1.Width;
int intHeight = this.panel1.Height;
int intDevice = 0;
string refDevice = intDevice.ToString();
//创建视频窗口并得到句柄
hHwnd = Form1.capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 640, 480, this.panel1.Handle.ToInt32(), 0);
if (Form1.SendMessage(hHwnd, 0x40a, intDevice, 0) > 0)
{
Form1.SendMessage(this.hHwnd, 0x435, -1, 0);
Form1.SendMessage(this.hHwnd, 0x434, 0x42, 0);
Form1.SendMessage(this.hHwnd, 0x432, -1, 0);
Form1.SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);
}
else
{
Form1.DestroyWindow(this.hHwnd);
}
}
//定义关闭摄像头
private void closecapture()
{
Form1.SendMessage(this.hHwnd, 0x40b, 0, 0);
Form1.DestroyWindow(this.hHwnd);
}
//截图函数
private void cutimg()
{
try
{
Form1.SendMessage(this.hHwnd, 0x41e, 0, 0);
//从当前剪切板获取数据
IDataObject obj1 = Clipboard.GetDataObject();
//判断数据是否为BITMAP
if (obj1.GetDataPresent(typeof(Bitmap)))
{
//保存图像
Image image1 = (Image)obj1.GetData(typeof(Bitmap));
//实例化一个保存控件
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
SaveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
SaveFileDialog1.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files (*.*)|*.*";
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
image1.Save(SaveFileDialog1.FileName, ImageFormat.Bmp);
}
}
}
catch
{
}
}
//录像函数
private
void cutavi()
{//截取视频的流文件
string
path;
SaveFileDialog
SaveFileDialog2 = new SaveFileDialog();
SaveFileDialog2.FileName
= DateTime.Now.ToString("yyyyMMddhhmmss");
SaveFileDialog2.Filter
= "AVI Files(*.avi)|*.avi|All files (*.*)|*.*";
if
(SaveFileDialog2.ShowDialog() == DialogResult.OK)
{
path
= SaveFileDialog2.FileName;
}
else
{
return;
}
IntPtr
hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(this.hHwnd,
WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64());
SendMessage(this.hHwnd,
WM_CAP_SEQUENCE, 0, 0);
}
//停止录像函数
private
void stopavi()
{
SendMessage(this.hHwnd,
WM_CAP_STOP, 0, 0);
}