博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 操作摄像头
阅读量:5261 次
发布时间:2019-06-14

本文共 2591 字,大约阅读时间需要 8 分钟。

 public partial class Form1 : Form

    {

        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 = 1073741824;

        const int WS_VISIBLE = 268435456;

        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, short 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 = 320;

            int iWidth = 200;

            //

            //  Open Preview window in picturebox

            //

            hHwnd = capCreateCaptureWindowA(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, 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 = int.Parse(device_number_textBox.Text);

            OpenPreviewWindow();

        }

    }

转载于:https://www.cnblogs.com/fang8206/archive/2012/08/29/2662319.html

你可能感兴趣的文章
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Linux中防火墙centos
查看>>
mysql新建用户,用户授权,删除用户,修改密码
查看>>
FancyCoverFlow
查看>>
JS博客
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
283. Move Zeroes把零放在最后面
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
Python 数据类型
查看>>
S5PV210根文件系统的制作(一)
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
数据清洗
查看>>