I tried to do it with this code but I get an ArgumentException with “ms” in byteArrayToImage function.
byteArrayToImage working correctly with byte[] but I have trouble when I get node value.
XML node contains a binary code.
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
foreach (var binary in xdoc.Root.Elements(ns + "binary"))
{
if (item.Value == "cover.jpg") MessageBox.Show(item.Value.ToString());
foreach (var binaryAttr in binary.Attributes())
{
if (binaryAttr.Value == "cover.jpg")
{
var s = GetBytes(binary.Value);
byteArrayToImage(s);
}
}
}
It’s not clear what you mean by “contains a binary code”. Usually, binary data in XML is stored in Base64… whereas you’re converting plain character data in a way which almost certainly wouldn’t work in XML.
I suspect you can remove your
GetBytesmethod and just use