Simple camera capture to picture box not working on Win11

Gary Henson 1 Reputation point
2022-09-23T22:34:15.273+00:00

This code has worked for me before using Win11!!!
Snippet shows that I'm using Avicap32. I can run the simple .exe app on Win 8 and Win 10 and it works fine, showing my handsome face from the laptop camera in the picture box control on the form. (green active light is lit)
Any ideas why I just get a black picture now?
C#. .Net v4.8, VS 2022 v17.3.4

   public void Attach(System.Windows.Forms.PictureBox control)  
    {  
        deviceHandle = Avicap32.capCreateCaptureWindow("", Constants.WS_VISIBLE | Constants.WS_CHILD,  
                                                        0, 0, 0, 0,  
                                                        control.Handle, 1);  
        if (User32.SendMessage(deviceHandle, Constants.WM_CAP_DRIVER_CONNECT, (IntPtr)deviceNumber, (IntPtr)0).ToInt32() > 0)  
        {  
            User32.SendMessage(deviceHandle, Constants.WM_CAP_SET_PREVIEWRATE, (IntPtr)66, (IntPtr)0);  
            User32.SendMessage(deviceHandle, Constants.WM_CAP_SET_SCALE, (IntPtr)(-1), (IntPtr)0);  
            User32.SendMessage(deviceHandle, Constants.WM_CAP_SET_PREVIEW, (IntPtr)(-1), (IntPtr)0);  
            User32.SetWindowPos(deviceHandle, (IntPtr)(0), 0, 0, control.Width, control.Height, 6);  
        }  
    }  

I'd use DirectShow but I cannot find EXACTLY how to load and install it. I downloaded Win App SDK, but I don't see DS anywhere!
Thanks to all and any help.

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-09-24T05:30:33.147+00:00

    I'd use DirectShow but I cannot find EXACTLY how to load and install it.

    There is nothing to install, just P/Invoke for interfaces

    I had posted a sample in this thread : Winforms add in to connect to USB camera?
    (on Mar 14 2021 at 9:47 AM)
    (tested on Windows 10 21H1, but it should work on Windows 11)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.