Custom Camera Dialog For Windows Using UWP

Introduction

In this tutorial, we will learn how to create a custom dialog for camera view for Windows 10 using UWP. To achieve this, we need to use Web Camera and Storage features for Windows 10 operating system.

Steps

I have split the coding part into 5 steps as in the following.
Step 1 - Creating new UWP Project
Step 2 - Implementation of Camera Dialog
Step 3 - Implementation of Camera Output Storage
Step 1 - Creating new UWP Project
Step 2 - Implementation of Camera Dialog
In this step, we will create a custom dialog for camera preview in UWP.
Step 3 - Implementation of Camera Output Storage
In this step, we will process and save the captured preview into system.
Full Code
You can find the full code for Camera Dialog below.
  1. using System;
  2. using Windows.Foundation;
  3. using Windows.UI.Xaml;
  4. using Windows.UI.Xaml.Controls;
  5. using Windows.UI.Xaml.Navigation;
  6. using Windows.UI.Core;
  7. using System.Threading.Tasks;
  8. using Windows.Graphics.Display;
  9. using Windows.Storage.Streams;
  10. using Windows.Media.MediaProperties;
  11. using Windows.Graphics.Imaging;
  12. using Windows.Storage.FileProperties;
  13. using Windows.Storage;
  14. using Windows.Media.Capture;
  15. using Windows.System.Display;
  16. // The Content Dialog item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
  17. namespace UwpCamSample
  18. {
  19. public sealed partial class CameraDialog : ContentDialog
  20. {
  21. MediaCapture mediaCapture;
  22. DisplayRequest displayRequest = new DisplayRequest();
  23. bool isPreviewing;
  24. //private static CameraDialog _instance = null;
  25. //public static CameraDialog Instance
  26. //{
  27. // get
  28. // {
  29. // if (_instance == null)
  30. // _instance = new CameraDialog();
  31. // return _instance;
  32. // }
  33. // set
  34. // {
  35. // }
  36. //}
  37. public CameraDialog()
  38. {
  39. this.InitializeComponent();
  40. StartPreviewAsync();
  41. }
  42. private async void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
  43. {
  44. await CleanupCameraAsync();
  45. }
  46. private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
  47. {
  48. var myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
  49. StorageFile file = await myPictures.SaveFolder.CreateFileAsync("photo.jpg", CreationCollisionOption.GenerateUniqueName);
  50. using (var captureStream = new InMemoryRandomAccessStream())
  51. {
  52. await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream);
  53. using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
  54. {
  55. var decoder = await BitmapDecoder.CreateAsync(captureStream);
  56. var encoder = await BitmapEncoder.CreateForTranscodingAsync(fileStream, decoder);
  57. var properties = new BitmapPropertySet {
  58. {
  59. "System.Photo.Orientation", new BitmapTypedValue(PhotoOrientation.Normal, PropertyType.UInt16)
  60. }
  61. };
  62. await encoder.BitmapProperties.SetPropertiesAsync(properties);
  63. await encoder.FlushAsync();
  64. }
  65. }
  66. await CleanupCameraAsync();
  67. }
  68. private async Task StartPreviewAsync()
  69. {
  70. try
  71. {
  72. mediaCapture = new MediaCapture();
  73. await mediaCapture.InitializeAsync();
  74. displayRequest.RequestActive();
  75. DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
  76. }
  77. catch (UnauthorizedAccessException)
  78. {
  79. return;
  80. }
  81. try
  82. {
  83. PreviewControl.Source = mediaCapture;
  84. await mediaCapture.StartPreviewAsync();
  85. isPreviewing = true;
  86. }
  87. catch (System.IO.FileLoadException)
  88. {
  89. mediaCapture.CaptureDeviceExclusiveControlStatusChanged += _mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
  90. }
  91. }
  92. private async void _mediaCapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture sender, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs args)
  93. {
  94. if (args.Status == MediaCaptureDeviceExclusiveControlStatus.SharedReadOnlyAvailable)
  95. {
  96. //ShowMessageToUser("The camera preview can't be displayed because another app has exclusive access");
  97. }
  98. else if (args.Status == MediaCaptureDeviceExclusiveControlStatus.ExclusiveControlAvailable && !isPreviewing)
  99. {
  100. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  101. {
  102. await StartPreviewAsync();
  103. });
  104. }
  105. }
  106. private async Task CleanupCameraAsync()
  107. {
  108. if (mediaCapture != null)
  109. {
  110. if (isPreviewing)
  111. {
  112. await mediaCapture.StopPreviewAsync();
  113. }
  114. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  115. {
  116. PreviewControl.Source = null;
  117. if (displayRequest != null)
  118. {
  119. displayRequest.RequestRelease();
  120. }
  121. mediaCapture.Dispose();
  122. //mediaCapture = null;
  123. });
  124. }
  125. }
  126. }
  127. }

Output

Custom Camera Dialog For Windows Using UWP
Custom Camera Dialog For Windows Using UWP
Custom Camera Dialog For Windows Using UWP
Download Code
You can download the code from GitHub. If you have doubts, feel free to post a comment. If you like this article and it is useful to you, do like, share the article & star the repository on GitHub.