[英]WindowsForm - Load picturebox.image from a xml
我在PictureBox控件中加载图像“string”时遇到问题。 我无法找到充电方式,也不知道是否可以这样做。 我尝试了以下代码,但没有成功:
1
var s =“0x89504E470D0A1A0A0000000D49484452000000900000005B0802000000130B9B9”;
picture.Image = Base64ToImage( s );
static Image Base64ToImage(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes); return Image.FromStream(ms, true);
}
任何人都可以帮忙!?
如果正确解码,这似乎是一个红色矩形91 * 144。
从字符串中删除0x和空格。
将字符串转换为byte [] - 我使用了StackOverFlow上的转换器CainKellye( 如何将十六进制字符串转换为字节数组? )
string s =“”;
byte[] imageBytes = StringToByteArrayFastest(s);
MemoryStream ms = new MemoryStream(imageBytes);
Bitmap bmp = (Bitmap)Image.FromStream(ms);
pictureBox1.Image = bmp;
我不认为s
是基本的64字符串,它看起来更像十六进制 - 它甚至在它前面仍然有0x
并且'数字'不高于F.你应该在前面删除0x
。 要获得base 64字符串,您可以使用此网站的代码(我还没有测试过):
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.