title: Web cam image capture
body: <p>I am trying to write an application to capture an image from a webcam. I have written a C++ console application and whilst I can get an image, I have difficulty setting the resolution, I want to get higher resolution images and would like to be able to get 4k from a logitech brio webcam for example.
  On Windows 10 with a lower resolution usb camera I was able to get 1280x720. On Windows 11 with a 4k USB camera I am only getting 640x480</p>
  <p>Here is my code:</p>
  <pre><code>#include &quot;vfw.h&quot;
  #include &lt;cstdio&gt;
  #include &lt;cstdint&gt;
  
  #pragma comment(lib, &quot;Vfw32.lib&quot;)
  
  int main(int argc, char** argv)
  {
  
      char device_name[100];
      char filename[100];
  
      size_t sz = 0;
      char* userpath = nullptr;
  
      strcpy_s(device_name, &quot;&quot;);
      strcpy_s(filename, userpath);
      strncat_s(filename, &quot;\\Documents\\image.bmp&quot;, 20);
  
    // create the preview window 
    HWND hCam = capCreateCaptureWindow (
     &quot;FMCamCapture&quot;,
     WS_CHILD | WS_VISIBLE,
     0, 0, 1024, 768,
     ::GetDesktopWindow(), 0);
  
    CAPDRIVERCAPS CapDrvCaps;
    capDriverGetCaps(hCam, &amp;CapDrvCaps, sizeof(CAPDRIVERCAPS));
  
    int driver_number = 0;
    int n = 1;
    //Driver argument
    while (n &lt; argc)
    {
  
        if (strcmp(argv[n], &quot;/drivernum&quot;) == 0)
        {
            // Set device number to specified value
            if (++n &lt; argc) {
                driver_number = atoi(argv[n]);
            }
            else printf(&quot;Error: invalid device number&quot;);
            if (driver_number &lt;= 0)
                printf(&quot;Error: invalid device number&quot;);
        }
  
        if (strcmp(argv[n], &quot;/showdrivers&quot;) == 0)
        {
            // Show the list of available drivers and pause and exit
            TCHAR  szDeviceName[80];
            TCHAR  szDeviceVersion[80];
  
            for (int wIndex = 0; wIndex &lt; 10; wIndex++)
            {
                if (capGetDriverDescription(
                    wIndex,
                    szDeviceName,
                    sizeof(szDeviceName),
                    szDeviceVersion,
                    sizeof(szDeviceVersion)
                ))
                {
                    printf(&quot;\nEach Driver may represent a different camera. The default driver is 0. Set a differnt driver to change cameras.\nAvailable Drivers:\n\%i - %s\n&quot;, wIndex, szDeviceName);
                    // Append name to list of installed capture drivers
                    // and then let the user select a driver to use.
                }
            }
            system(&quot;pause&quot;);
            return 0;
        }
  
        n++;
    }
    fprintf(stderr, &quot;Saved file location: %s\n&quot;, filename);
  
    CAPTUREPARMS captureParms;
  
    BITMAPINFO bMInfo;
    // connect to the first Driver 
    // for other cameras try index 
    // 1, 2, in place of the 0 below 
    if(capDriverConnect(hCam, driver_number))
    {
  
        capGetVideoFormat(hCam, &amp;bMInfo, sizeof(BITMAPINFO));
        //printf(&quot;height %ld&quot;, bMInfo.bmiHeader.biHeight);
        //printf(&quot;width %ld&quot;, bMInfo.bmiHeader.biWidth);
        //printf(&quot;bisize %lu&quot;, bMInfo.bmiHeader.biSizeImage);
        //printf(&quot;compression %lu&quot;, bMInfo.bmiHeader.biCompression);
  
        bMInfo.bmiHeader.biWidth = 1280;
        bMInfo.bmiHeader.biHeight = 720;
        bMInfo.bmiHeader.biSizeImage = 0;
        //bMInfo.bmiHeader.biSizeImage = (bMInfo.bmiHeader.biHeight) * (((bMInfo.bmiHeader.biWidth * 2) * bMInfo.bmiHeader.biBitCount + 31) / 32) * 4;
        bMInfo.bmiHeader.biCompression = BI_RGB;
  
        if (capSetVideoFormat(hCam, &amp;bMInfo, sizeof(BITMAPINFO))) {
            //printf(&quot;set res success setvidformat&quot;);
        }
  
      capFileSaveDIB(hCam, filename);
  
      //printf(&quot;Saved!&quot;);
      capDriverDisconnect(hCam);
    }
  
    else 
    {
  
      printf(&quot;Check camera?&quot;);
  
    }
  
    DestroyWindow(hCam);
  
    return 0;
  
  }```
  </code></pre>