Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 21 June 2018

How to capture image in Xamarin Forms? – Solved

Hi developers, I am writing this blog for helping one of my internet friend who asked for my help.

Query – How to capture an image in Xamarin Forms? And image size should not be greater than 5MB in size?

In this article, I have explained not only how to capture an image by phone camera but also an easy way to validate the size of an image (highlighted in yellow color) whose size should not be greater than 5MB in size with a real-time example where one can use this and learn more.

In this example, I am capturing an image using Mobile Camera using Xamarin Forms.

(Note: You need to install Xamarin.Mobile(or xamstore-xamarin.mobile) from Nuget package manager)

Practical Implementation:


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            <Button Clicked="TakeAPicture_Clicked" />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Media;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DemoXamarin
       public partial class TakeAPicturePage : ContentPage

              public static MediaPicker MediaPicker;
              public TakeAPicturePage ()
                     InitializeComponent ();

        private async void PickAPicture_Clicked(object sender, EventArgs e)
            if (IsBusy)

                IsBusy = true;

                var picker = MediaPicker;
                if (!picker.IsCameraAvailable)
                    Console.WriteLine("No camera!");
                        MediaFile file = await picker.PickPhotoAsync();

                        byte[] bytes = new byte[0];

                        var memoryStream = new MemoryStream();

                            bytes = memoryStream.ToArray();

                        //Validating image
                        int fileSize = bytes.Length;
                        if (fileSize > 5242880)
                            Console.WriteLine("File Attachment should be less than 5 MB");
                    catch (OperationCanceledException)
            catch (Exception ex)
                IsBusy = false;


Hope you find the article helpful and interesting. For any query, comment us below.


Click imagination hunt to read latest blogs.

Keep learning and sharing...

No comments:

Post a comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...