0

我正在尝试将 varbinary 转换为 silverlight 项目中的图像。

首先,我从服务中的数据库中获取二进制文件。

    [OperationContract]
    public byte[] getAfbeelding(int id)
    {

        var query = (from p in dc.Afbeeldings
                     where p.id == id
                     select p.source).Single();

        byte[] source = query.ToArray();

然后我尝试使用 StackOverflow 上的代码将 varbinary 转换为图像:

    public static string convertToImage(byte[] source)
    {
        MemoryStream ms = new MemoryStream(source);
        Image img = Image.FromStream(ms);
        return img.Source.ToString();
    }

但事实证明,silverlightImage没有,我尝试了在这个线程.FromStream中找到的所有示例,但没有一个在 silverlight 中工作。

'System.Windows.Controls.Image' does not contain a definition for 'FromStream'

所以,是的,我有点迷路了,不知道该怎么办。关于如何在 Silverlight 中执行此操作的任何想法?

4

2 回答 2

1

你应该看看 WriteableBitmap。

在codepelexNuget上有一组相当不错的免费扩展

于 2011-11-26T16:46:26.813 回答
1

你几乎是对的。以下代码应该是您所需要的:

var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;

whereimageData是类型byte[]newImage是您要更新的图像。

于 2011-11-26T16:47:35.100 回答