How do you take pictures with Xamarin Forms?

Xamarin Forms is a great tool for cross platform app development but sometimes hooking into native features (like the camera) can be challenging.

The good news, is that Xamarin.Essentials is Microsoft supported package that hooks into a ton of native features for you and it is getting better every release.

Install Xamarin.Essentials 1.6 or higher

First up, create a new Xamarin forms project and update the Xamarin Essentials package to a minimum of 1.6. To do this, right click on your solution in the right hand solution pane, select Manage Nuget Packages for Solution and update all your projects to 1.6.

Right now, 1.6 is a preview, so you may need to Tick include prerelease if it is not out of preview when you read this.

Take a picture with Xamarin Forms

Now that you have the package, the code is super simple!

First, go to your Content Page and Add a button to it.

Now in your code behind paste the following.

Thats it!

So what happens? In this example, we do a very simple case of code behind, so no MVVM.

Basically, tap the button. Then in the code await MediaPicker.CapturePhotoAsync(); Finally, save that to storage and set the image source of the image to that path.

Note: All camera actions have to be on the UI thread.

Isnt that easy? Love it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.