locked
BLOB to Image Binding RRS feed

  • Question

  • User51931 posted

    I have to bind images from a SQLite database. The images are stores as BLOB (png file).

    var image = new Image
     {
              HorizontalOptions = LayoutOptions.Start,
              WidthRequest = 40,
              HeightRequest = 40
     };
    
     var bnd = new Binding("Image", BindingMode.Default, new ArrayToImageConverter());
     image.SetBinding(Image.SourceProperty, bnd);
    

    How to define an "ArrayToImageConverter"

            public abstract class ArrayToImageConverter : IValueConverter
                {
                   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                   {
                         byte[] bmp = value as byte[];
                         if (bmp == null)
                             return null;
    
                       return new StreamImageSource 
                       {
                             Stream =  ?????
                        };
                   }
                    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                   {
                         throw new System.NotImplementedException();
                    }
                }
    
    Thursday, June 5, 2014 2:48 PM

All replies

  • User51931 posted

    Solved:

    return ImageSource.FromStream(() => new MemoryStream(bmp));

    Thursday, June 5, 2014 3:09 PM
  • User59750 posted

    Thank you for your Thread, @JoaquimVarandas? . I had the save problem. Solved thanks to you ;-)

    Thursday, July 24, 2014 2:09 PM
  • User84475 posted

    Skip the bitmap, create the stream using the byte array directly. The thing to remember is to reset the stream to Position=0

    namespace Core.Converters
    {
        public class ByteArrayToImageSource : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == null || value is DBNull)
                    return null;
                var bArray = (byte[]) value;
    
                var imgsrc = ImageSource.FromStream(() => {
                    var ms = new MemoryStream(bArray);
                    ms.Position = 0;
                    return ms;
                });
    
                return imgsrc;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    
    Monday, November 24, 2014 11:42 PM
  • User175539 posted

    hey i was also stuck on this! but this worked perfectly! thank you @JoaquimVarandas

    Monday, March 7, 2016 2:04 AM
  • User279438 posted

    I am using this <Image Source="{Binding pictureBlob,Converter={StaticResource arrayToIamgeConverter},Mode=Default}" /> where pictureBlob is string of blob. It is giving me this error.

    System.MissingMethodException: Default constructor not found for type PsiDashboard.ArrayToImageConverter

    Can someone suggest how to use it in listview?

    Thursday, March 30, 2017 4:02 PM
  • User308644 posted

    hi @JoaquimVarandas

    if suitable, Can you provide me all steps if i have used XAML ?

    thank you

    Wednesday, April 19, 2017 1:13 AM
  • User184124 posted

    And If I have the JPG Image and I want to convert it to blob file?

    Tuesday, February 12, 2019 12:12 AM