How to list camera available video resolution

Amer Sawan picture Amer Sawan · Sep 21, 2011 · Viewed 11k times · Source

if I have more than one camera attached to my PC ... I want to know the best available resolutions for a specific camera ...

for example some cameras are HD or FullHD (1,280×720 pixels (720p) or 1,920×1,080 pixels (1080i/1080p)) or the most common are web cameras....

I want to know at least the best video mode that the camera work properly...(the mode that the camera made to work with)

my work is on WPF using C# (I am using Directshow)

thanks in advance

Answer

Amer Sawan picture Amer Sawan · Jan 7, 2013

This is a code that I wrote, its working perfectly for me

public static List<Point> GetAllAvailableResolution(DsDevice vidDev)
{
    try
    {
        int hr;
        int max = 0;
        int 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<Point>();
        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();
                    max = 0;
                    bitCount = v.BmiHeader.BitCount;
                }
                AvailableResolutions.Add(new Point(v.BmiHeader.Width, v.BmiHeader.Height));
                if (v.BmiHeader.Width > max || v.BmiHeader.Height > max)
                    max = (Math.Max(v.BmiHeader.Width, v.BmiHeader.Height));
            }
            hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
        }
        return AvailableResolutions;
    }

    catch (Exception ex)
    {
        Log(ex);
        return new List<Point>();
    }
}

(E.g. this can be added to VideoCaptureElement in WPF-MediaKit)