XML到图像的Blob字节数组

时间:2010-05-21 13:35:33

标签: c# image bytearray blob

我正在获取XML文件以生成预览,格式如下:

<PARAM> <LABEL>Preview 16x16</LABEL> <ID>{03F5C6D3-ABCD-4889-B3AA-C3524C62FA1C}</ID> <LAYER>-1</LAYER> <VALUE> <BLOB> /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8S EhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEU Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAAR CAAOABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG h4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl 5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA AgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYk NOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOE hYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk 5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDP+IXxH+Ilr4/1DRrLxZqUrjUTbMLKdtsz eZgLCgHyDHy4GSeee9dT4Z8G/FjxHrmla3Y3bRlRtl1iW9ztGTnlXLS47YG0nrXQa78Bda07 U7jXNM1nS9yGVhJLHJvk8wkEkchWAYjcuDzxggGuB8K6v49i8X28Wg+IodMkaUQFI4sQSEt1 eM5B42qOPlVVVcAV5M8NKdSMW/cV3vrfXyb/AB/I+4+sVUnPCUVeyV3Z3010923krdPU/9k= </BLOB> </VALUE> </PARAM>

我需要将<BLOB>部分转换为图片。我像这样访问元素值:

string clean = valueC.ElementAt(0).Value.Replace("\t", string.Empty).Replace("\n", string.Empty);

我尝试将其读入MemoryStream并转换为Image:

MemoryStream ms = new MemoryStream(blob, 0, blob.Length);
ms.Write(blob, 0, blob.Length);
Image i = Image.FromStream(ms);

通过这种方式,我在获取图像时得到“参数无效异常”。 我也尝试将其直接保存到文件中:

using (FileStream fs = new FileStream(label + ".jpg", FileMode.Create))
{
    fs.Write(blob, 0, blob.Length);
}

但是当我尝试打开生成的文件时,会显示有关损坏的消息。

我知道编码很重要,我已经尝试过ASCII,UTF-8,UTF-7和这个:

BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, clean);
ms.Seek(0, 0);
byte[] blob = ms.ToArray();

我不知道还能做什么。如果有人可以帮助我,我将不胜感激。

由于

1 个答案:

答案 0 :(得分:2)

您的数据似乎是Base64格式,因此您首先需要对其进行解码,然后将其另存为图像。