VideoCamera get supported resolutions

6.1k views Asked by At

I'm using DirectShow library (DirectShowLib-2005.dll) with C# to capture a video camera image. When I'm definig the capture object's size, I have these parameters:

const int VIDEOWIDTH = 640; // Depends on video device caps
const int VIDEOHEIGHT = 480; // Depends on video device caps
const int VIDEOBITSPERPIXEL = 24; // BitsPerPixel values determined by device

capture = new Capture(0, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, pictureBox1);

I'm using this loop for getting the names of devices. Can I somehow read every possible resolutions for each cam?

DsDevice[] capDevices;
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (capDevices.Length == 0)
{
  //Could not found camera
}
else
{
  for (var i = 0; i < capDevices.Length; i++)
  {
    myCamerName = capDevices[i].Name.ToString();
  }
} 
2

There are 2 answers

0
Roman Ryltsov On BEST ANSWER

The video source might be flexible to support any resolution you request (e.g. within certain bounds), so listing resolutions is not something indispensable for the filter operation. There are two methods to obtain resolutions suggested by the video source, resolutions it advertises support of:

  1. using IAMStreamConfig.GetStreamCaps method on the output (capture) pin of the filter. This is how MSDN suggests that developers implement it: Exposing Capture and Compression Formats and you can query the capabilities respectively. Note that an enumerated capability obtained this way might be not a specific resolution, but a range of supported resolutions for specific pixel format instead.

  2. Another method is to use IPin.EnumMediaTypes and list available media types (and resolutions).

See also:

0
K. Roland On

Searching around the web finally I found a code snippet, using the IPin.EnumMediaTypes

private List<string> GetAllAvailableResolution(DsDevice vidDev)
{
   try
   {
     int hr, bitCount = 0;

     IBaseFilter sourceFilter = null;

     var m_FilterGraph2 = new FilterGraph() as IFilterGraph2;
     hr = m_FilterGraph2.AddSourceFilterForMoniker(vidDev.Mon, null, vidDev.Name, out sourceFilter);
     var pRaw2 = DsFindPin.ByCategory(sourceFilter, PinCategory.Capture, 0);
     var AvailableResolutions = new List<string>();

     VideoInfoHeader v = new VideoInfoHeader();
     IEnumMediaTypes mediaTypeEnum;
     hr = pRaw2.EnumMediaTypes(out mediaTypeEnum);

     AMMediaType[] mediaTypes = new AMMediaType[1];
     IntPtr fetched = IntPtr.Zero;
     hr = mediaTypeEnum.Next(1, mediaTypes, fetched);

     while (fetched != null && mediaTypes[0] != null)
     {
       Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);
       if (v.BmiHeader.Size != 0 && v.BmiHeader.BitCount != 0)
       {
         if (v.BmiHeader.BitCount > bitCount)
         {
           AvailableResolutions.Clear();
           bitCount = v.BmiHeader.BitCount;
         }
         AvailableResolutions.Add(v.BmiHeader.Width +"x"+ v.BmiHeader.Height);
       }
       hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
     }
     return AvailableResolutions;
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message);
     return new List<string>();
   }
}