ShowVideo.cs code:
Namespace Webcam {Using System.Runtime.InterOpServices;
Public class showvideo {// methods public showvideo () {}
[DllImport ( "avicap32.dll")] public static extern IntPtr capCreateCaptureWindowA (byte [] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DLLIMPORT ("Avicap32.dll")] Public Static Extern Bool CapGetDriverDescriptiona (Short WDRIVER, BYTE [] lpszname, int cname, byte [] lpszver, int cbver;
[DLLIMPORT ("Avicap32.dll")] Public Static Extern Int CapGetVideoFormat (INTPTR HWND, INTPTR PSVideoFormat, int wsize);
Public Static Void Copy (int Ptr, Byte [] DATA) {ShowVideo.copy (New INTPTR (PTR), DATA);
Public Static Void Copy (INTPTR PTR, BYTE [] DATA) {Marshal.copy (PTR, DATA, 0, DATA.LENGTH);}
Public Static Object GetStructure (INT PTR, VALUETYPE STRUCTURE) {Return ShowVideo.getstructure (New INTPTR (PTR), Structure);}
Public Static Object GetStructure (INTPTR PTR, VALUETYPE STRUCTURE) {Return Marshal.PTRTRUCTRUCTURE (PTR, Structure.gettype ());}
[DLLIMPORT ("User32.dll")] Public Static Extern Bool SendMessage (INTPTR HWND, INT WMSG, BOOL WPARAM, INT LPARAM);
[DLLIMPORT ("User32.dll")] Public Static Extern Bool SendMessage (INTPTR HWND, INT WMSG, SHORT WPARAM, INT LPARAM);
[DLLIMPORT ("User32.dll")] Public Static Extern Bool SendMessage (INTPTR HWND, INT WMSG, INT WPARAM, REF BitmapInfo LPARAM);
[DLLIMPORT ("User32.dll")] Public Static Extern Int setWindowPos (INTPTR HWND, INT HWNDINSERTAFTER, INT X, INT Y, INT CX, INT CY, INT WFLAGS);
public static int SizeOf (object structure) {return Marshal.SizeOf (structure);} // Nested Types [StructLayout (LayoutKind.Sequential)] public struct BITMAPINFO {[MarshalAs (UnmanagedType.Struct)] public showVideo.BITMAPINFOHEADER bmiHeader; [MarshalAs (UnmanagedType.ByValArray, SizeConst = 0x400)] public int [] bmiColors;} [StructLayout (LayoutKind.Sequential)] public struct BITMAPINFOHEADER {[MarshalAs (UnmanagedType.I4)] public int biSize; [MarshalAs (UnmanagedType.I4)] public int biWidth; [MarshalAs (UnmanagedType.I4)] public int biHeight; [MarshalAs (UnmanagedType.I2)] public short biPlanes; [MarshalAs (UnmanagedType.I2)] public short biBitCount; [MarshalAs (UnmanagedType.I4)] public int biCompression ; [MarshalAs (UnmanagedType.I4)] public int biSizeImage; [MarshalAs (UnmanagedType.I4)] public int biXPelsPerMeter; [MarshalAs (UnmanagedType.I4)] public int biYPelsPerMeter; [MarshalAs (UnmanagedType.I4)] public int biClrUsed; [ Marshal As (unmanagedtype.i4)] public int biclrimport;
[StructLayout (LayoutKind.Sequential)] public struct VIDEOHDR {[MarshalAs (UnmanagedType.I4)] public int lpData; [MarshalAs (UnmanagedType.I4)] public int dwBufferLength; [MarshalAs (UnmanagedType.I4)] public int dwBytesUsed; [MarshalAs (UnmanagedType.I4)] public int dwTimeCaptured; [MarshalAs (UnmanagedType.I4)] public int dwUser; [MarshalAs (UnmanagedType.I4)] public int dwFlags; [MarshalAs (UnmanagedType.ByValArray, SizeConst = 4)] public int [] dwreserved;}}}
Webcamera.cs code:
Namespace Webcam {Using System;
public class WebCamera {// Methods public WebCamera (IntPtr handle, int width, int height) {this.mControlPtr = handle; this.mWidth = width; this.mHeight = height;} private bool capDriverConnect (IntPtr lwnd, short i) { Return ShowVideo.sendMessage (LWND, 0x40a, i, 0);
Private Bool Capdriverdisconnect (INTPTR LWND) {Return ShowVideo.sendMessage (LWND, 0x40B, (Short) 0, 0);}
Private Bool Cappreview (INTPTR LWND, BOOL F) {Return ShowVideo.sendMessage (LWND, 0x432, F, 0);
Private Bool CappreviewRate (INTPTR LWND, SHORT WMS) {Return ShowVideo.sendMessage (LWND, 0x434, WMS, 0);
private bool capSetVideoFormat (IntPtr hCapWnd, ref showVideo.BITMAPINFO BmpFormat, int CapFormatSize) {return showVideo.SendMessage (hCapWnd, 0x42d, CapFormatSize, ref BmpFormat);}
Public void closewebcam () {this.capdriverdisconnect (this.lwndc);}
public void StartWebCam () {byte [] buffer1 = new byte [100]; byte [] buffer2 = new byte [100]; showVideo.capGetDriverDescriptionA (0, buffer1, 100, buffer2, 100); this.lwndC = showVideo.capCreateCaptureWindowA (buffer1, 1342177280, 0, 0, this.mWidth, this.mHeight, this.mControlPtr, 0); if (this.capDriverConnect (this.lwndC, 0)) {showVideo.BITMAPINFO bitmapinfo1; this.capPreviewRate (this.lwndC , 0x42); this.capPreview (this.lwndC, true); bitmapinfo1 = new showVideo.BITMAPINFO (); bitmapinfo1.bmiHeader.biSize = showVideo.SizeOf (bitmapinfo1.bmiHeader); bitmapinfo1.bmiHeader.biWidth = 320; bitmapinfo1.bmiHeader .biHeight = 240; bitmapinfo1.bmiHeader.biPlanes = 1; bitmapinfo1.bmiHeader.biBitCount = 0x18; this.capSetVideoFormat (this.lwndC, ref bitmapinfo1, showVideo.SizeOf (bitmapinfo1)); showVideo.SetWindowPos (this.lwndC, 0, 0, 0, this.mwidth, this.mHeight, 6);}} // Fields Private INTPTPTR LWNDC; Private INTPTR MCONTROLPTR; Private Int MHEIGHT; priv Ate int mwidth;}}
FORM1.CS code:
Namespace Webcam {Using System.Componentmodel; Using System.drawing; Using System.Windows.Forms;
Public class form1: form {// methods public form1 () {this.components = null; this.initializeComponent ();
Private void b_stop_click (object sender, eventargs e) {this.b_play.enabled = true; this.b_stop.enabled = false; this.closeWebcam ();
private void button1_Click (object sender, EventArgs e) {this.b_play.Enabled = false; this.b_stop.Enabled = true; this.panelPreview.Size = new Size (320, 240); this.wc = new WebCamera (this. panelPreview.Handle, this.panelPreview.Width, this.panelPreview.Height); this.wc.StartWebCam ();!} protected override void Dispose (bool disposing) {if (disposing && (this.components = null)) {this .components.dispose ();} base.dispose (disposing);
Private Void Form1_Load (Object Sender, Eventargs E) {this.b_play.enabled = false; this.b_stop.enabled = true; this.panelpreview.size = new size (320, 240); this.wc = new Webcamera (this. Paneseelpreview.handle, this.Panelpreview.width, this.Panelpreview.height); this.wc.startwebcam ();
private void InitializeComponent () {this.b_play = new System.Windows.Forms.Button (); this.panelPreview = new System.Windows.Forms.Panel (); this.b_stop = new System.Windows.Forms.Button () This.suspendlayout (); // / b_play // this.b_play.location = new system.drawing.point (83, 264); this.b_play.name = "b_play"; this.b_play.tabindex = 0; THIS.B_PLAY.TEXT = "& play"; this.b_play.click = new system.eventhandler (this.button1_click); /// Panelpreview // this.PanelPreview.Location = new system.drawing.point (0, 8 ); This.PanelPreview.Name = "panelpreview"; this.Panelpreview.size = new system.drawing.size (320, 240); this.panelpreview.tabindex = 1; // // b_stop // this.b_stop.enabled = false; this.b_stop.location = new system.drawing.point (163, 264); this.b_stop.name = "b_stop"; this.b_stop.tabindex = 2; this.b_stop.text = "& stop"; this .b_stop.click = new system.eventhandler (this.b_stop_click); // // Form1 // this.Autos Calebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (320, 310); this.Controls.add (this.b_stop); this.controls.add (this.Panelpreview ); This.Controls.add (this.b_play); this.maximizebox = false; this.minimizebox = false; this.name = "form1"; this.text = "Goodview test web Camera"; this.load = New System.EventHandler (this.form1_load); this.ResumeLayout (false);}
[Stathread] private static void main () {application.run (new form1 ());}