Load bitmapImage from base64String(从 base64String 加载 bitmapImage)
问题描述
如何在 windows 8 中从 base64String 加载 bitmapImage?
How can I load a bitmapImage from base64String in windows 8?
我试过这个,但我没有成功.它曾经在windows phone上工作.有什么不同?
I tried this but I am not successful. It used to work on windows phone. What is different?
看来我必须使用函数 setsourceasync.当我使用它时,我需要将参数作为 IRandomMemory 传递,这是我无法做到的.如何做到这一点?
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;
    }
推荐答案
要为 SetSource 方法创建 IRandomAccessStream 对象,您需要使用 DataWriter.看看这段代码:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 base64String 加载 bitmapImage
				
        
 
            
        - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - 输入按键事件处理程序 2022-01-01
 
						
						
						
						
						
				
				
				
				