Sdílet prostřednictvím


How to adjust captured photo resolution in an app for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Starting with Windows Phone OS 7.1, you can programmatically access the phone’s camera with the Microsoft.Devices..::.PhotoCamera class. This topic describes how to programmatically set the resolution of captured photos in your Windows Phone app. It is a continuation of How to create a base camera app for Windows Phone 8 and assumes you have already created the base camera project in that topic.

Tip

Starting with Windows Phone 8, apps can use the PhotoCaptureDevice class for advanced capture scenarios. PhotoCaptureDevice allows apps to control photo properties such as ISO, exposure compensation, and manual focus position (when available on the phone). This topic doesn’t demonstrate PhotoCaptureDevice; for more info about using this class, see Advanced photo capture for Windows Phone 8.

This topic corresponds to the Basic Camera Sample.

Setting the Captured Photo Resolution

In this section, you add a button to the UI and add the necessary logic in the code-behind file to set the captured photo resolution to the next size when the Res button is tapped.

To set the captured photo resolution

  1. Open your base camera project created in How to create a base camera app for Windows Phone 8.

  2. In the main page XAML file, MainPage.xaml, add the following code in the StackPanel element, below the Button element named ShutterButton.

    <Button Content="Res" Name="ResButton" Click="changeRes_Clicked" 
        FontSize="26" FontWeight="ExtraBold" Height="75"/>
    

    This code is the button for setting the photo resolution.

  3. Open the code-behind file for the main page, MainPage.xaml.cs, and add the following variable declaration above the MainPage class constructor.

    // Holds the current resolution index.
    int currentResIndex = 0;
    
    ' Holds the current resolution index.
    Dim currentResIndex As Integer = 0
    
  4. In MainPage.xaml.cs, add the following code to the OnNavigatedTo method, just below the Disable UI comment.

    ResButton.IsEnabled = false;
    
    ResButton.IsEnabled = False
    

    This code disables the resolution button. It is used when a camera is not available on the phone.

  5. In MainPage.xaml.cs, add the following code to the MainPage class.

        private void changeRes_Clicked(object sender, System.Windows.RoutedEventArgs e)
        {
            // Variables
            IEnumerable<Size> resList = cam.AvailableResolutions;
            int resCount = resList.Count<Size>();
            Size res;
    
            // Poll for available camera resolutions.
            for (int i = 0; i < resCount; i++)
            {
                res = resList.ElementAt<Size>(i);
            }
    
            // Set the camera resolution.
            res = resList.ElementAt<Size>((currentResIndex + 1) % resCount);
            cam.Resolution = res;
            currentResIndex = (currentResIndex + 1) % resCount;
    
            // Update the UI.
            txtDebug.Text = String.Format("Setting capture resolution: {0}x{1}", res.Width, res.Height);
            ResButton.Content = "R" + res.Width;
        }
    
        Private Sub changeRes_Clicked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
    
            ' Variables
            Dim resList As IEnumerable(Of Size) = cam.AvailableResolutions
            Dim resCount As Integer = resList.Count()
            Dim res As Size
    
            ' Poll for available camera resolutions.
            For i As Integer = 0 To resCount - 1
                res = resList.ElementAt(i)
            Next i
    
            ' Set the camera resolution.
            res = resList.ElementAt((currentResIndex + 1) Mod resCount)
            cam.Resolution = res
            currentResIndex = (currentResIndex + 1) Mod resCount
    
            ' Update the UI.
            txtDebug.Text = String.Format("Setting capture resolution: {0}x{1}", res.Width, res.Height)
            ResButton.Content = "R" & res.Width
    
        End Sub
    

    This code changes the photo resolution to the next available resolution. Available capture resolutions are provided by the AvailableResolutions property of the PhotoCamera class. These resolutions are a collection of Size structures. Each Size specifies a Height and Width property.

  6. On a phone, run the appa by selecting the Debug | Start Debugging menu command.

See Also

Other Resources

Advanced photo capture for Windows Phone 8

Capturing video for Windows Phone 8

Lenses for Windows Phone 8

How to use the camera capture task for Windows Phone 8