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;
}
}
Image img = new Image
{
Source = await CaptureImage()
};
CameraCaptureUI class
StorageFile class
BitmapImage class
Image class