c# 简单打开关闭摄像头
阅读原文时间:2023年07月12日阅读:1
    const short WM\_CAP = 1024;  
    const int WM\_CAP\_DRIVER\_CONNECT = WM\_CAP + 10;  
    const int WM\_CAP\_DRIVER\_DISCONNECT = WM\_CAP + 11;  
    const int WM\_CAP\_EDIT\_COPY = WM\_CAP + 30;  
    const int WM\_CAP\_SET\_PREVIEW = WM\_CAP + 50;  
    const int WM\_CAP\_SET\_PREVIEWRATE = WM\_CAP + 52;  
    const int WM\_CAP\_SET\_SCALE = WM\_CAP + 53;  
    const int WS\_CHILD = 0x40000000;  
    const int WS\_VISIBLE = 0x10000000;  
    const short SWP\_NOMOVE = 2;  
    const short SWP\_NOSIZE = 1;  
    const short SWP\_NOZORDER = 4;  
    const short HWND\_BOTTOM = 1;  
    int iDevice = 0;  
    int hHwnd;

    \[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA")\]  
    static extern int SendMessage(int hwnd, int wMsg, int wParam, \[MarshalAs(UnmanagedType.AsAny)\]  
        object lParam);  
    \[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")\]  
    static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);  
    \[System.Runtime.InteropServices.DllImport("user32")\]  
    static extern bool DestroyWindow(int hndw);  
    \[System.Runtime.InteropServices.DllImport("avicap32.dll")\]  
    static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);  
    \[System.Runtime.InteropServices.DllImport("avicap32.dll")\]  
    static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);

    private void OpenPreviewWindow()  
    {

        int iHeight = 600;  
        int iWidth = 800;

        //  
        //  Open Preview window in picturebox  
        //  
        hHwnd = capCreateCaptureWindowA(iDevice.ToString(), (WS\_VISIBLE | WS\_CHILD), 0, 0, iWidth, iHeight, picCapture.Handle.ToInt32(), 0);

        //  
        //  Connect to device  
        //  
        if (SendMessage(hHwnd, WM\_CAP\_DRIVER\_CONNECT, iDevice, 0) == 1)  
        {  
            //  
            // Set the preview scale  
            //  
            SendMessage(hHwnd, WM\_CAP\_SET\_SCALE, 1, 0);  
            //  
            // Set the preview rate in milliseconds  
            //  
            SendMessage(hHwnd, WM\_CAP\_SET\_PREVIEWRATE, 66, 0);  
            //  
            // Start previewing the image from the camera  
            //  
            SendMessage(hHwnd, WM\_CAP\_SET\_PREVIEW, 1, 0);  
            //  
            //  Resize window to fit in picturebox  
            //  
            SetWindowPos(hHwnd, HWND\_BOTTOM, 0, 0, iWidth, iHeight, (SWP\_NOMOVE | SWP\_NOZORDER));  
        }  
        else  
        {  
            //  
            //  Error connecting to device close window  
            //  
            DestroyWindow(hHwnd);  
        }  
    }

    private void ClosePreviewWindow()  
    {  
        //  
        //  Disconnect from device  
        //  
        SendMessage(hHwnd, WM\_CAP\_DRIVER\_DISCONNECT, iDevice, 0);

        //  
        //  close window  
        //  
        DestroyWindow(hHwnd);  
    }

    private void button1\_Click(object sender, EventArgs e)  
    {

        iDevice = 0;// int.Parse(device\_number\_textBox.Text);

        OpenPreviewWindow();

    }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章