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:

TakeAPicturePage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DemoXamarin.TakeAPicturePage">
    <ContentPage.Content>
        <StackLayout>
            <Button Clicked="TakeAPicture_Clicked" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

TakeAPicturePage.xaml.cs

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
{
       [XamlCompilation(XamlCompilationOptions.Compile)]
       public partial class TakeAPicturePage : ContentPage
       {

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

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

            try
            {
                IsBusy = true;

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

                        byte[] bytes = new byte[0];

                        var memoryStream = new MemoryStream();
                        {
                            file.GetStream().CopyTo(memoryStream);
                            file.Dispose();

                            bytes = memoryStream.ToArray();
                        }

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

Demo 



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...