在Windows8应用程序中使用C#读取Base64图像



我正在尝试在Windows 8应用程序上显示图像。图像数据是从web服务收集的,并作为Base64编码的字符串提供。

我在Stack Overflow上发现了以下内容:

如何在WPF中读取base64图像?

然而,当我使用BitmapImage类时,我似乎无法访问System.Windows.Media.Imaging,尽管以下Microsoft文档使我们相信它可以在.NET 4.5和Windows 8应用程序中使用:

http://msdn.microsoft.com/en-us/library/ms619218.aspx

非常感谢你的帮助。

所需的类位于Windows.UI.Xaml.Media.Imaging命名空间中。下面是一个使用Base64图像并从中创建图像的示例。。。

var img = @"/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQ ... "; // Full Base64 image as string here
var imgBytes = Convert.FromBase64String(img);
var ms = new InMemoryRandomAccessStream();
var dw = new Windows.Storage.Streams.DataWriter(ms);
dw.WriteBytes(imgBytes);
await dw.StoreAsync();
ms.Seek(0);
var bm = new BitmapImage();
bm.SetSource(ms);
// img1 is an Image Control in XAML
bm.ImageOpened += (s, e) => { this.img1.Source = bm; };
bm.ImageFailed += (s, e) => { Debug.WriteLine(e.ErrorMessage); };

我无法将完整的Base64图像复制到答案中。

最新更新