BLOB to Image Binding

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();
                }
            }

Posts

  • JoaquimVarandasJoaquimVarandas USMember ✭✭
    edited June 2014

    Solved:

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

  • SMouligneauxSMouligneaux USUniversity ✭✭

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

  • BartonClineBartonCline USMember

    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();
            }
        }
    }
    
  • hey i was also stuck on this! but this worked perfectly! thank you @JoaquimVarandas

  • pallavboharapallavbohara USMember ✭✭
    edited March 2017

    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?

  • MohamedElhefnawyMohamedElhefnawy USMember ✭✭

    hi @JoaquimVarandas

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

    thank you

  • RafaelGlezRafaelGlez USMember
    edited February 12

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

Sign In or Register to comment.