Introduction
In this article, we will start with functionality to capture images in a Windows 10 application. For that, we need to add the capability of a Webcam and PicturesLibrary in the Package.appxmanifest file. After adding the capability, we will add a button to capture an image and an Image control to show the captured image. Then we will save the image in the system.
So, add the following code to the XAML file:
- <Button Name="Capture" Content="Capture" HorizontalAlignment="Left" Margin="151,300,0,0" VerticalAlignment="Top" Height="81" Width="159" Click="Capture_Click"/>
- <Image Name="image" HorizontalAlignment="Left" Height="503" Margin="523,106,0,0" VerticalAlignment="Top" Width="634"/>
- <Button x:Name="Save" Content="Save" HorizontalAlignment="Left" Margin="151,477,0,0" VerticalAlignment="Top" Height="81" Width="159" Click="Save_Click"/>
- using Windows.Media.Capture;
- using Windows.UI.Xaml.Media.Imaging;
- using System.Collections.Generic;
- using Windows.Storage;
- using Windows.Storage.Pickers;
- using Windows.Storage.Streams;
- using Windows.UI.Popups;
- private StorageFile sf;
- private IRandomAccessStream rs;
- private async void Capture_Click(object sender, RoutedEventArgs e)
- {
- // Using the CameraCaptureUI object to invoke webcam and capture the image.
- CameraCaptureUI cc = new CameraCaptureUI();
- cc.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; // Added support for Jpeg format, you can use any as you like.
- cc.PhotoSettings.CroppedAspectRatio = new Size(3, 4);
- cc.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;
- // saving captured file in the StorageFile object, so that it can be later used to save the file in the specified location.
- sf = await cc.CaptureFileAsync(CameraCaptureUIMode.Photo);
- if (sf != null)
- {
- BitmapImage bmp = new BitmapImage();
- rs = await sf.OpenAsync(FileAccessMode.Read);
- bmp.SetSource(rs);
- image.Source = bmp;
- }
- }
- private async void Save_Click(object sender, RoutedEventArgs e)
- {
- // Adding try catch block in case of occurence of an exception
- try
- {
- // Creating object of FileSavePicker and adding few values to the properties of the object.
- FileSavePicker fs = new FileSavePicker();
- fs.FileTypeChoices.Add("Image", new List < string > ()
- {
- ".jpeg"
- });
- fs.DefaultFileExtension = ".jpeg";
- fs.SuggestedFileName = "Image" + DateTime.Today.ToString();
- fs.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
- // Using storagefile object defined earlier in above method to save the file using filesavepicker.
- fs.SuggestedSaveFile = sf;
- // Saving the file
- var s = await fs.PickSaveFileAsync();
- if (s != null)
- {
- using(var dataReader = new DataReader(rs.GetInputStreamAt(0)))
- {
- await dataReader.LoadAsync((uint) rs.Size);
- byte[] buffer = new byte[(int) rs.Size];
- dataReader.ReadBytes(buffer);
- await FileIO.WriteBytesAsync(s, buffer);
- }
- }
- } catch (Exception ex)
- {
- var messageDialog = new MessageDialog("Something went wrong.");
- await messageDialog.ShowAsync();
- }
- }
Run the program, and enjoy!
Summary
In this article, we learned about Capturing and Saving Image Application in Windows 10.


Santhakumar MunuswamyPosted Sep 1, 2015, 11:35 AM EDT
Welcome to C# Corner family
Santhakumar MunuswamyPosted Sep 1, 2015, 11:34 AM EDT
Thanks for nice article:)
Sibeesh VenuTOP 100Posted Sep 1, 2015, 3:27 AM EDT
Nice Share
Dinesh BeniwalTOP 10Posted Sep 1, 2015, 2:48 AM EDT
Good start Aditya, Welcome to C# Corner.
Praveen KumarPosted Aug 31, 2015, 7:20 PM EDT
Very nice
Karthikeyan KPosted Aug 31, 2015, 7:17 PM EDT
Good one...
Syed ShanuTOP 100Posted Aug 31, 2015, 4:23 PM EDT
Hi, Nice One.One small request is that possibel for you to add the sample Source code zip file and few screens Images.
Pankaj Kumar ChoudharyTOP 100Posted Aug 31, 2015, 3:54 PM EDT
Nice Start Sir...........
Mohammed IbrahimPosted Aug 31, 2015, 2:51 PM EDT
nice