如何将图像显示在图片框中包含在字符串

时间:2016-06-30 09:22:55

标签: c# winforms

我有一个图像字符串包含在XML文件中,并希望将字符串转换为图像。我使用了以下代码给出错误:参数无效。

以下代码是上面的错误:

    Byte[] data = new Byte[0];
    byte[] array = Encoding.ASCII.GetBytes("D:\notimg.txt");
    MemoryStream mem = new MemoryStream(array);
    pictureBox1.InitialImage = null;
    pictureBox1.Image = Image.FromStream(mem);

下面是我需要转换为图像并显示到图片框中的XML标记中包含的数据,我已将下面的数据放入记事本文件中并删除了XML标记。请任何人指导如何将以下数据显示在图像中。提前谢谢。

<photograph>/9j/4AAQSkZJRgABAQECWAJYAAD/4QBuRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAZKGAAcAAAA6AAAALAAAAABVTklDT0RFAABDAHIAZQBhAHQAZQBkACAAYgB5ACAAQQBjAGMAdQBTAG8AZgB0ACAAQwBvAHIAcAAu/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgBwgFeAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBA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iu+0fWIdTh+VgJUHzLXlCSEY7+xFX7DUJbOdZoppRRVDR//2Q==</photograph>

2 个答案:

答案 0 :(得分:0)

又快又脏:

  var s = File.ReadAllText(@"d:\file.txt");
  s = s.Replace("<photograph>", string.Empty).Replace("</photograph>", string.Empty);
  var b = Convert.FromBase64String(s);
  using (var mem = new MemoryStream(b))
  {
    pictureBox1.Image = Image.FromStream(mem);
  }

答案 1 :(得分:0)

这是Base64

首先转换为byte []

然后使用

public System.Drawing.Image Base64ToImage(byte[] imageBytes)
{
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
    ms.Write(imageBytes, 0, imageBytes.Length);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
    return image;
} 

然后你会得到形象..!:)