【问题标题】:How to convert byte array to image from web API?如何从 Web API 将字节数组转换为图像?
【发布时间】:2019-10-22 09:14:37
【问题描述】:

我的 API 将许多图像转换为字节数组并返回字节,但我无法再次转换为图像。我打算在一些列表视图中显示我的 API 中的所有图像。

我试过了,但我无法获得任何图像,我试图至少获得一张。

        var url = "https://appmaragogi.com.br/api/Files/Upload?Id=ChurrascariaEstreladoMar";

        Byte[] imageAsBytes = client.GetByteArrayAsync(url).Result;

        MemoryStream stream1 = new MemoryStream(imageAsBytes);
        teste.Source = ImageSource.FromStream(() => { return stream1; });

我想在列表视图中显示所有图像,或者一种获取所有图像的方法

我在 Xaml 中使用图像来显示图像

【问题讨论】:

  • 您确定您的 byte[] 是有效的图像数据吗?尝试将其写入文件并使用图像查看器进行测试。
  • 你知道你收到的数据是否包含头信息吗?如果是这样,您可能还应该知道图像/文件类型(jpeg、png 等)。如果它们是原始像素字节,那么您可以轻松地将数组复制到图像对象
  • 您只需从控制器和 UI (MVC) 端返回字节数组和文件类型(媒体类型),您需要根据 fie 类型显示图像
  • 我的 byte[] 包含多个图像数据,我为 jpeg 设置了 MediaTypeHeaderValue
  • 如果您的数据包含多个图像,那么您需要有一些机制来判断每个图像的开始/停止位置并分别保存它们。您不能只创建一个流并期望 Forms 解决它。

标签: c# asp.net-mvc xamarin


【解决方案1】:

您可以尝试使用从IValueConverter 派生的转换器,它可以根据字节数组创建图像。

你可以参考我this的帖子:

主要代码如下:

ByteArrayToImageSourceConverter.cs

 public class ByteArrayToImageSourceConverter : IValueConverter
 {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    ImageSource retSource = null;
    if (value != null)
    {
        byte[] imageAsBytes = (byte[])value;
        var stream = new MemoryStream(imageAsBytes);
        retSource = ImageSource.FromStream(() => stream);
    }
    return retSource;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
  }
}

ImagesModel.cs

public class ImagesModel
{
   // other fields

   public byte[] PlayerImage { get; set; }
 }

xaml(一个使用示例):

<ContentPage.Resources>
<ResourceDictionary>
    <myformapp1:ByteArrayToImageSourceConverter x:Key="ByteArrayToImage" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Margin="5">
<CollectionView x:Name="collectionView"
                ItemsSource="{Binding YoudataList}"> <!--changd to your dataList-->
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid Padding="10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Image Grid.RowSpan="2"  
                       x:Name="PlayerPic"
                       Source="{Binding PlayerImage, Converter={StaticResource ByteArrayToImage}}"
                       Aspect="AspectFill"
                       HeightRequest="60" 
                       WidthRequest="60" />
                <Label Grid.Column="1" 
                       Text="test1" 
                       FontAttributes="Bold" />
                <Label Grid.Row="1"
                       Grid.Column="1" 
                       Text="test2"
                       FontAttributes="Italic" 
                       VerticalOptions="End" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>

【讨论】:

    【解决方案2】:

    我认为你只需要将它保存在本地文件中,你唯一需要检查的是字节数组中图像的格式是否正确

    File.WriteAllBytes(nameLocalFile, imageAsBytes);
    

    【讨论】:

    • 我使用 Environment.GetFolderPath(Environment.SpecialFolder.Personal) 保存在本地文件中,但没有保存任何图像
    猜你喜欢
    • 1970-01-01
    • 2011-04-17
    • 2011-06-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多