windows phone 7应用程序中的byte []到图像转换

时间:2011-04-16 11:41:47

标签: windows-phone-7 c#-4.0

我正在为Windows Phone 7做一个应用程序。 应用程序从数据库访问映像(Sql server 2008)。 数据存储在数据类型'image'中。我想显示图像。 我使用以下代码

     public object Convert(object value, Type targetType, object parameter,           System.Globalization.CultureInfo culture)
    {
        byte[] data;
        BitmapImage empImage = new BitmapImage(); 
        Stream mm;
        data = (byte[])value;
        mm = new MemoryStream(data);
        mm.Position = 0;
        BinaryReader BR = new BinaryReader(mm);
        byte[] image=BR.ReadBytes(data.Length);
        mm = new MemoryStream(image);
        //empImage.SetSource(mm);
        return empImage;
    }

但是注释行(empImage.SetSource(mm);)中存在“未指定”错误。

请帮助我......

1 个答案:

答案 0 :(得分:0)

BitmapImage.SetSource接受Stream(如果之后不需要立即访问字节,则可以省略CreateOptions):

public object Convert(object value, Type targetType, object parameter,
       System.Globalization.CultureInfo culture)
{
    byte[] data = (byte[])value;

    using (MemoryStream stream = new MemoryStream(data))
    {
        BitmapImage image = new BitmapImage
        {
            CreateOptions = BitmapCreateOptions.None
        };

        image.SetSource(stream);

        return image;
    }
}

此外,我认为IValueConverter不适合此类代码。

最后,image数据库类型已被弃用,转而使用varbinary(MAX)