Search code examples
c#xamlwindows-phone-8.1windows-store-apps

WP8.1RT: Custom resolution image taken from mediacapture C#


I'm having a little hard time after navigating around the site to find out solution. The problem I have is that I'm trying to capture an image using mediacapture taken from this url (download here).

I've found a couple threads in SO that deal with resolution already, but what they're doing is using default resolution as following

3024*4992.......4:3
1936*2592...162:121
1536*2048.......4:3
480*640..........4:3
3024*5376.....16:9
1728*3072.....16:9
1456*2592...162:91

(suggeted by this)

However what I want is to capture an image with 800x600 resolution, is this really possible somehow?


Solution

  • You can use 800x600 only if your camera supports it. My camera for example doesn't.

    Find available resolutions:

    uint[] x_res; // available horizontal resolutions
    uint[] y_res; // available vertical resolutions
    uint resolutionwidth; //used horizontal resolution
    uint resolutionheight; //used vertical resolution
    
    private void get_res_button_click(object sender, RoutedEventArgs e)
    {
        resolution_listbox.Items.Clear();
        IEnumerable<VideoEncodingProperties> available_resolutions = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Select(x => x as VideoEncodingProperties);
        int total_res = available_resolutions.Count();
        x_res = new uint[total_res];
        y_res = new uint[total_res]
        int i = 0;
        foreach (VideoEncodingProperties resolution in available_resolutions)
        {
            x_res[i] = resolution.Width;
            y_res[i] = resolution.Height;
            resolution_listbox.Items.Add(x_res[i].ToString() + "  x  " + y_res[i].ToString());
            i++;
        }
    }
    

    Select the one you want:

    private async void resolution_listbox_selectionchanged(object sender, SelectionChangedEventArgs e)
    {
        if (resolution_listbox.SelectedItem != null)
        {
            int j = resolution_listbox.SelectedIndex;
            resolutionwidth = x_res[j];
            resolutionheight = y_res[j];
        }
    // And apply it:
        IReadOnlyList<IMediaEncodingProperties> resolutions = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);
        for (int k = 0; k < resolutions.Count; k++)
        {
            if (resolutions[k] is VideoEncodingProperties)
            {
                VideoEncodingProperties vidprops = (VideoEncodingProperties)resolutions[k];
                // check which VideoEncodingProperties contains the correct resolution
                if (vidprops.Width == resolutionwidth && vidprops.Height == resolutionheight)
                {
                    await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutions[k]);
                }
            }
        }
    }
    

    Note:in the 1st method I used IEnumerable< VideoEncodingProperties > . This because I only want the numbers.

    In the 2nd method I used IReadOnlyList< IMediaEncodingProperties >. This is because only the VideoEncodingProperties which contains the wanted resolution, needs to be applied. Not every IMediaEncodingProperties contains resolution information.