首页 > 解决方案 > c#,winforms中如何将手机摄像头或其他摄像头连接到图片框?

问题描述

从过去1个月开始学习c#,我有一个问题,是否可以在图片框中连接并显示手机摄像头或其他网络摄像头。,在Windows窗体的c#.net框架中。提前致谢

标签: c#.netwinforms

解决方案


有几种与相机通信的方法。

我使用此代码与相机进行通信。

public class CameraAPI
{
    public bool IsAvailable { get; set; }

    [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 cbName, byte[] lpszVer, int cbVer);
    [DllImport("User32.dll")]
    public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, long lParam);
    [DllImport("User32.dll")]
    public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, long lParam);

    [System.Runtime.InteropServices.DllImport("user32")]
    public static extern bool DestroyWindow(IntPtr hWnd);

    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_PREVIEWFORMAT = WM_USER + 45;
    public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
    public const int WM_CAP_START = WM_USER;
    public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
    public const int WM_CAP_EDIT_COPY = (WM_CAP_START + 30);


    private IntPtr hWnd;
    private IntPtr mControlPtr;
    private int mWidth;
    private int mHeight;

    public CameraAPI(IntPtr handel, int width, int height)
    {
        mControlPtr = handel; //handle of video dom
        mWidth = width; //video width
        mHeight = height; //video height
    }

    public void StartPreviewWebcam()
    {
        if (hWnd != null)
            DestroyWindow(hWnd);

        byte[] lpszName = new byte[100];
        byte[] lpszVer = new byte[100];
        capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 0);
        hWnd = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
        if (SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, 0, 0))
        {
            SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 100, 0);
            SendMessage(hWnd, WM_CAP_SET_PREVIEW, true, 0);
            IsAvailable = true;
        }
        else
        {
            IsAvailable = false;
        }
    }

    public void CloseWebcam()
    {
        SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
        IsAvailable = false;
    }

    public void SavePictureByPath(string path)
    {
        IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
        SendMessage(hWnd, WM_CAP_SAVEDIB, 0, hBmp.ToInt64());
    }
    
    public byte[] TakePicture()
    {
        byte[] imgByteArray = null;
        var path = Application.StartupPath + @"\Image.png";
        try
        {
            DeleteExist(path);
            SavePictureByPath(path);
           
            if (File.Exists(path))
            {
                using (Image img = Image.FromFile(path))
                {
                    imgByteArray = ImageToByteArray(img);
                }
            }
        }
        catch (Exception exp)
        {
            var a = 1;
        }
        finally
        {
            DeleteExist(path);
        }


        return imgByteArray;
    }
    
    public void DeleteExist(string path)
    {
        if (File.Exists(path))
        {
            File.Delete(path);
        }
    }
    
    public byte[] ImageToByteArray(System.Drawing.Image imageIn)
    {
        using (var ms = new MemoryStream())
        {
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
    }
}

推荐阅读