Download Demo Project
Taking pictures using Camera or File Picker is pretty easy using WinRT, we going to use the MediaCapture class for Audio Recording, CameraCaptureUI for taking Picture from the built in Camera and FileOpenPicker for choosing pictures from local directories.
The MediaCapture class will allow us to capture:
But first we need to create our project resources and define the Application Capabilities to allow us using those features.
Open Package.appmanifest file located under the project and select the following: Each of the below selections gives the application the needed permissions to work with those features.
mediaCaptureMgr = new MediaCapture();
/// Invoked when this page is about to be displayed in a Frame.
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected async override void OnNavigatedTo(NavigationEventArgs e)
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
The code below using DeviceInformation.FindAllAsync method to get all media devices on your machine, you can also use this to get specific devices such as AudioCapture, AudioRender, PortableStorageDevice or VideoCapture.
Then we just add the name of each device found to a listbox.
async void EnumerateWebcamsAsync()
devInfoCollection = null;
devInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.All);
if (devInfoCollection == null || devInfoCollection.Count == 0)
await new Windows.UI.Popups.MessageDialog("No WebCams found.").ShowAsync();
for (int i = 0; i < devInfoCollection.Count; i++)
var devInfo = devInfoCollection.ElementAt(i);
EnumedDeviceList.SelectedIndex = 0;
catch (Exception e)
File Picker is part of WinRT, allowing the user to choose files from local directories.
In the code below we define new class of FileOpenPicker and set several options:
Finally we call the PickMultipleFilesAsync to open the file picker and allowing the user to select multiple images. (You can also use PickSingleFileAsync for single image selection)
When the user click select the desire images and click the OK button we get a IReadOnlyList of StorageFile, in order to show them on the screen we can get the stream from each item our we can copy those file to our local storage and then use the “ms-appdata:///Local/FileName” path to display those images from our local storage folder.
private async void btnFilePicker_Click(object sender, RoutedEventArgs e)
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); //PickSingleFileAsync
if (files.Count == 0)
await new Windows.UI.Popups.MessageDialog("No files were seleced!").ShowAsync();
var storage = Windows.Storage.ApplicationData.Current.LocalFolder;
foreach (var file in files)
var copiedFile = await file.CopyAsync(storage, file.Name, NameCollisionOption.GenerateUniqueName);
Now it’s time to capture a image from the built in Camera, to do so we’ll use the CameraCaptureUI, simply define what type of image you want to receive by defining the Format, size etc..
After the user close the Camera dialog we check if the return file isn’t null and if it doesn't we use OpenAsyc method to get the file stream and then using that stream we’ll create a new BitmapImage from that stream and assign it to the ImgCamera.
private async void btnCamera_Click(object sender, RoutedEventArgs e)
CameraCaptureUI dialog = new CameraCaptureUI();
Size aspectRatio = new Size(16, 9);
dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;
dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bmpimg = new BitmapImage();
ImgCamera.Source = bmpimg;
catch (Exception ex)
No for the last part of application, Recording Audio, first we’ll create a StorageFile inside the Music Library, we’ll set the recording profile to create an M4a file type and just calling the StartRecordToStorageFileAsync method of the MediaCapture class passing the storageFile and the recording profile.
After the user click the stop button we call the StopRecordAsync method, this will stop the recording and same as we did for the Camera, we open the file stream and assign the stream to the MediaPlayer source.
private async void btnAudio_Click(object sender, RoutedEventArgs e)
String fileName = "AudioRecording.m4a";
recordStorageFile = await Windows.Storage.KnownFolders.MusicLibrary.CreateFileAsync(fileName);
Windows.Media.MediaProperties.MediaEncodingProfile recordProfile = null;
recordProfile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateM4a(Windows.Media.MediaProperties.AudioEncodingQuality.Auto);
recording = true;
btnAudio.Content = "Stop Recording";
await mediaCaptureMgr.StartRecordToStorageFileAsync(recordProfile, recordStorageFile);
btnAudio.Content = "Start Recording";
var stream = await recordStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
playbackElement.AutoPlay = true;
recording = false;
recording = false;
Pingback from Windows 8 C# ??? Capture Media - Shai Raiten
Pingback from Windows 8 Developer Links – 2012-05-23 | Dan Rigby
Pingback from Windows 8 C# ??? Capture Media – Shai Raiten | Media Point - O Ponto de Encontro de todos os interessados nos Media!
Pingback from Windows 8 C# ??? Capture Media – Shai Raiten
Hi Shai Raiten,
I just came across your article and I found it really intersting. I just can't get it to run on Visual Studio 2012 or VS Express for Win 8.
I always get "MediaPlayer is not supported in a Windows App project". Any idea why?
Pingback from Windows 8 RTM C# ??? Capture Media - Shai Raiten
Pingback from Capture Media in Windows 8 C# Apps | SELA Blog News | Scoop.it