How to Get the bytes from the ImageSource in Xamarin Forms ?

SmartManSmartMan INMember ✭✭✭

Hi All,

I could able to convert the bytes into ImageSource.

Can you please suggest how to Convert the ImageSource into bytes

Thanks

Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @SmartMan

    This question (or something very similar) has been asked many, many times in the forum. If you Google "xamarin forms image to byte array" you will find the threads. Note that I've put image not imagesource in the search criteria - I'm sure you know how to populate an Image from an ImageSource.

    One answer that pops up can be found at https://forums.xamarin.com/discussion/127844/how-to-convert-image-to-byte-array
    I haven't tested it, but take a look

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    @SmartMan
    Try the code, ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
    ```
    Image image = new Image();
    image.Source = ImageSource.FromStream(() => new MemoryStream(new byte[] { }));
    ````
    Check the link: https://forums.xamarin.com/discussion/comment/62884/#Comment_62884

  • SmartManSmartMan INMember ✭✭✭

    Hi @yelinzh I want bytes from the ImageSource.

  • JohnHardmanJohnHardman GBUniversity mod

    @SmartMan said:
    Hi @yelinzh I want bytes from the ImageSource.

    Did you try the code at the link I posted above?

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    Sorry for my mistake,have a try the code.

    StreamImageSource streamImageSource  = (StreamImageSource) some image source...
    System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
    Task<Stream> task = streamImageSource.Stream(cancellationToken);
    Stream stream = task.Result;
    

    Check the link: https://stackoverflow.com/a/37491412

  • SmartManSmartMan INMember ✭✭✭

    HI @JohnHardman I am not sure about how to get the image from the imagesource..

  • SmartManSmartMan INMember ✭✭✭

    Hi @yelinzh Suppose if my image source is not a type streamimagesource .will it work ?

  • JohnHardmanJohnHardman GBUniversity mod

    @SmartMan said:
    HI @JohnHardman I am not sure about how to get the image from the imagesource..

    See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows

    You use the Source property of Image.

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    Suppose if my image source is not a type streamimagesource .will it work ?

    The .Stream property exists in StreamImageSource but not in ImageSource. StreamImageSource extends from ImageSource. You can convert ImageSource to StreamImageSource.

    Tutorial: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.imagesource?view=xamarin-forms

  • SmartManSmartMan INMember ✭✭✭

    HI @JohnHardman Actually i want to get the Bytes from the Imagesource and need to create pdf using that.

    Scanbot plugin,Once we take the pic,It will give us imagesource.I have to convert those imagesource into bytes

  • JohnHardmanJohnHardman GBUniversity mod

    @SmartMan said:
    HI @JohnHardman Actually i want to get the Bytes from the Imagesource and need to create pdf using that.

    Scanbot plugin,Once we take the pic,It will give us imagesource.I have to convert those imagesource into bytes

    The first link I posted above converts from an Image to bytes.
    The second link I posted above tells you how to get an Image from an ImageSource.
    Combined, they will convert an ImageSource to bytes. It only takes a few lines of code and they are all provided by those two links.

  • SmartManSmartMan INMember ✭✭✭
    edited May 13

    @JohnHardman I got Something.Let me check

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    What's the result? Have you made it?

  • ajaxerajaxer AUMember ✭✭

    Obviously it is still unclear how to do this functionality. I also was looking at how to achieve this and being confused by all the non-working suggestions.
    I was looking the wrong way for a solution, going from MediaFile to ImageSource and attempting to get a byte array.

    What worked was to go further back up to how the MediaFile was created in the first place.
    You can get a byte array from the Stream of the file that is the source.

    var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions { PhotoSize = PhotoSize.Small });
    
    byte[] imageArray = null;
    if (file != null)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            var stream = file.GetStream();
            stream.CopyTo(ms);
            imageArray = ms.ToArray();
        }
    }
    

    From a byte array it is easy to get an Image.
    retSource = ImageSource.FromStream(() => new MemoryStream(imageArray ));

Sign In or Register to comment.