我想将字符串转换为位图或可以在pixelbox中显示的内容。
我的字符串如下所示:
string rxstring = "010010010020020020030030030040040040050050050060060060070070070080080080090090090100100100110110110120120120130130130140140140150150150160160160“
删除字符串中的RGB代码没有问题
("01002003004005060070080090100110120130140150160");
我只需要显示它,就不重要了[原文如此]
IDE:VS2010 C#
最佳答案
经过不断的审查,我意识到您得到的字符串不是字节数组。这将创建一个正方形位图,并允许您逐像素设置值。
List<string> splitBytes = new List<string>();
string byteString = "";
foreach (var chr in rsstring)
{
byteString += chr;
if (byteString.Length == 3)
{
splitBytes.Add(byteString);
byteString = "";
}
}
var pixelCount = splitBytes.Count / 3;
var numRows = pixelCount / 4;
var numCols = pixelCount / 4;
System.Drawing.Bitmap map = new System.Drawing.Bitmap(numRows, numCols);
var curPixel = 0;
for (int y = 0; y < numCols; y++)
{
for (int x = 0; x < numRows; x++ )
{
map.SetPixel(x, y, System.Drawing.Color.FromArgb(
Convert.ToInt32(splitBytes[curPixel * 3]),
Convert.ToInt32(splitBytes[curPixel * 3 + 1]),
Convert.ToInt32(splitBytes[curPixel * 3 + 2])));
curPixel++;
}
}
//Do something with image
编辑:对行/列迭代进行更正以匹配上面显示的图像。
关于c# - 在C#中将字符串转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244595/