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# 中的图像
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
