将一个字符串转换为位图在C#位图、字符串、转换为

2023-09-03 06:54:46 作者:旧人

我想一个字符串转换成位图或东西我可以在pixelbox显示。

I want to convert a string into a bitmap or something I can show in a pixelbox.

我的字符串是这样的:

string rxstring = "010010010020020020030030030040040040050050050060060060070070070080080080090090090100100100110110110120120120130130130140140140150150150160160160"

这是没有问题的擦除RGB code字符串中的

It is no problem to erase the RGB code in the string

("01002003004005060070080090100110120130140150160");

我只需要它显示的并不重要[原文]

I only need it to show, the is not important [sic]

IDE:VS2010 C#

IDE: VS2010 C#

推荐答案

经连续审查,我意识到,你得到的字符串不是一个字节数组。这将创建一个正方形位图,并允许您设置的值逐像素。

Upon continuing review, I realized that the string your getting isn't a byte array. This creates a square Bitmap and lets you set the values pixel by pixel.

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

编辑:制造更正行/列迭代上面显示的图像相匹配

Made corrections to the row/col iterations to match the image shown above.