Skip to content

Latest commit

 

History

History
52 lines (44 loc) · 1.67 KB

Capture-an-image.md

File metadata and controls

52 lines (44 loc) · 1.67 KB

Capture an image

Captures an image using the device's camera and returns it as a bitmap.

using System;
using System.Threading.Tasks;
using Windows.Media.Capture;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

public async Task<BitmapImage> CaptureImage(bool allowCropping = true, 
    CameraCaptureUIPhotoFormat photoFormat = CameraCaptureUIPhotoFormat.Jpeg, 
    CameraCaptureUIMaxPhotoResolution maxPhotoResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable)
{
    var captureUI = new CameraCaptureUI();
    captureUI.PhotoSettings.AllowCropping = allowCropping;
    captureUI.PhotoSettings.Format = photoFormat;
    captureUI.PhotoSettings.MaxResolution = maxPhotoResolution;
    StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file == null) return null;
    using (IRandomAccessStream ras = await file.OpenAsync(FileAccessMode.Read))
    {
        var bitmapImage = new BitmapImage();
        bitmapImage.SetSource(ras);
        return bitmapImage;
    }
}

Usage

Image img = new Image 
{
    Source = await CaptureImage()
};

See also

CameraCaptureUI class
StorageFile class
BitmapImage class
Image class