Capturing and Saving Image Application in Windows 10

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:

  1. <Button Name="Capture" Content="Capture" HorizontalAlignment="Left" Margin="151,300,0,0" VerticalAlignment="Top" Height="81" Width="159" Click="Capture_Click"/>    
  2. <Image Name="image" HorizontalAlignment="Left" Height="503" Margin="523,106,0,0" VerticalAlignment="Top" Width="634"/>    
  3. <Button x:Name="Save" Content="Save" HorizontalAlignment="Left" Margin="151,477,0,0" VerticalAlignment="Top" Height="81" Width="159" Click="Save_Click"/>   
Before adding any code in the Capture_click and Save_Click events, we need to add a few namespaces in the .CS file so that no issues will occur in the code:
  1. using Windows.Media.Capture;    
  2. using Windows.UI.Xaml.Media.Imaging;    
  3. using System.Collections.Generic;    
  4. using Windows.Storage;    
  5. using Windows.Storage.Pickers;    
  6. using Windows.Storage.Streams;    
  7. using Windows.UI.Popups;  
Now, we go to the Capture_click event to capture the image using the webcam. But before adding the functionality of capture_click, we need to declare the following variables that will be used throughout the application:
  1. private StorageFile sf;    
  2. private IRandomAccessStream rs;   
Now, the functionality of this capture_click event is:
  1. private async void Capture_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.   
  4.     // Using the CameraCaptureUI object to invoke webcam and capture the image.    
  5.     CameraCaptureUI cc = new CameraCaptureUI();  
  6.     cc.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; // Added support for Jpeg format, you can use any as you like.    
  7.     cc.PhotoSettings.CroppedAspectRatio = new Size(3, 4);  
  8.     cc.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;  
  9.   
  10.     // saving captured file in the StorageFile object, so that it can be later used to save the file in the specified location.    
  11.     sf = await cc.CaptureFileAsync(CameraCaptureUIMode.Photo);  
  12.     if (sf != null)   
  13.     {  
  14.         BitmapImage bmp = new BitmapImage();  
  15.         rs = await sf.OpenAsync(FileAccessMode.Read);  
  16.         bmp.SetSource(rs);  
  17.         image.Source = bmp;  
  18.     }  
  19. }  
We will now add the code for the save_click method that will save the image file to a specific location in the system:
  1. private async void Save_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.     // Adding try catch block in case of occurence of an exception    
  4.     try   
  5.     {  
  6.         // Creating object of FileSavePicker and adding few values to the properties of the object.    
  7.         FileSavePicker fs = new FileSavePicker();  
  8.         fs.FileTypeChoices.Add("Image"new List < string > ()   
  9.         {  
  10.             ".jpeg"  
  11.         });  
  12.         fs.DefaultFileExtension = ".jpeg";  
  13.         fs.SuggestedFileName = "Image" + DateTime.Today.ToString();  
  14.         fs.SuggestedStartLocation = PickerLocationId.PicturesLibrary;  
  15.   
  16.         // Using storagefile object defined earlier in above method to save the file using filesavepicker.    
  17.         fs.SuggestedSaveFile = sf;  
  18.   
  19.         // Saving the file    
  20.         var s = await fs.PickSaveFileAsync();  
  21.         if (s != null)   
  22.         {  
  23.             using(var dataReader = new DataReader(rs.GetInputStreamAt(0)))   
  24.             {  
  25.                 await dataReader.LoadAsync((uint) rs.Size);  
  26.                 byte[] buffer = new byte[(int) rs.Size];  
  27.                 dataReader.ReadBytes(buffer);  
  28.                 await FileIO.WriteBytesAsync(s, buffer);  
  29.             }  
  30.         }  
  31.     } catch (Exception ex)   
  32.     {  
  33.         var messageDialog = new MessageDialog("Something went wrong.");  
  34.         await messageDialog.ShowAsync();  
  35.     }  
  36. }  
Run the program, and enjoy!

Up Next
    Ebook Download
    View all
    Learn
    View all