Click here to Skip to main content
11,806,026 members (54,706 online)
Rate this: bad
Please Sign up or sign in to vote.
On a Windows 8 desktop app, I need to take a photo using the camera in C# 4.5.

I've tried to use the CameraCaptureUI class, but it is not available on a desktop app.

So I try to use the MediaCapture class, which is available for Metro app or desktop app. It works great, based on the example found here :

var capture = new MediaCapture();
// Find the camera device id to use
string deviceId = "";
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
for (var i = 0; i < devices.Count; i++) {
     deviceId = devices[i].Id;
// init the settings of the capture
var settings = new MediaCaptureInitializationSettings();
settings.AudioDeviceId = "";
settings.VideoDeviceId = deviceId;
settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Photo;
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
await capture.InitializeAsync(settings);
// Find the highest resolution available
VideoEncodingProperties resolutionMax = null;
int max = 0;
var resolutions = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);
for (var i = 0; i < resolutions.Count; i++) {
     VideoEncodingProperties res = (VideoEncodingProperties)resolutions[i];
     Console.WriteLine("resolution : " + res.Width + "x" + res.Height);
     if (res.Width * res.Height > max) {
          max = (int)(res.Width * res.Height);
          resolutionMax = res;
await capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutionMax);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
var fPhotoStream = new InMemoryRandomAccessStream();
// captureElement.Source = capture;
// await capture.StartPreviewAsync();

// Take the photo and show it on the screen
await capture.CapturePhotoToStreamAsync(imageProperties, fPhotoStream);
await fPhotoStream.FlushAsync();
byte[] bytes = new byte[fPhotoStream.Size];
await fPhotoStream.ReadAsync(bytes.AsBuffer(), (uint)fPhotoStream.Size, InputStreamOptions.None);
BitmapImage bitmapImage = new BitmapImage();
MemoryStream byteStream = new MemoryStream(bytes);
bitmapImage.StreamSource = byteStream;
image.Source = bitmapImage;

I can take a photo using the camera, but I'm unable to show a preview before taking the photo.
To be able to show the preview, I have to use the component CaptureElement, for example with the following code :
captureElement.Source = mediaCapture;
await mediaCapture.startPreviewAsync();

Unfortunately, I cannot use a CaptureElement on a non store app.
Is there another component that I can use in a WPF or WinForm app, to be able to show the preview of the camera ?
Posted 25-Jan-13 4:30am
Jonathan Morales Vélez at 12-Feb-13 11:26am

I'm also trying to capture a photo from a desktop wpf app. I see you say you can use the MediaCapture class on the desktop app too, but I don't know how you are doing it. I can't find the reference to MediaCapture class on my desktop app. Am I missing something. Could you tell me how you use the MediaCapture class in a wpf desktop app please?

Thank you
Ray20 at 12-Feb-13 11:37am
See this link to be able to use MediaCapture API on a desktop app :
Hope it will help you
Member 9038124 at 29-Oct-14 23:43pm
Any solution to this problem? I am having the exact same issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Richard Deeming 225
1 OriginalGriff 215
2 Sergey Alexandrovich Kryukov 170
3 ppolymorphe 125
4 Abhinav S 118
0 OriginalGriff 3,300
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 29 Oct 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100