Silverlight 4.0:如何将 byte[] 转换为图像? [英] Silverlight 4.0: How to convert byte[] to image?

查看:17
本文介绍了Silverlight 4.0:如何将 byte[] 转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
        return image;
    }

我想将 byte[] 转换为图像,但是 Silverlight 不支持 System.Drawing.Image.有什么选择吗?

I want to convert byte[] to image, however System.Drawing.Image is not supported in Silverlight. Any alternative?

推荐答案

您需要创建一个 ImageSource 并将其分配给 Image 控件或使用 ImageBrush 设置在背景上.BitmapImage 位于 System.Windows.Media.Imaging 命名空间中.

You need to create an ImageSource and assign that to an Image control or use an ImageBrush to set on the background. BitmapImage is located in the System.Windows.Media.Imaging namespace.

        byte[] imageBytes = Convert.FromBase64String(base64String);
        using (MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length))
        {
            BitmapImage im = new BitmapImage();
            im.SetSource(ms);
            this.imageControl.Source = im;
        }

或用于 ImageBrush

or for the ImageBrush

        byte[] imageBytes = Convert.FromBase64String(base64String);
        using (MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length))
        {
            BitmapImage im = new BitmapImage();
            im.SetSource(ms);
            imageBrush.ImageSource = im;
            this.BoxBorder.Background = imageBrush;
        }

这篇关于Silverlight 4.0:如何将 byte[] 转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆