Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

how to convert byte[] to Image??

plz tell me i want to byte array convert in image

        imageSlider.Order = Convert.ToInt32(orderNo.Text);      //int convert
        imageSlider.IsActive = chekIsActive.IsChecked;             //bool convert


  • LandLuLandLu Member, Xamarin Team Xamurai

    What is the control you are using named imageSlider?
    But if you are meaning you want to consume an Image control in Xamarin and you want to display an image using bytes you could try:

    HttpClient client = new HttpClient();
    var response = await client.GetAsync("");
    var bytes = await response.Content.ReadAsByteArrayAsync();
    var stream = new MemoryStream(bytes);
    MyImage.Source = ImageSource.FromStream(() => stream);  
  • sandy3080sandy3080 Member ✭✭

    I want to code Xamarin Form

    ImageSlide imageslider is my class,,
    this is my form

    Order no

    Submit Button

    now i upload image
    take order no =1
    IsActive true=true
    but no idea how to convert image to byte[]
    When clicked submit button

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sandy3080 If you could detect which image is selected you could record its source.
    Let's assume you load images from the internet then you could get image path.
    Upload the image path or convert it to bytes as you want.

  • sandy3080sandy3080 Member ✭✭

    plz tell me code @LandLu

    public static byte[] ToByteArray(Image imageIn)
    var ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    return ms.ToArray();

    now i using this code but get error

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sandy3080 You can't convert the image control to stream directly. Once the stream has been passed to the StreamImageSource it will be released.
    What I mean above is you need to store the resource's path where you got the bytes.
    i.e. I obtained the bytes from the address so I needed to store this address in a list corresponding to your images.
    If you have five images to be displayed you need to create a list which contains five image paths.
    When one of the images has been seleceted you could retrieve the image path from the list due to the image's index.
    At last, get the bytes from the image path, upload it to your server.

  • lillianlillian Member ✭✭

    Please have a try, hope it can help you.

    private byte[] ToByteArray(Image image, ImageFormat format)
    using (var ms = new MemoryStream())
    image.Save(ms, format);
    var byData = new byte[ms.Length];
    ms.Position = 0;
    ms.Read(byData, 0, byData.Length);
    return byData;

  • sandy3080sandy3080 Member ✭✭

    @lillian your code is okk but method
    not found in Xamrin forms
    got error

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sandy3080 This is not a .Net Standard api:
    Image here in Xamarin.Forms is a standard control and you can't convert it directly to a byte array.
    You could try my approach to get the image path and convert it to what you need.

  • lillianlillian Member ✭✭

    maybe you can use FFImageLoading

    Here is an example:
    <ContentPage xmlns="" xmlns:x="" xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" x:Class="App1.MainPage"> <StackLayout> <ffimageloading:CachedImage x:Name="TestImage" Source="icon" /> <Button Text="Convert" Clicked="Button_OnClicked" /> </StackLayout> </ContentPage>

    private async void Button_OnClicked(object sender, EventArgs e)
    var imageBytes = await TestImage.GetImageAsJpgAsync();

Sign In or Register to comment.