We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

İmage byte to array convert

Hello there. I wanted to print the string array that came with the web API, byte to array, and print it. Normally I have xamarin.forms code that I wrote but I do not know how to do it here. Examples of related code are my printer. I'm waiting for your help. The code I wrote is returning to the wrong place. Could you tell me what is wrong with something missing from my eyes? And most importantly does synfusion support this event? maybe it is not because of this.

  <ContentPage.Resources>
        <ResourceDictionary>
            <local:XXX x:Key="xxx" />
        </ResourceDictionary>
    </ContentPage.Resources>

  
    <ContentPage.Content>

        <syncfusion:SfDataGrid x:Name="grid" ItemsSource="{Binding .}" ColumnSizer="Star"
            AutoGenerateColumns="False" AllowResizingColumn="True" RowHeight="100" GridTapped="grid_GridTapped" GridDoubleTapped="grid_GridDoubleTapped" SelectionChanging="grid_SelectionChanging" AllowDraggingRow="True" AllowSwiping="True"  GridStyle="{StaticResource dark}" >
            
        <syncfusion:SfDataGrid.Columns x:TypeArguments="syncfusion:Columns">
            <syncfusion:GridTextColumn HeaderText="Stok Kodu" 
                                   MappingName="StkKod"/>
            <syncfusion:GridTextColumn HeaderText="Stok Cins"
                                   MappingName="StkCins" />
            <syncfusion:GridTextColumn MappingName="StkFiyat"  HeaderText="Stok Fiyat"/>
            <syncfusion:GridTextColumn HeaderText="Stok Durum"
                                   MappingName="StkDurum" />
                <syncfusion:GridImageColumn HeaderText="Resim" MappingName="resim , Converter={StaticResource xxx}" ValueBinding="{Binding resim, Converter={StaticResource xxx}}"/>
            
        </syncfusion:SfDataGrid.Columns>
    </syncfusion:SfDataGrid>





 public class XXX : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ImageSource retSource = null;

            ////if (value != null)
            ////{
            ////    byte[] imageAsBytes = (byte[])System.Text.Encoding.UTF8.GetBytes(value.ToString());
            ////    byte[] decodedByteArray = System.Convert.FromBase64String(Encoding.UTF8.GetString(imageAsBytes, 0, imageAsBytes.Length));
            ////    retSource = ImageSource.FromStream(() => new MemoryStream(decodedByteArray));
            ////}



            if (value != null)
            {
                byte[] imageAsBytes = (byte[])System.Text.Encoding.UTF8.GetBytes(value.ToString());
                retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
            }

            return retSource;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

1 Reply

AN Ashok N Syncfusion Team September 20, 2017 09:07 PM UTC

Hi Damla, 
 
Thanks for contacting Syncfusion support, 
 
You can achieve your requirement with help of DisplyBinding. Please refer the below code example for more details. 
 
//Using DisplyBinding in GridColumn 
<syncfusion:GridImageColumn MappingName="IsAvailable"  
                            DisplayBinding="{Binding IsAvailable, 
                    Converter={StaticResource imageConverter}}" /> 
//Converter class 
public class ImageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        var data = value as byte[]; 
        if (data != null) 
        { 
            return ImageSource.FromStream(() => new MemoryStream(data)); 
        } 
        return null; 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 
 
//Convert image into byte[] 
private byte[] GetImageBytes(StreamImageSource imagesource) 
{ 
    byte[] ImageBytes; 
    using (var memoryStream = new System.IO.MemoryStream()) 
    { 
        var stream  =  imagesource.Stream.Invoke(new System.Threading.CancellationToken()).Result; 
        stream.CopyTo(memoryStream); 
        stream = null; 
        ImageBytes = memoryStream.ToArray(); 
    } 
    return ImageBytes; 
} 
 
 
Regards, 
Ashok 


Loader.
Live Chat Icon For mobile
Up arrow icon