【问题标题】:loading image from Employees table of Northwind database从 Northwind 数据库的员工表中加载图像
【发布时间】:2012-03-19 07:30:23
【问题描述】:

从昨天开始,我正在尝试从字节数组加载图像。 我从NorthWind database 的Employees 表中得到字节数组。 我读到一些articles 说在我们从 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}" />

你能帮我弄清楚如何让它工作吗?

【问题讨论】:

    标签: image converter silverlight-5.0 northwind


    【解决方案1】:

    http://en.wikipedia.org/wiki/BMP_file_format

    您应该能够查看数组并使用位图中的标头信息查看位图从何处开始。

    此代码还可以帮助您: http://blogs.msdn.com/b/pranab/archive/2008/07/15/removing-ole-header-from-images-stored-in-ms-access-db-as-ole-object.aspx

    【讨论】:

    • 在 Windows 中,标题总是以 BM 或 ASCII “424D”开头,也可能是“4D42”,具体取决于数据的大端或小端格式。
    • 感谢您的回答。我去了你的链接并使用了给定的代码,但图像仍然没有加载(没有图像显示在我的图像控件中。)你的第二个链接中的一段代码确认偏移量是 78。
    • 您能否将位图标题添加到您的问题中。可能格式不正确。
    • 这是一个silverlight 应用程序。我正在使用 Silverlight 5
    • 抱歉,因为在您最初的问题中看到了标签,所以我删除了那个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2018-10-29
    • 2014-11-12
    相关资源
    最近更新 更多