Search code examples
arraysimagexamarin.formsuser-preferences

xamarin forms get byte array from image control


In my application I let the users select an image (of any size). The application will then load it to an image control, resize it if needed, and display it on the screen.

I have all the saving / loading from the preferences working correctly right now as I use the Xamarin Forms Plugin.FilePicker and grab the byte array from that to save to my preferences.

The challenge I am facing is that if the uses pics a large image from their device, the large version of the image is what is uploaded to the FilePicker plugin and the byte array is too large to save. (I get the error ' The size of the state manager setting value has exceeded the limit.')

So what I would like to do is take the contents of the image control, which have been resized to a manageable size, convert that to a byte array and then save that in my preferences.

Any idea how I can convert the content of the image control into a byte array so I can serialize it in JSON and save it to my preferences?? Below is that code that saves the Byte Array from the file picker.

private async void btnChooseFile_Clicked(object sender, System.EventArgs e)
{
    try
    {
        FileData fileData = await CrossFilePicker.Current.PickFile();
        if (fileData == null)
            return; // user canceled file picking

        //lblFilePath.Text = fileData.FileName;

        imgIcon.Source = ImageSource.FromStream(() => fileData.GetStream());

        // THIS IS THE LINE OF CODE I NEED TO CHANGE TO IT SAVES THE 
        // BYTE ARRAY OF THE SMALLER IMAGE AS DISPLAYED BY THE 
        // IMAGE CONTROL INSTEAD OF THE FULL SIZE FILE THE USER
        // SELECTED 
        ViewModelObjects.AppSettings.KioskIcon = fileData.DataArray;

    }
    catch (Exception ex)
    {
        System.Console.WriteLine("Exception choosing file: " + ex.ToString());
    }

}

Solution

  • you could resize the image size befor assigning the value to the image control:

     #if __IOS__
        public static byte[] ResizeImageIOS(byte[] imageData, float width, float height)
        {
            UIImage originalImage = ImageFromByteArray(imageData);
            UIImageOrientation orientation = originalImage.Orientation;
    
            //create a 24bit RGB image
            using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero,
                                                 (int)width, (int)height, 8,
                                                 4 * (int)width, CGColorSpace.CreateDeviceRGB(),
                                                 CGImageAlphaInfo.PremultipliedFirst))
            {
    
                RectangleF imageRect = new RectangleF(0, 0, width, height);
    
                // draw the image
                context.DrawImage(imageRect, originalImage.CGImage);
    
                UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation);
    
                // save the image as a jpeg
                return resizedImage.AsJPEG().ToArray();
            }
        }
    
    
    #if __ANDROID__
    
        public static byte[] ResizeImageAndroid (byte[] imageData, float width, float height)
        {
            // Load the bitmap
            Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
            Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);
    
            using (MemoryStream ms = new MemoryStream())
            {
                resizedImage.Compress (Bitmap.CompressFormat.Jpeg, 100, ms);
                return ms.ToArray ();
            }
        }
    

    you could refer to ImageResizer