在 Windows 8 应用程序中使用 C# 读取 Base64 图像 [英] Read Base64 image with C# in a Windows 8 app

查看:22
本文介绍了在 Windows 8 应用程序中使用 C# 读取 Base64 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to display an image on a Windows 8 app. The image data is gathered from a web service, and provided as a Base64 encoded string.

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

I found the following on Stack Overflow:

如何在 WPF 中读取 base64 图像?

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

However, when I come to use the BitmapImage Class, I can't seem to access System.Windows.Media.Imaging, even though the following Microsoft documentation leads us to believe that it is available for use in .NET 4.5, and with Windows 8 apps:

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

非常感谢您的帮助.

推荐答案

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

The classes you want are in the Windows.UI.Xaml.Media.Imaging namespace. Here is an example of taking a Base64 image and creating an image out of it ...

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 图像复制到答案中.

I could not copy a full Base64 image into the answer.

这篇关于在 Windows 8 应用程序中使用 C# 读取 Base64 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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