MSDN Documentation

UWP Camera App Sample

This sample demonstrates how to build a robust and feature-rich Universal Windows Platform (UWP) application that leverages the device's camera capabilities. It covers common scenarios like capturing photos, recording videos, accessing camera settings, and applying basic image effects.

Developed For: Universal Windows Platform (UWP)
Language: C#
Framework: .NET
IDE: Visual Studio
Key Features:
  • Photo Capture
  • Video Recording
  • Camera Preview
  • Flash Control
  • Focus Control
  • Zoom Functionality
  • Basic Image Filters
  • Save Media to Library
Last Updated: October 26, 2023
Version: 1.2.1
Compatibility: Windows 10 version 1809 or later
Download Sample

Overview

The UWP Camera App sample provides a comprehensive example of integrating camera functionality into your Windows applications. You will learn how to:

Key Code Snippets

Initializing the Camera

The core of camera interaction in UWP applications relies on the Windows.Media.Capture namespace, specifically the MediaCapture class.


async Task InitializeCameraAsync()
{
    var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
    if (cameraDevice == null)
    {
        // Handle case where no camera is found
        return;
    }

    mediaCapture = new MediaCapture();
    await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
    {
        AudioDeviceId = "", // No audio needed for this photo sample
        VideoDeviceId = cameraDevice.Id,
        StreamingCaptureMode = StreamingCaptureMode.Video
    });

    // Set the preview source to a CaptureElement
    var previewElement = new CaptureElement();
    previewElement.Source = mediaCapture;
    await mediaCapture.StartPreviewAsync();
}
        

Capturing a Photo

Capturing a photo involves using the MediaCapture.CapturePhotoToStorageFileAsync method.


async Task CapturePhotoAsync()
{
    if (mediaCapture.CameraStreamState == Windows.Media.Devices.CameraStreamState.Streaming)
    {
        var photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("SamplePhoto.jpg", CreationCollisionOption.GenerateUniqueName);
        await mediaCapture.CapturePhotoToStorageFileAsync(Windows.Media.Imaging.ImageFormat.Jpeg, photoFile);
        // Notify user or display photo
    }
}
        

Dependencies and Requirements

This sample requires the following:

Further Information

For more detailed information on UWP camera APIs, refer to the official Microsoft documentation.