A Simple Camera App for Windows 10

Here we are using CameraCaptureUI class which provides a full window UI for capturing audio, video, and photos from a camera.

Step 1: Open a blank app and add a Button and a Image control either from the toolbox or by copying the following XAML code into your grid.

  1. <StackPanel Margin="10,40,0,0">  
  2.    <TextBlock Text="Simple Camera" FontSize="20"></TextBlock>  
  3.    <Button Name="myCamButton" Content="Start Camera" Height="40" Width="120" Click="myCamButton_Click"></Button>  
  4.    <Image Margin="0,20,0,0" Name="capturedImage" Height="300" Width="300"></Image>  
  5. </StackPanel>        
 

Step 2 : Add the following namespaces to your project which is needed in further C# code.

  1. using Windows.Media.Capture;  
  2. using Windows.Storage;  
  3. using Windows.Storage.Streams;  
  4. using Windows.UI.Xaml.Media.Imaging;  

Step 3: Copy and paste the following code to the cs page which will be called on button click event and will start the camera and the captured image will be displayed.

  1. CameraCaptureUI captureUI = new CameraCaptureUI();    
  2. captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;    
  3.     
  4. StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);    
  5.     
  6. BitmapImage bitmapImage = new BitmapImage();    
  7. using (IRandomAccessStream fileStream = await photo.OpenAsync(FileAccessMode.Read))    
  8. {    
  9.    bitmapImage.SetSource(fileStream);    
  10. }    
  11. capturedImage.Source = bitmapImage;    

Step 4: Run your application and test yourself.

 

Up Next
    Ebook Download
    View all
    Learn
    View all