Convert base64 string to image in C# on Windows Phone(在 Windows Phone 上将 base64 字符串转换为 C# 中的图像)
问题描述
我有一个 base64 字符串,我想将其转换为图像并将 Image 控件的 Source 设置为其结果.
I have a base64 string and I want convert that to an image and set the Source of an Image control to the result of that.
通常我会使用 Image.FromStream
来做到这一点,类似于:
Normally I would do that using Image.FromStream
, similar to this:
Image img;
byte[] fileBytes = Convert.FromBase64String(imageString);
using(MemoryStream ms = new MemoryStream())
{
ms.Write(fileBytes, 0, fileBytes.Length);
img = Image.FromStream(ms);
}
但是,Image.FromStream
方法在 Windows Phone 上不存在,随便搜索只会找到依赖于该方法的结果.
However, the Image.FromStream
method does not exist on Windows Phone, and a casual search only turns up results that depend on that method.
推荐答案
你可以使用这样的方法:
You can use a method like this:
public static BitmapImage base64image(string base64string)
{
byte[] fileBytes = Convert.FromBase64String(base64string);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
}
将图像添加到您的 XAML,例如:
Add an image to your XAML, such as this:
<Image x:Name="myWonderfulImage" />
然后您可以设置源,如下所示:
You can then set the source, like this:
myWonderfulImage.Source = base64image(yourBase64string);
这篇关于在 Windows Phone 上将 base64 字符串转换为 C# 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Windows Phone 上将 base64 字符串转换为 C# 中的图像


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01