Simple camera programming code!

Source: Internet
Author: User

ShowVideo. cs code:

Namespace webcam
{
Using System;
Using System. Runtime. InteropServices;

Public class showVideo
{
// Methods
Public showVideo ()
{
}

[DllImport ("avicap32.dll")]
Public static extern IntPtr capcreatecapturew.wa (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 cbName, 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, 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, valuetype structure)
{
Return marshal. ptrtostructure (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
{
[Financialas (UnmanagedType. Struct)]
Public showVideo. BITMAPINFOHEADER bmiHeader;
[Financialas (UnmanagedType. ByValArray, SizeConst = 0x400)]
Public int [] bmiColors;
}

[StructLayout (LayoutKind. Sequential)]
Public struct BITMAPINFOHEADER
{
[Financialas (UnmanagedType. I4)]
Public int biSize;
[Financialas (UnmanagedType. I4)]
Public int biWidth;
[Financialas (UnmanagedType. I4)]
Public int biHeight;
[Financialas (UnmanagedType. I2)]
Public short biplanes;
[Financialas (unmanagedtype. I2)]
Public short bibitcount;
[Financialas (unmanagedtype. I4)]
Public int bicompression;
[Financialas (unmanagedtype. I4)]
Public int bisizeimage;
[Financialas (unmanagedtype. I4)]
Public int bixpelspermeter;
[Financialas (unmanagedtype. I4)]
Public int biypelspermeter;
[Financialas (unmanagedtype. I4)]
Public int biclrused;
[Financialas (unmanagedtype. I4)]
Public int biclrimportant;
}

[Structlayout (layoutkind. Sequential)]
Public struct videohdr
{
[Financialas (unmanagedtype. I4)]
Public int lpdata;
[Financialas (unmanagedtype. I4)]
Public int dwbufferlength;
[Financialas (unmanagedtype. I4)]
Public int dwbytesused;
[Financialas (unmanagedtype. I4)]
Public int dwtimecaptured;
[Financialas (unmanagedtype. I4)]
Public int dwuser;
[Financialas (unmanagedtype. I4)]
Public int dwflags;
[Financialas (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 bmp format, int CapFormatSize)
{
Return showVideo. SendMessage (hCapWnd, 0x42d, CapFormatSize, ref BMP format );
}

Public void CloseWebcam ()
{
This. capDriverDisconnect (this. lwndC );
}

Public void StartWebCam ()
{
Byte [] buffer1 = new byte [1, 100];
Byte [] buffer2 = new byte [1, 100];
ShowVideo. capGetDriverDescriptionA (0, buffer1, 100, buffer2, 100 );
This. lwndC = showVideo. capcreatecapturew.wa (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 IntPtr lwndC;
Private IntPtr mControlPtr;
Private int mHeight;
Private int mWidth;
}
}

Form1.cs code:

Namespace webcam
{
Using System;
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. WC. closewebcam ();
}

Private void button#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. panelPreview. 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. button#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. AutoScaleBaseSize = 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. form#load );
This. resumelayout (false );

}

[Stathread]
Private Static void main ()
{
Application. Run (New form1 ());
}

// Fields
Private button B _play;
Private Button B _stop;
Private Container components;
Private Panel panelPreview;
Private WebCamera wc;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.