win32API to capture an image

0

I've written a C# application to capture an image from a web cam. The app uses win32 API calls and works fine on my win 10 lenovo laptop and similarly with colleagues Lenovo laptops of various models and OS's (Win 7 and Win 10).

When I try to run the app on a lenovo Thinkcenter with an integrated camera, I get a black screen (No exceptions thrown). The camera app on the Thinkcenter that comes with Win 10 works fine as do 3rd party apps I installed to verify that the camera works and that any security policies are not preventing my application from accessing the camera.

The drivers (realtek) are up to date and I've tried the generic windows drivers also but my app still gives me a black screen on this Thinkcenter pc.

The led comes on, telling me that the there is some interaction with the hardware but no image.

I've also disabled the integrated camera and plugged in a usb camera and this works. So it appears that whatever the issue is, is specific to the camera (and my code).

Being new to Win32API programming, I'm wondering how to approach troubleshooting this problem. I'd appreciate any suggestions or pointers anyone might have?

public partial class Window1 : MetroWindow
{

    IntPtr deviceHandle;       
    public const uint WM_CAP_DRIVER_CONNECT = 0x40a;
    public const uint WM_CAP_SET_PREVIEW = 0x432;
    public const uint WM_CAP_SET_PREVIEWRATE = 0x434;
    public const uint WM_CAP_SET_SCALE = 0x435;
    public const uint WS_CHILD = 0x40000000;
    public const uint WS_VISIBLE = 0x10000000;      

    [DllImport("avicap32.dll")]
    public extern static IntPtr capCreateCaptureWindow(string title, uint style, int x, int y, int width, int height, IntPtr window, int id);

    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll")]
    extern static IntPtr GetClipboardData(uint uFormat);  

    public Window1()
    {
        InitializeComponent();           
    }

    public void GetCamera()
    {           
        deviceHandle = capCreateCaptureWindow(string.Empty, WS_VISIBLE | WS_CHILD, 0, 0, (int)ImgWebCam.Width, (int)ImgWebCam.Height, new WindowInteropHelper(this).Handle, 0);

        if (SendMessage(deviceHandle, WM_CAP_DRIVER_CONNECT, (IntPtr)0, (IntPtr)0).ToInt32() > 0)
        {
            SendMessage(deviceHandle, WM_CAP_SET_SCALE, (IntPtr)(-1), (IntPtr)0);
            SendMessage(deviceHandle, WM_CAP_SET_PREVIEWRATE, (IntPtr)0x42, (IntPtr)0);
            SendMessage(deviceHandle, WM_CAP_SET_PREVIEW, (IntPtr)(-1), (IntPtr)0);
            SetWindowPos(deviceHandle, new IntPtr(0), 0, 0, (int)ImgWebCam.Width, (int)ImgWebCam.Height, 6);
        }
    }

    private void Start(object sender, RoutedEventArgs e)
    {
        //StartQRScanning();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        GetCamera();            
    }       
}
api
winapi
camera
asked on Stack Overflow Mar 11, 2019 by Tom J • edited Mar 12, 2019 by Dale K

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0