如何在 Xamarin 中加载二进制图像

How load binary images in Xamarin

我正在使用 Xamarin Forms,我想加载带有图像单元的列表视图,同时我正在使用 XAML 绑定数据。 我的网络服务提供商 returns 我是图像的二进制代码,¿有人知道我如何转换它来显示图像吗?

这是我的 XAML 列表视图模板:

    <ListView x:Name="lv_products">

      <ListView.ItemTemplate>
        <DataTemplate>
          <ImageCell
                    Text="{Binding Name}"
                    Detail="{Binding Description}"
                    ImageSource="{Binding Image, Converter={StaticResource cnvImage}}">
          </ImageCell>
        </DataTemplate>

      </ListView.ItemTemplate>
    </ListView>

和转换器:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && value is byte[])
        {

            byte[] binary = (byte[])value;

            Image image = new Image();
            image.Source = ImageSource.FromStream(() => new MemoryStream(binary));

            return image.Source;
        }
        return null;
    }

但图片显示为空(透明)。

如果您有 URL 的 returns 图像文件,为什么不直接使用 URL 作为图像源?

  <ImageCell  Text="{Binding Name}"
              Detail="{Binding Description}"
              ImageSource="{Binding ImageURL}">
  </ImageCell>

您可以将字节数组转换为位图图像,并将该位图分配给 ImageView。我在 Xamarin.Android 中做了这个,不知道它是否适用于表单。

位图=BitmapFactory.DecodeByteArray(字节,0,byte.Length);

然后使用imageView.FromBitmap()来显示这张图片。

这是工作转换器。我使用 MemoryStream 和 ImageSource.FromStream.

public class ByteImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        var image = value as byte[];
        if (image == null)
            return null;
        return ImageSource.FromStream(() => new MemoryStream(image));
    }

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

这里是我的页面的示例

public partial class Page : ContentPage
{
    readonly ViewModel _bindingContext = new ViewModel();

    public Page()
    {
        InitializeComponent();

        BindingContext = _bindingContext;
        LoadImage();
    }

    private async void LoadImage()
    {
        var assembly = typeof (ByteImageConverter).GetTypeInfo().Assembly;
        var stream = assembly
              .GetManifestResourceStream("TestImage.c5qdlJqrb04.jpg");
        using (var ms = new MemoryStream())
        {
            await stream.CopyToAsync(ms);
            _bindingContext.Image = ms.ToArray();
        }
    }
}

public class ViewModel : INotifyPropertyChanged
{
    private byte[] _image;
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(
                             [CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public byte[] Image
    {
        get { return _image; }
        set
        {
            _image = value;
            OnPropertyChanged();
        }
    }
}