Windows窗体 - 从xml加载picturebox.image

时间:2013-08-22 16:35:28

标签: c# winforms picturebox

我在PictureBox控件中加载图像“string”时遇到问题。 我无法找到充电方式,也不知道是否可以这样做。 我尝试了以下代码,但没有成功:

第一

  
    
      

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);
     

}

任何人都可以帮忙!?

2 个答案:

答案 0 :(得分:2)

如果正确解码,这似乎是一个红色矩形91 * 144。

  1. 从字符串中删除0x和空格。

  2. 将字符串转换为byte [] - 我在StackOverFlow上找到了转换器CainKellye(How can I convert a hex string to a byte array?

  3. 字符串s = “89504E470D0A1A0A0000000D49484452000000900000005B0802000000130B9B98000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000013B49444154785EEDD6410D00211443418470C4BF333C808977F8C9242B8094CE9675F7F10D4A600D3AABA3FE045CD8B01F8C0B736146B14C8030C2CA7E7977104618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB86114618616502841156F6CB863DFFBF32F5C2C8C5B40000000049454E44AE426082”;

    byte[] imageBytes = StringToByteArrayFastest(s);
    
    MemoryStream ms = new MemoryStream(imageBytes);
    
    Bitmap bmp = (Bitmap)Image.FromStream(ms);
    
    pictureBox1.Image = bmp; 
    
    1. ,结果是:enter image description here

答案 1 :(得分:0)

我认为s不是64字符串,它看起来更像十六进制 - 它前面仍然有0x,而且'数字'不高于F.绝对应该删除前面的0x。要获得base 64字符串,可以使用this website中的代码(我还没有测试过):

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);
}