如何从Windows Phone 8.1上的base64字符串中获取图像? 如果有帮助,我知道如何解码图像?
var imageBytes = Convert.FromBase64String(base64StringWithImageData);
var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
var decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, ms.AsRandomAccessStream());
但我无处可以找到如何从中获取图像。
我找到的所有解决方案都是针对wfp或pre win phone 8.1。 我需要这个在Windows商店应用程序中工作。 如果可能,我不想先将图像保存到磁盘然后加载它。
背景:我从自定义对象中的服务器获取图像(客户签名),其中图像存储为base64编码的字符串。因此,图像不会存储在网站或磁盘上。
更新 建议的答案对我不起作用:( 我稍微改了一下就可以编译了。以下代码来自建议的答案。
var imageBytes = Convert.FromBase64String(_orderEntity.Signature);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
ms.Write(imageBytes, 0, imageBytes.Length);
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(ms.AsRandomAccessStream());
signatureImage.Source = bitmapImage;
}
但是该代码抛出了一个"无法找到该组件。 (HRESULT异常:0x88982F50)" 谷歌sais意味着图像格式未知:(
这是我用作输入的(其中一个)字符串:
"iVBORw0KGgoAAAANSUhEUgAAAYwAAACQCAIAAACUOMtJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGpMAABqTAfnkTrYAABEeSURBVHhe7Z0J0JXTH8d/xVspWkSLmLK1UKORbUojMqVlZI+xJYRRg7xpTGWZIUwhGcxkKWRLiiJjD61ka6hRSZZUKr2RpaT3/z1u/+ut995znufe53nuOef5nmlMo+c+55zP79zv/Z1zfud3alRWVgoLCZAACdhKoKatDWO7SIAESEARoEhxHJAACVhNgCJltXnYOBIgAYoUxwAJkIDVBChSVpuHjSMBEqBIcQyQAAlYTYAiZbV52DgSIAGKFMcACZCA1QQoUlabh40jARKgSHEMkAAJWE2AImW1edg4EiABihTHAAmQgNUEKFJWm4eNIwESoEhxDJAACVhNgCJltXnYOBIgAYoUxwAJ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KgSIUlxudJgAQSJUCRShQ3KyMBEghLgCIVlhifJwESSJTA/wBcyHVBDaExMgAAAABJRU5ErkJggg=="
我可以在Windows窗体应用程序中反编译该字符串:
public static Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
var imageBytes = Convert.FromBase64String(base64String);
var ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
var image = Image.FromStream(ms, true);
return image;
}
和一个wpf app:
public static Image Base64ToImage(string base64String)
{
try
{
byte[] binaryData = Convert.FromBase64String(base64String);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();
Image img = new Image();
img.Source = bi;
return img;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return new Image();
}
那么为什么我不能让它在Windows Phone上工作?
答案 0 :(得分:2)
我找到了一个有效的解决方案!
var imageBytes = Convert.FromBase64String(base64String);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])imageBytes);
writer.StoreAsync().GetResults();
}
var image = new BitmapImage();
image.SetSource(ms);
}
在此处找到解决方案:Load, show, convert image from byte array (database) in Windows Phone 8.1 这里有一个解决方案:http://www.codeproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an