Convert Image to Byte Array and Convert Byte Array to Image in Xamarin.Forms

May I ask about How to "Convert Image to Byte Array" and "Convert Byte Array to Image" in Xamarin.Forms?
But i'm facing problem to convert MediaFile into Byte[].
I'm using Xam.Plugin.Media ( http://www.nuget.org/packages/Xam.Plugin.Media ) to access camera and gallery.
Please kindly guide me how to convert MedieFile into Byte[].
Thank you.
Any sample source code for references?

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @CheahEngSoon,

    MediaFile has GetStream().

    You could use this to the Stream and then convert that to a byte[]. Here is one way to do that:

    public byte[] GetImageStreamAsBytes(Stream input)
        {
            var buffer = new byte[16*1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
    }
    
  • CheahEngSoonCheahEngSoon USMember ✭✭

    @JohnMiller said:
    @CheahEngSoon,

    MediaFile has GetStream().

    You could use this to the Stream and then convert that to a byte[]. Here is one way to do that:

    public byte[] GetImageStreamAsBytes(Stream input)
        {
            var buffer = new byte[16*1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
    }
    

    @JohnMiller How about convert the bytes array to images?

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @CheahEngSoon,

    myImage.Source = ImageSource.FromStream(() => new MemoryStream(bytes));
    
  • IvanositoIvanosito COMember ✭✭

    Hi, I'm really new with Xamarin.Forms.
    How can I read the jpg file from a specific path I have to an image variable?
    Can it be done at the PCL Project?

    Thanks!

  • IonCojucovschiIonCojucovschi USMember ✭✭
    edited January 2018
    imageBitmap = ImageHelper.GetImageBitmapFromFilePath(imageFile.Path, width, height);
    
       if (imageBitmap != null)
                {
                    PictureImageView.SetImageBitmap(imageBitmap);
                    imageBitmap = null;
                    FileimagePath = imageFile.Path;
    
                }
    
    

    it wirk for me :)

  • IonCojucovschiIonCojucovschi USMember ✭✭
    edited January 2018

    an d image helper just copy this class to your project...

    class ImageHelper
            {
                public static Bitmap GetImageBitmapFromUrl(string url)
                {
                    Bitmap imageBitmap = null;
                    using (var webClient = new WebClient())
                    {
                        var imageBytes = webClient.DownloadData(url);
                        if (imageBytes != null && imageBytes.Length > 0)
                        {
                            imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                        }
                    }
                    return imageBitmap;
                }
    
                public static Bitmap GetImageBitmapFromFilePath(string fileName, int width, int height)
                {
                    // first we get the the dimensions of the file on disk
                    BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
                    BitmapFactory.DecodeFile(fileName, options);
    
                    // next we calculate the ratio that we need to resize the image by 
                    // in order to fit the requested dimensions
                    int outHeight = options.OutHeight;
                    int outWidth = options.OutWidth;
                    int inSampleSize = 1;
    
                    if (outHeight > height || outWidth > width)
                    {
                        inSampleSize = outWidth > outHeight
                                        ? outHeight / height
                                        : outWidth / width;
                    }
    
                    // now we will load the image and have BitmapFactory resize it for us
                    options.InSampleSize = inSampleSize;
                    options.InJustDecodeBounds = false;
                    Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);
    
                    return resizedBitmap;
                }
    
    
            }
    
Sign In or Register to comment.