将从 ByteStream 获得的 BitmapImage 绑定到 UWP C# 上的 ImageBrush
Binding a BitmapImage obtained from a ByteStream to an ImageBrush on UWP C#
所以我正在为通用 Windows 平台开发。我从网站上以 base64 字符串的形式提取图像,并使用 byte[] imageBytes = Convert.FromBase64String(srcString);
将其转换为字节数组,然后使用以下代码将其转换为 BitmapImage:
public async static Task<BitmapImage> ImageFromBytes(Byte[] bytes)
{
BitmapImage image = new BitmapImage();
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
await image.SetSourceAsync(stream);
}
return image;
}
这似乎正确地拉入了我的图像,因为正确填充了 BitmapImage 对象中图像的维度属性。问题是我需要使用带有以下 XAML 代码的图像画笔将此图像对象用作 GridView 项模板的背景:
<GridView.ItemTemplate>
<DataTemplate x:DataType="models:data" >
<StackPanel
Orientation="Horizontal"
HorizontalAlignment="Center"
Width="190"
Height="270"
BorderThickness="1" BorderBrush="DarkBlue"
>
<StackPanel.Background>
<ImageBrush Stretch="Fill" ImageSource="{x:Bind Image}"/>
</StackPanel.Background>
<StackPanel HorizontalAlignment="Center" Padding="5" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="Title:" FontWeight="Bold" />
<TextBlock Name="Block" Margin="5,0,0,0"
Text="{x:Bind Title}" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
我已经正确设置了后台代码,因为如果我将本地 URI 提供给图像绑定变量,那么一切都会完美无缺。但是,由于我制作的 BitmapImage 中的 URI 属性 为空,我无法只获取它的 URI 并将其用于绑定。将图像作为字节数组获取是我能够做到的唯一方法。有没有办法使用我通过字节流提取的图像并将其绑定到我的 XAML 代码?
谢谢,
肯尼
更改了图像源
ImageSource="{Binding Image}"
非常感谢克莱门斯!
使用
ImageSource="{Binding Image}"
而不是
ImageSource="{x:Bind Image}"
虽然 x:Bind
需要精确的类型匹配,但 Binding
利用来自多种源类型的内置转换(例如 string
、Uri
、byte[]
) 到 ImageSource
.
所以我正在为通用 Windows 平台开发。我从网站上以 base64 字符串的形式提取图像,并使用 byte[] imageBytes = Convert.FromBase64String(srcString);
将其转换为字节数组,然后使用以下代码将其转换为 BitmapImage:
public async static Task<BitmapImage> ImageFromBytes(Byte[] bytes)
{
BitmapImage image = new BitmapImage();
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
await image.SetSourceAsync(stream);
}
return image;
}
这似乎正确地拉入了我的图像,因为正确填充了 BitmapImage 对象中图像的维度属性。问题是我需要使用带有以下 XAML 代码的图像画笔将此图像对象用作 GridView 项模板的背景:
<GridView.ItemTemplate>
<DataTemplate x:DataType="models:data" >
<StackPanel
Orientation="Horizontal"
HorizontalAlignment="Center"
Width="190"
Height="270"
BorderThickness="1" BorderBrush="DarkBlue"
>
<StackPanel.Background>
<ImageBrush Stretch="Fill" ImageSource="{x:Bind Image}"/>
</StackPanel.Background>
<StackPanel HorizontalAlignment="Center" Padding="5" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="Title:" FontWeight="Bold" />
<TextBlock Name="Block" Margin="5,0,0,0"
Text="{x:Bind Title}" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
我已经正确设置了后台代码,因为如果我将本地 URI 提供给图像绑定变量,那么一切都会完美无缺。但是,由于我制作的 BitmapImage 中的 URI 属性 为空,我无法只获取它的 URI 并将其用于绑定。将图像作为字节数组获取是我能够做到的唯一方法。有没有办法使用我通过字节流提取的图像并将其绑定到我的 XAML 代码?
谢谢, 肯尼
更改了图像源
ImageSource="{Binding Image}"
非常感谢克莱门斯!
使用
ImageSource="{Binding Image}"
而不是
ImageSource="{x:Bind Image}"
虽然 x:Bind
需要精确的类型匹配,但 Binding
利用来自多种源类型的内置转换(例如 string
、Uri
、byte[]
) 到 ImageSource
.