简体   繁体   English

从base64string显示图像

[英]Displaying image from base64string

I'm displaying an image from a base 64 string that came from an API. 我正在显示来自API的基本64字符串的图像。 The problem is, the image is not being displayed. 问题是,图像没有显示。

Here's the code: 这是代码:

profilePictureImg.Source = GetUserImage(user.MobileNumber);


private BitmapImage GetUserImage(string phoneNumber)
    {
        BitmapImage bitmapImage = new BitmapImage();

        var baseAddress = "http://192.168.0.103/vchatapi/api/Images/" + phoneNumber;

        var http = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new System.Uri(baseAddress));
        http.Accept = "application/json";
        http.ContentType = "application/json";
        http.Method = "GET";

        var response = http.GetResponse();

        var stream = response.GetResponseStream();
        var sr = new StreamReader(stream);
        var content = sr.ReadToEnd();
        var y ="";
        var x = y.FromJson(content);

        byte[] binaryData = Convert.FromBase64String(x);

        using (MemoryStream ms = new MemoryStream(binaryData, 0, binaryData.Length))
        {
            ms.Write(binaryData, 0, binaryData.Length);
            bitmapImage.StreamSource = ms;

        }
        return bitmapImage;
    }

Any Ideas?? 有任何想法吗?? Thanks! 谢谢!

EDIT: 编辑:

Got the fix. 得到了修复。 For some reason, it requires to call BeginInit and EndInit. 出于某种原因,它需要调用BeginInit和EndInit。

The image may be decoded as shown in this answer : 图像可以解码,如下面的答案所示:

var binaryData = Convert.FromBase64String(x);
var bitmapImage = new BitmapImage();

using (var stream = new MemoryStream(binaryData))
{
    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = stream;
    bitmapImage.EndInit();
}

The reason why you have to use BeginInit and EndInit is explained in the Remarks section of the BitmapImage MSDN documentation : 您必须使用BeginInitEndInit的原因在BitmapImage MSDN文档的备注部分中进行了解释:

BitmapImage implements the ISupportInitialize interface to optimize initialization on multiple properties. BitmapImage实现ISupportInitialize接口以优化多个属性的初始化。 Property changes can only occur during object initialization. 属性更改只能在对象初始化期间发生。 Call BeginInit to signal that initialization has begun and EndInit to signal that initialization has completed. 调用BeginInit表示初始化已经开始,EndInit表示初始化已完成。 After initialization, property changes are ignored. 初始化后,将忽略属性更改。

This may be one of those cases where it pays not to dispose the stream too eagerly; 这可能是其中一种情况,即急于处理流; also, the Write here is unnecessary: you already added the data via the constructor. 此外, Write此处是不必要的:您已经通过构造函数添加了数据。 So just: 所以就:

bitmapImage.StreamSource = new MemoryStream(binaryData);
return bitmapImage;

does that work? 那样有用吗?

You can try the following 您可以尝试以下方法

byte[] binaryData = Convert.FromBase64String(x);
using (MemoryStream ms = new MemoryStream(binaryData))
{
    bitmapImage = (Bitmap)Image.FromStream(ms);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM