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
        imageSlider.Image=?????

Answers

  • 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("https://sample-videos.com/img/Sample-jpg-image-50kb.jpg");
    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

    image
    Order no
    IsActive

    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 https://sample-videos.com/img/Sample-jpg-image-50kb.jpg 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);
    ms.Close();
    return byData;
    }
    }

  • sandy3080sandy3080 Member ✭✭

    @lillian your code is okk but
    image.save() method
    not found in Xamrin forms
    got error

  • LandLuLandLu Member, Xamarin Team Xamurai

    @sandy3080 This is not a .Net Standard api: https://docs.microsoft.com/en-us/dotnet/api/system.drawing.image.save?redirectedfrom=MSDN&view=netframework-4.8#System_Drawing_Image_Save_System_String_System_Drawing_Imaging_ImageCodecInfo_System_Drawing_Imaging_EncoderParameters_
    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="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 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.