locked
Load Image form byte[] array. RRS feed

  • Question

  • User13536 posted

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

    Thanks

    Saturday, July 5, 2014 2:19 PM

All replies

  • User65 posted

    try something like this (haven't checked exact syntax)

    Image image = new Image();
    Stream stream = new MemoryStream(byteArray);
    image.Source = ImageSource.FromStream(stream);
    
    Saturday, July 5, 2014 3:05 PM
  • User13536 posted

    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 typeSystem.Func'

    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.

    Saturday, July 5, 2014 4:12 PM
  • User65 posted

    ImageSource.FromStream(() => new MemoryStream(imageAsBytes));

    Saturday, July 5, 2014 7:17 PM
  • User13536 posted

    It works!

    Thanks a lot!

    Saturday, July 5, 2014 8:25 PM
  • User60645 posted

    @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

    Monday, August 11, 2014 4:00 PM
  • User46615 posted

    thanks @JasonAwbrey

    Wednesday, March 11, 2015 9:49 AM
  • User163400 posted

    ```csharp 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?

    Friday, December 18, 2015 4:32 AM
  • User230417 posted

    @PankajG said: ```csharp 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!

    Friday, November 25, 2016 7:35 PM
  • User230417 posted

    @PankajG said: ```csharp 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

    Friday, November 25, 2016 7:41 PM
  • User261729 posted

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

    Wednesday, December 21, 2016 11:49 AM
  • User341826 posted
    //Xmal Code
    <image source={Binding ProfileImage}/>
    
    //C# Code.
    
    //Image
    Byte[] mstream;
    
    ImageSource ProfileImage = ImageSource.FromStream(() => new MemoryStream(mstream.ToArray()));
    
    Friday, November 24, 2017 11:20 AM
  • User333528 posted

    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????

    Friday, December 8, 2017 8:07 AM
  • User74518 posted

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

    Sunday, December 10, 2017 9:16 PM
  • User357842 posted

    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;
                });
    
    Saturday, February 24, 2018 9:46 PM
  • User370794 posted

    @JasonAwbrey said: ImageSource.FromStream(() => new MemoryStream(imageAsBytes));

    I did it! o:)

    Sunday, July 15, 2018 3:18 AM
  • User370794 posted

    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!

    Sunday, July 15, 2018 3:23 AM
  • User31568 posted

    I am using this in my WinForm application and want to have the same in my Xamarin but getting errors

    'Image' does not contain a definition for 'FromStream'

    here is the code...

    public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
    

    and then:

    sql_connection = new SqlConnection("Server=my.domain.com; Database=myDB; User Id=sa; Password=myPassword;");
    await sql_connection.OpenAsync();
    
    sql_command = new SqlCommand("sp_get_student_details", sql_connection);
    sql_command.CommandType = CommandType.StoredProcedure;
    sql_command.Parameters.Add(new SqlParameter("@kid", kid));
    sql_reader = await sql_command.ExecuteReaderAsync();
    
    while (sql_reader.Read())
    {
        MyKidPhoto.Source = byteArrayToImage((byte[])sql_reader["student_photo"]),
    }
    

    Kindly help...

    Thanks

    Saturday, June 29, 2019 11:34 AM