Load Image form byte[] array.

BadStormBadStorm ITMember ✭✭

Hi,
there is the posibility to load in the Xamarin.Forms Image an Image from a byte[] array?

Thanks

Posts

  • BadStormBadStorm ITMember ✭✭
    edited July 2014

    Thanks for suggestion, but if I try this code I get this error:

    The best overloaded method match for Xamarin.Forms.ImageSource.FromStream(System.Func<System.IO.Stream>)' has some invalid arguments Argument #1' cannot convert System.IO.Stream' expression to type System.Func<System.IO.Stream>'

    I also tried this:

    image.Source = ImageSource.FromStream(() => {return stream; });
    

    But I get:

    A System.ArgumentNullException was thrown.
    Argument cannot be null.
    Parameter name: data

    Thanks for support.

  • BadStormBadStorm ITMember ✭✭

    It works!

    Thanks a lot!

  • yinshenyinshen CNMember ✭✭
    edited August 2014

    @JasonAwbreyAwbrey‌
    ((RegisterDetailPage)CurrentPage).ChildImage.Source = ImageSource.FromFile("/sdcard/feng.png");
    ((RegisterDetailPage)CurrentPage).ChildImage.Source = ImageSource.FromStream(() => _memoryStreamChildPhoto);

    second method can't work, do you know why

  • SandeepkumarSandeepkumar USMember ✭✭

    thanks @JasonAwbrey

  • SacomaniSacomani USMember ✭✭

    @PankajG said:

    public class ByteToImageFieldConverter : IValueConverter
    {
       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
         ImageSource retSource = null;
          if (value != null)
          {
             byte[] imageAsBytes = (byte[])value;
             retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
          }
          return retSource;
       }
    
       public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
          throw new NotImplementedException();
       }
    }
    

    Refer: https://forums.xamarin.com/discussion/18077/binding-an-image-to-a-byte-property-on-a-model?

    there is any way to get thumbnail from byte array??

    in my case this code are throwing OutOfMemory!

  • SacomaniSacomani USMember ✭✭

    @PankajG said:

    public class ByteToImageFieldConverter : IValueConverter
    {
       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
         ImageSource retSource = null;
          if (value != null)
          {
             byte[] imageAsBytes = (byte[])value;
             retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
          }
          return retSource;
       }
    
       public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
          throw new NotImplementedException();
       }
    }
    

    Refer: https://forums.xamarin.com/discussion/18077/binding-an-image-to-a-byte-property-on-a-model?

    thanks, o found here!!

    https://forums.xamarin.com/discussion/58342/best-way-to-create-thumbnails-of-images

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    byte[] imageAsBytes = Base64.Decode(base64Image, Base64Flags.Default);
    Xamarin.Forms.Image.Source = Xamarin.Forms.ImageSource.FromStream(() => new MemoryStream(imageAsBytes));

  • MuhammadhamzaMuhammadhamza USMember ✭✭
    edited November 2017
    //Xmal Code
    <image source={Binding ProfileImage}/>
    
    //C# Code.
    
    //Image
    Byte[] mstream;
    
    ImageSource ProfileImage = ImageSource.FromStream(() => new MemoryStream(mstream.ToArray()));
    
  • I am using the above method in converter

    public class ByteArrayToImageSourceConverter : IValueConverter     
    {        
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        
     {             
         ImageSource retSource = null;             
            if (value != null)            
             {                
                 byte[] imageAsBytes = (byte[])value;           
                         retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));           
                 }             
              return retSource;         
            }      
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        
           {             
            throw new NotImplementedException();        
           }  
    }
    

    but it seems new MemoryStream applies when images load finally throws OutOfMemory! exception after sometime when the program runs , any solution to this ?? because in android memory grows gradually, is their any other solution????

  • Nadjib_BaitNadjib_Bait DZMember ✭✭✭✭

    Your image is probably too big for a mobile device. Reduce its size.

  • ncflyingncflying USMember ✭✭

    I am having a problem getting an image picked by the Xam.Media.Plugin to upload to Azure. I am able to push other images to Azure, but not if I cannot when the image is selected. The first byteData line works for the static images. The current setup gives error: Cannot convert from 'Xamarin.Forms.ImageSource' to 'string'

     //WORKS     byteData = Convert.ToByteArray("MBZ.article_image_3.jpg");
                //NOT WORKS var Pic = ImageSource.FromFile(file.Path);
                //NOT WORKS byteData = Convert.ToByteArray(Pic);
                //NOT WORKS byteData = Convert.ToByteArray(image.Source);
        //NOT WORKS byteData = image.Source;
    
                byteData = Convert.ToByteArray(image.Source);
    
            //    await DisplayAlert("File Location", file.Path, "OK");
    
                imageToUpload.Source = ImageSource.FromStream(() => new MemoryStream(byteData));
                uploadedFilename = await AzureStorage.UploadFileAsync(ContainerType.Image, new MemoryStream(byteData));
    
                image.Source = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    file.Dispose();
                    return stream;
                });
    
  • @JasonAwbrey said:
    ImageSource.FromStream(() => new MemoryStream(imageAsBytes));

    I did it! o:)

  • Image image1 = new Image();
    Byte[] bindata;
    MySqlCommand Sel = new MySqlCommand("select imgcolumn from tabla where campo1 = '" + _picker.SelectedItem.ToString() + "' Order by campo2", cConn.BD);
    bindata = (byte[])(Sel .ExecuteScalar());
    image1.Source = ImageSource.FromStream(() => new MemoryStream(bindata));

    o:) B) Thank you!

Sign In or Register to comment.