using System;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public class VideoCapture
{
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
[MarshalAs(UnmanagedType.I4)] public Int32 biSize ;
[MarshalAs(UnmanagedType.I4)] public Int32 biWidth ;
[MarshalAs(UnmanagedType.I4)] public Int32 biHeight ;
[MarshalAs(UnmanagedType.I2)] public short biPlanes;
[MarshalAs(UnmanagedType.I2)] public short biBitCount ;
[MarshalAs(UnmanagedType.I4)] public Int32 biCompression;
[MarshalAs(UnmanagedType.I4)] public Int32 biSizeImage;
[MarshalAs(UnmanagedType.I4)] public Int32 biXPelsPerMeter;
[MarshalAs(UnmanagedType.I4)] public Int32 biYPelsPerMeter;
[MarshalAs(UnmanagedType.I4)] public Int32 biClrUsed;
[MarshalAs(UnmanagedType.I4)] public Int32 biClrImportant;
}
[StructLayout(LayoutKind.Sequential)] public struct BITMAPINFO
{
[MarshalAs(UnmanagedType.Struct, SizeConst=40)]
public BITMAPINFOHEADER bmiHeader;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public Int32[] bmiColors;
}
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int SWP_SHOWWINDOW = 0x0040;
private const int WM_CAP_START = 0x0400;
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_SET_PREVIEWRATE = (WM_CAP_START+52);
private const int WM_CAP_SET_PREVIEW = (WM_CAP_START+50);
private const int WM_CAP_GRAB_FRAME = (WM_CAP_START+60);
private const int WM_CAP_GET_VIDEOFORMAT = (WM_CAP_START+44);
private IntPtr hCapture=(IntPtr) 0;
private int width=0;
private int height=0;
public bool CreateCaptureWindow(IntPtr hParent, int x, int y)
{
hCapture = capCreateCaptureWindow("",
WS_CHILD | WS_VISIBLE,
x, y, 32, 24, hParent, 0);
if ((int) hCapture==0) {return false;}
SendMessage(hCapture, WM_CAP_DRIVER_CONNECT, 0, 0);
GetVideoFormat();
SetWindowPos(hCapture, 0, x, y, width, height,
SWP_SHOWWINDOW);
return true;
}
public void ReleaseCaptureWindow()
{
if ((int) hCapture != 0)
{
SendMessage(hCapture, WM_CAP_DRIVER_DISCONNECT, 0, 0);
DestroyWindow(hCapture);
hCapture= (IntPtr) 0;
}
}
public void StartPreview()
{
SendMessage(hCapture, WM_CAP_SET_PREVIEWRATE, 15, 0);
SendMessage(hCapture, WM_CAP_SET_PREVIEW, 1, 0);
}
public void StopPreview()
{
SendMessage(hCapture, WM_CAP_SET_PREVIEW, 0, 0);
}
public void GetFrame()
{
SendMessage(hCapture, WM_CAP_GRAB_FRAME, 0, 0);
}
private void GetVideoFormat()
{
BITMAPINFO buffer = new BITMAPINFO();
int size = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
buffer.bmiHeader.biSize = size;
SendMessage(hCapture, WM_CAP_GET_VIDEOFORMAT,
size, ref buffer);
width = buffer.bmiHeader.biWidth;
height = buffer.bmiHeader.biHeight;
}
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd,
int wMsg, int wParam, ref BITMAPINFO lParam);
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd,
int wMsg, int wParam, int lParam);
[DllImport("User32.dll")]
private static extern int SetWindowPos(IntPtr hWnd,
int hWndInsertAfter, int x, int y,
int cx, int cy, int wFlags);
[DllImport("user32.dll")]
private static extern int DestroyWindow(IntPtr hwnd);
[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindow
(string lpszWindowName, int dwStyle,
int x, int y, int nWidth, int nHeight,
IntPtr hWnd, int nID);
}
}
|