从昨天开始,我正在尝试从字节数组中加载图像。我从NorthWind 数据库的雇员表中得到字节数组。我读过一些文章说在我们从 byte[] 转换为 ImageSource 之前应该删除一个大小为 78 的 OLE 标头。但它无法获得任何图像。这是我的转换器:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] data = value as byte[];
if (data != null)
{
MemoryStream ms = new MemoryStream();
int offset = 78;
BitmapImage img = new BitmapImage();
ms.Write(data, offset, data.Length - offset);
img.SetSource(ms);
ms.Close();
return img;
}
return null;
}
这是我在 XAML 中的图像定义
<Image Grid.Column="1" Height="147" HorizontalAlignment="Left" Margin="3,3,0,6" Name="photoImage" Source="{Binding Path=Photo, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource PhotoConverter1}}" Stretch="Fill" VerticalAlignment="Center" Width="137" DataContext="{Binding}" />
你能帮我弄清楚如何让它工作吗?