C # Open the camera and grab the photo and exit

Source: Internet
Author: User

In fact, the camera in the background grab a picture, can be used to monitor
1. [Code][c#] Code
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Diagnostics;


Namespace Myvideoservice
{
    class program
    {
        
        static void Main (string[] args)
        {
             Cvideo video = new Cvideo (Process.getcurrentprocess (). Mainwindowhandle, 379, 316);
            video. Startwebcam ();
            video. Grabimage (Process.getcurrentprocess (). Mainwindowhandle, "d:\\b.jpg");
            video. Closewebcam ();
       }
   }
}


2. [Code][c#] Code
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;
Using System.Drawing.Imaging;
Using System.Drawing;
Using System.IO;

Namespace Myvideoservice
{
public class VIDEOAPI//Video API Class
{
Video API calls
[DllImport ("Avicap32.dll")]
public static extern IntPtr Capcreatecapturewindowa (byte[] lpszwindowname, int dwstyle, int x, int y, int nwidth, int Nhei Ght, IntPtr hwndparent, int nID);
[DllImport ("Avicap32.dll")]
public static extern bool Capgetdriverdescriptiona (short wdriver, byte[] lpszName, int cbname, byte[] lpszver, int cbver);
[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);
Constant
public const int wm_user = 0x400;
public const int ws_child = 0x40000000;
public const int ws_visible = 0x10000000;
public const int swp_nomove = 0x2;
public const int swp_nozorder = 0x4;
public const int Wm_cap_driver_connect = Wm_user + 10;
public const int Wm_cap_driver_disconnect = Wm_user + 11;
public const int Wm_cap_set_callback_frame = Wm_user + 5;
public const int Wm_cap_set_preview = Wm_user + 50;
public const int wm_cap_set_previewrate = Wm_user + 52;
public const int Wm_cap_set_videoformat = Wm_user + 45;
public const int wm_cap_start = Wm_user;
public const int WM_CAP_SAVEDIB = Wm_cap_start + 25;
}
public class Cvideo//Video class
{
Private INTPTR LWNDC; To save an unsigned handle
Private INTPTR mcontrolptr; Save Management indicators
private int mwidth;
private int mheight;
Public Cvideo (IntPtr handle, int width, int height)
{
Mcontrolptr = handle; To display a handle to a video control
Mwidth = width; Video width
Mheight = height; Video height
}
<summary>
Turn on the video device
</summary>
public void Startwebcam ()
{
byte[] LpszName = new byte[100];
byte[] Lpszver = new byte[100];
Videoapi.capgetdriverdescriptiona (0, LpszName, lpszver, 100);
THIS.LWNDC = Videoapi.capcreatecapturewindowa (lpszName, Videoapi.ws_child | videoapi.ws_visible, 0, 0, mwidth, mheight, mcontrolptr, 0);
if (Videoapi.sendmessage (LWNDC, videoapi.wm_cap_driver_connect, 0, 0))
{
Videoapi.sendmessage (LWNDC, videoapi.wm_cap_set_previewrate, 100, 0);
Videoapi.sendmessage (LWNDC, Videoapi.wm_cap_set_preview, True, 0);
}
}
<summary>
Turn off the video device
</summary>
public void Closewebcam ()
{
Videoapi.sendmessage (LWNDC, videoapi.wm_cap_driver_disconnect, 0, 0);
}
<summary>
Photo
</summary>
<param name= "path" > Path to save the JPEG file </param>
public void Grabimage (IntPtr hwndc, string path)
{
IntPtr hbmp = marshal.stringtohglobalansi (path);

Videoapi.sendmessage (LWNDC, Videoapi.wm_cap_savedib, 0, Hbmp.toint32 ());

Bitmap image = new Bitmap (path);
if (path. Contains ("BMP"))
{
Image. Save (path. Replace ("BMP", "JPG"), imageformat.jpeg);
Image. Dispose ();
if (System.IO.File.Exists (path))
{
Try
{
System.IO.File.Delete (path);
}
catch (System.IO.IOException e)
{
Return
}
}
else {
Image. Save (path, imageformat.jpeg);
Image. Dispose ();
}

}

}
}

}

C # Open the camera and grab the photo and exit

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.