【问题标题】:How to convert jpeg data to image in UWP如何在 UWP 中将 jpeg 数据转换为图像
【发布时间】:2018-01-15 21:03:43
【问题描述】:
XDocument xdoc = XDocument.Parse(rr.Nodes[1].ToString(), LoadOptions.None);
xdoc.Declaration = new XDeclaration("1.0", "UTF - 8", "yes");


var q = from b in xdoc.Descendants("PictureNo")

        select b.Value;

foreach (string item in q.ToList())
    textBox2.Text = item;

if (textBox2.Text == String.Empty)
{

    Byte[] data2 = new Byte[0];
    data2 = (Byte[])(Convert.FromBase64String(textBox2.Text));
    string base64String1 = Convert.ToBase64String(data2, 0, data2.Length);
    image.Source = "data:image/jpeg;base64," + base64String1;

【问题讨论】:

  • 您好,ASP.NET 同样的问题可以如下代码处理:
  • 数据集 dataSet = new DataSet();数据适配器。填充(数据集); if (dataSet.Tables[0].Rows.Count == 1) { Byte[] data = new Byte[0]; data = (Byte[])(dataSet.Tables[0].Rows[0]["PictureNo"]); string base64String = Convert.ToBase64String(data, 0, data.Length); Image2.ImageUrl="data:image/jpeg;base64," + base64String; Image1.ImageUrl="data:image/jpeg;base64," + base64String; }
  • 如果您有具体问题,请考虑学习 how-to-askon topic ,提供符合 How to create a Minimal, Complete, and Verifiable example 的代码以及您的代码无法满足的异常/期望,我相信 SO 会有所帮助你出去。
  • 如果您想为您的问题添加有价值的信息,edit 问题,请不要使用 cmets。
  • 您好,如何在 UWP 中将 JPEG 数据转换为图像?

标签: image uwp jpeg


【解决方案1】:

您可以使用以下方法将Base64字符串转换为BitmapImage,然后您可以将其设置为Image.Source

public async Task<BitmapImage> Base64ToBitmapAsync(string source)
{
    var byteArray = Convert.FromBase64String(source);            
    BitmapImage bitmap = new BitmapImage();
    using (MemoryStream stream = new MemoryStream(byteArray))
    {
        await bitmap.SetSourceAsync(stream.AsRandomAccessStream());
    }
    return bitmap;
}

您首先从Base64 源创建一个字节数组,然后将其用作MemoryStream,作为BitmapImage 的源加载。

然后你只需这样做:

image.Source = await Base64ToBitmapAsync( Convert.FromBase64String(textBox2.Text) );

【讨论】:

  • 非常感谢。 image.Source = 等待 Base64ToBitmapAsync(Textbox2.Text);足够了,它正在工作
  • 您好,很高兴为您提供帮助!如果这解决了您的问题,请考虑接受答案,以便问题得到解决 :-) 您可以通过单击答案左侧箭头下方的灰色勾号来完成。
  • 不管 Base64 到 ByteArray 的转换如何,我发现这个答案更有帮助(来自不同但类似的问题)stackoverflow.com/a/46679185/997940
  • 我认为在这种情况下base64是问题的关键部分,链接的不处理base64 ...
猜你喜欢
  • 2018-08-20
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 2018-09-14
相关资源
最近更新 更多