.NET Developer, Microsoft MVP

Windows Community Toolkit: Use CameraPreview to Take Photos

In UWP Apps, if we want to use the device's camera to take photos and save to a file, it is used to be some how complicated, the developer need to know about quite a lot things and also write quite a few code to get it down (As you can find the Camera examples from Microsoft offical sample code). In fact, in many cases, we just want to take a photo by the most easy and default options via the camera. In those cases, our focus is on the business logic of the App instead of spending a few hours just to get the camera code working.

Now, you can get this easily done by Windows Community Toolkit. The CameraPreviewcontrol provides a straight forward way for us to use the camera and take photos without knowing the details.

1. Install via NuGet

PM> Install-Package Microsoft.Toolkit.Uwp.UI.Controls

2. Allow WebCam capability in your UWP App

You can do it from Visual Studio UI by opening Package.appxmanifest file:

Now when we click the Capture button, we need to save the current frame as picture into local file system. The picture is presented as SoftwareBitmapobject which we can access via _currentVideoFrame?.SoftwareBitmap

To let the user pick a file save location, we will use the FileSavePicker, it will return a StorageFileobject which represents the file we need to save the photo to. You can also choose file format, here I will use JPG because this is the most common format for photos.

Many thanks. We where test crawling our website and noticed a difference in the URLs. Now we know why :) It's already hard to keep consistency when developing applications in a team. Now even need to be more careful with selecting which UrlEncode to use by default.