1

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

My string looks like this:

string rxstring = "010010010020020020030030030040040040050050050060060060070070070080080080090090090100100100110110110120120120130130130140140140150150150160160160“

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#

Blue Magister
  • 13,044
  • 5
  • 38
  • 56
LFS96
  • 856
  • 1
  • 12
  • 23

3 Answers3

8

I'm afraid the data you are getting is not a meaningful image. If you split the data into groups of three. You get the following:

010
010
010
020
020
020
030
030
030
040
040
040
050
050
050
060
060
060
070
070
070
080
080
080
090
090
090
100
100
100
110
110
110
120
120
120
130
130
130
140
140
140
150
150
150
160
160
160

If you look at that data there's no way you can convert this to an image that would actually mean something to us. It would be a collection of 48 pixels. Containing a sort of gradient like image (since the numbers below follow a pattern that is constantly increasing.

We would need more information to debug this. (Like what component is providing the data etc.)

Update This is what I get when I convert your data to pixels (take in account i've enlarged every pixel to 16x16)

Result when assembling the data

Community
  • 1
  • 1
woutervs
  • 1,500
  • 12
  • 28
  • Also converting the above string to text gives you: (((222<< – woutervs Nov 27 '13 at 14:42
  • it is a piture with 4x4 Pixel witch want to show in gray on a pixelbox i know you cann't see much but in some time there will be more pixel then i get better termal camara – LFS96 Nov 27 '13 at 14:52
  • I've updated my answer with an upscaled image of the result of your data. This should be 16 times smaller, thus invisible for the naked eye to see. Are you sure your arduino is sending the right data? – woutervs Nov 27 '13 at 15:18
1

Try converting the string to a byte array and loading it into a memory stream. Once in the stream, you should be able to convert to an image.

List<byte> splitBytes = new List<byte>();            
string byteString = "";

foreach (var chr in testString)
{
    byteString += chr;

    if (byteString.Length == 3)
    {
       splitBytes.Add(Convert.ToByte(byteString));
       byteString = "";
    }
}

if (byteString != "")
    splitBytes.AddRange(Encoding.ASCII.GetBytes(byteString));

using (var ms = new MemoryStream(splitBytes.ToArray()))
{
    var img = System.Drawing.Image.FromStream(ms);

    //do something with image.
}

EDIT: Added updated code. This was tested by loading an image of my own and converting the bytes into a string, then converting them back into a byte array using the above code and I successfully loaded the image from a string.

string testString = "255216255224000016074070073070000001001001000096000096000000255225000104069120105102000000077077000042000000000008000004001026000005000000000001000000000062001027000005000000000001000000000070001040000003000000000001000002000000001049000002000000000018000000000078000000000000000000000096000000000001000000000096000000000001080097105110116046078069084032118051046053046049049000255219000067000002001001002001001002002002002002002002002003005003003003003003006004004003005007006007007007006007007008009011009008008010008007007010013010010011012012012012007009014015013012014011012012012255219000067001002002002003003003006003003006012008007008012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012012255192000017008000004000004003001034000002017001003017001255196000031000000001005001001001001001001000000000000000000000000001002003004005006007008009010011255196000181016000002001003003002004003005005004004000000001125001002003000004017005018033049065006019081097007034113020050129145161008035066177193021082209240036051098114130009010022023024025026037038039040041042052053054055056057058067068069070071072073074083084085086087088089090099100101102103104105106115116117118119120121122131132133134135136137138146147148149150151152153154162163164165166167168169170178179180181182183184185186194195196197198199200201202210211212213214215216217218225226227228229230231232233234241242243244245246247248249250255196000031001000003001001001001001001001001001000000000000000000001002003004005006007008009010011255196000181017000002001002004004003004007005004004000001002119000001002003017004005033049006018065081007097113019034050129008020066145161177193009035051082240021098114209010022036052225037241023024025026038039040041042053054055056057058067068069070071072073074083084085086087088089090099100101102103104105106115116117118119120121122130131132133134135136137138146147148149150151152153154162163164165166167168169170178179180181182183184185186194195196197198199200201202210211212213214215216217218226227228229230231232233234242243244245246247248249250255218000012003001000002017003017000063000252225248089251085248195193031007060033030133127054137107121166121143107103121116176043069052182202085076167111238224143056234193152252204073040162128063255217";

EDIT: Added a sample string of the image I used to test the above code.

MX D
  • 2,453
  • 4
  • 35
  • 47
Loren Shaw
  • 445
  • 4
  • 16
  • This would work, but still be a meaningless image with his data. – woutervs Nov 27 '13 at 14:37
  • 1
    From what I can tell, converting an images byte array into a string is what is corrupting the data. Theoretically, the above code should work, but upon testing, the string itself when converted into a byte array isn't a valid byte array that the Image class can load. – Loren Shaw Nov 27 '13 at 15:07
  • If your string is a valid representation of a byte array, it should load. – Loren Shaw Nov 27 '13 at 15:41
1

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

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

Loren Shaw
  • 445
  • 4
  • 16