Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Silverlight Tip: Using LINQ to Select the Largest Available Webcam Resolution

Pete Brown - 15 June 2010

Sometimes in Silverlight, you just want to get the largest possible webcam resolution. This snippet shows how to obtain that using LINQ.

if (CaptureDeviceConfiguration.AllowedDeviceAccess || 
    CaptureDeviceConfiguration.RequestDeviceAccess())
{
    var camera = 
        CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

    if (camera != null)
    {
        var source = new CaptureSource();

        var format = (from VideoFormat f in camera.SupportedFormats
                      orderby f.PixelWidth * f.PixelHeight descending
                      select f).FirstOrDefault<VideoFormat>();
        if (format != null)
            camera.DesiredFormat = format;

        source.VideoCaptureDevice = camera;

        VideoBrush videoBrush = new VideoBrush();
        videoBrush.Stretch = Stretch.None;
        videoBrush.SetSource(source);
        PresentationSurface.Fill = videoBrush; // PresentationSurface is a rectangle

        source.Start();
    }

}

That will pick the format with the highest total pixel count. You can modify the statement to pick just the largest width, or the largest size that will fit within a given box, etc.

Here's the screen shot in all its 720p webcam awesomeness, running in a Silverlight 4 out-of-browser application. (Took this photo at 3:30 am, and the tank light is off, so you can't see the lovely fish)

image

(But if you look closely, you'll see the Argo/Yamato sitting on top of the C128 monitor. heh)

PS. Yes, those are uuuugly cabinets. Old 1979 kitchen cabinets, removed from the kitchen and installed down here by the previous owner. Rey Bango gives me grief over them every time we're on a video call :)

   
posted by Pete Brown on Tuesday, June 15, 2010
filed under:    

6 comments for “Silverlight Tip: Using LINQ to Select the Largest Available Webcam Resolution”

  1. Rene Schultesays:
    I used this before in my webcam apps and SLARToolkit examples to get a lower (faster) resolution of 320x240. Some users reported problems with their Macs. I found out that the SupportedFormats collection was empty, so Silverlight did not return the supported formats. That's why I changed the code to use the DesiredFormat with the VideoFormat ctor:
    captureDevice.DesiredFormat = new VideoFormat(PixelFormatType.Unknown, 320, 240, 30);
    This works and selects 320x240 as video format. The default of most webcams is 640x480. I verified this with the size of a capturing result (WriteableBitmap).

    The DesiredFormat property is pretty clever and picks the nearest available VideoFormat if the exact format isn't supported. So if you simply set it to VideoFormat(PixelFormatType.Unknown, 4096, 4096, 30), it should always select the highest resolution. It
    It's just one line of code and always works on Macs.
  2. Adolfito121says:
    Is truly interesting read discussions between programmers.

    Thanks for your contributions (and also thanks to René for his real time edge detection example).

    Is there any way to also force webcam to work in black and white mode?Ç

    Thanks.

Comment on this Post

Remember me