0

I'm having trouble loading an image "string" in the PictureBox control. I am not able to find a way to charge it and do not know if it is possible to do it that way. I tried the following code, but without success:

1st

var s= "0x89504E470D0A1A0A0000000D49484452000000900000005B0802000000130B9B9 8000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000013B49444154785EEDD6410D00211443418470C4BF333C808977F8C9242B8094CE9675F7F10D4A600D3AABA3FE045CD8B01F8C0B736146B14C8030C2CA7E7977104618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB863DFFBF32F5C2C8C5B40000000049454E44AE426082";

picture.Image = Base64ToImage(s);

static Image Base64ToImage(string base64String)

{

     byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream ms = new MemoryStream(imageBytes);
   return Image.FromStream(ms, true);

}

Can anyone help!?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
sapc1313041
  • 21
  • 1
  • 6
  • 1
    What file format is that image supposed to be? – keyboardP Aug 22 '13 at 17:00
  • I fail to see how this is related to XML. – Federico Berasategui Aug 22 '13 at 17:05
  • The problem is that my program will not have access to the database and I'll load the XML that contains the image, the XML is fed by another system that replicates the image information from the DataBase to the document, in the end I will have a string "s" with the image in bytes and takes loads there in picturebox. – sapc1313041 Aug 22 '13 at 17:16

2 Answers2

2

This seems to be a red rectangle 91*144 if decoded properly.

  1. remove 0x and space from string.

  2. convert string to byte[] - I used converter found on StackOverFlow the one by CainKellye (How can I convert a hex string to a byte array?)

string s = "89504E470D0A1A0A0000000D49484452000000900000005B0802000000130B9B98000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000013B49444154785EEDD6410D00211443418470C4BF333C808977F8C9242B8094CE9675F7F10D4A600D3AABA3FE045CD8B01F8C0B736146B14C8030C2CA7E7977104618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB863DFFBF32F5C2C8C5B40000000049454E44AE426082";

byte[] imageBytes = StringToByteArrayFastest(s);

MemoryStream ms = new MemoryStream(imageBytes);

Bitmap bmp = (Bitmap)Image.FromStream(ms);

pictureBox1.Image = bmp; 
  1. and the result is: enter image description here
Community
  • 1
  • 1
Na Na
  • 818
  • 3
  • 13
  • 19
0

I don't think s is a base 64 string, it looks more like hexadecimal - it even still has 0x in front of it and the 'digits' are no higher than F. You should definitely remove the 0x in front. To get a base 64 string, you could use the code from this website (I haven't tested it):

public static string ConvertHexStringToBase64(string hexString)
{
    if (hexString.Length % 2 > 0)
        throw new FormatException("Input string was not in a correct format.");
    if (Regex.Match(hexString, "[^a-fA-F0-9]").Success == true)
        throw new FormatException("Input string was not in a correct format.");
    byte[] buffer = new byte[hexString.Length / 2];
    int i=0;
    while (i < hexString.Length) {
        buffer[i / 2] = byte.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
        i += 2;
    }
return Convert.ToBase64String(buffer);
}
Timo
  • 263
  • 3
  • 8