从base64String加载的BitmapImage [英] Load bitmapImage from base64String
问题描述
我如何能够装载的BitmapImage
从 base64String
在 Windows 8的
?
我试过,但我没有成功。它用来在Windows Phone上运行。什么是不同的?
看起来像我不得不使用功能setsourceasync。当我使用这一点,那么我需要传递参数作为IRandomMemory对此我无法做到。如何做到这一点?
公共静态的BitmapImage Base64ToImage(字符串base64String)
{
变种的BitmapImage =新的BitmapImage();
试
{
如果
{
VAR imageBytes = Convert.FromBase64String(base64String)(String.IsNullOrEmpty(base64String)!);
使用(VAR毫秒=新的MemoryStream(imageBytes,0,imageBytes.Length))
{
bitmapImage.SetSourcec(毫秒);
返回BitmapImage的;
}
}
}
赶上(例外五)
{
}
返回NULL;
}
要创建 IRandomAccessStream 的的SetSource 方式的对象,您需要使用的 DataWriter 。看看这个代码:
公共异步任务<&的BitmapImage GT;的getImage(字符串值)
{
如果(价值== NULL)
返回NULL;
VAR缓冲= System.Convert.FromBase64String(值);
使用(InMemoryRandomAccessStream毫秒=新InMemoryRandomAccessStream())
{使用
(DataWriter作家=新DataWriter(ms.GetOutputStreamAt(0)))
{
作家。 WriteBytes(缓冲液);
等待writer.StoreAsync();
}
变种形象=新的BitmapImage();
image.SetSource(毫秒);
返回图像;
}
}
How can I load a bitmapImage
from base64String
in windows 8
?
I tried this but I am not successful. It used to work on windows phone. What is different?
Looks like I have to use the function setsourceasync. When I use that, then I am required to pass the parameter as IRandomMemory which I am unable to do. How to do this?
public static BitmapImage Base64ToImage(string base64String)
{
var bitmapImage = new BitmapImage();
try
{
if (!String.IsNullOrEmpty(base64String))
{
var imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
bitmapImage.SetSourcec(ms);
return bitmapImage;
}
}
}
catch (Exception e)
{
}
return null;
}
To create an IRandomAccessStream object for the SetSource method, you need to use a DataWriter. Take a look to this code:
public async Task<BitmapImage> GetImage(string value)
{
if (value == null)
return null;
var buffer = System.Convert.FromBase64String(value);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes(buffer);
await writer.StoreAsync();
}
var image = new BitmapImage();
image.SetSource(ms);
return image;
}
}
这篇关于从base64String加载的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!