c# - 在C#中将字符串转换为位图

标签 c# .net string bitmap

我想将字符串转换为位图或可以在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/

相关文章:

c# - 是 type.isSubclassOf(Type otherType) 缓存还是我必须自己做?

c# - .net User Secrets 在 Windows 10 上不起作用

c# - 防止代码清理在 Resharper 中创建文件头

c# - 为什么我的 Resharper NUnit session 停止工作,但在 Debug模式下工作

c# - 将 double 转换为整数

c# - 如果它 = 0 c#,则跳过 for 循环中的值

C# 导入 .CSV 数据到 MySQL 表只复制 1 列

c - 递增指向字符串数组的指针返回 NULL

c++ - 在运行时向 vector 插入元素 C++。抛出运行时错误

java - 检查两个字符串是否与字母、数字和特殊字符匹配