0

Here is my code, it is simple and should work, but doesn't and it's driving me crazy. I troubleshooted every part of it, everything is working as expected but when i open my file no matter what composes my bitmap it's just full of 0.

Here's the code:

public void saveToolStripButton_Click(object sender, EventArgs e)
    {
        int[,] map = new int[xs,ys];
        int yt, xt;
        yt = 0;
        Color pixcolor;
        while ( yt < ys)
        {
            xt = 0;
            while (xt < xs)
            {
                pixcolor = drawg.myBitmap.GetPixel(xt, yt);
                if (pixcolor == Color.Green)
                {
                    map[xt, yt] = 0;
                }
                if (pixcolor == Color.Black)
                {
                    map[xt, yt] = 1;
                }
                if (pixcolor == Color.White)
                {
                    map[xt, yt] = 2;
                }
                if (pixcolor == Color.Red)
                {
                    map[xt, yt] = 3;
                }
                if (pixcolor == Color.DarkGreen)
                {
                    map[xt, yt] = 4;
                }
                if (pixcolor == Color.Gray)
                {
                    map[xt, yt] = 5;
                }
                if (pixcolor == Color.IndianRed)
                {
                    map[xt, yt] = 6;
                }
                if (pixcolor == Color.Gold)
                {
                    map[xt, yt] = 7;
                }
                xt++;
            }
            yt++;
        }
        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
        for (int yl = 0; yl < ys; yl++)
        {
            for (int xl = 0; xl < xs; xl++)
            {
                        file.Write(map[xl,yl].ToString());
            }
            file.Write(Environment.NewLine);
        }
    file.Close();
    }

xs and ys are just the dimension of the bitmap.The format of the bitmap is Format24bppRgb. I have no idea why it wont work, probably something stupid i overlooked.

Thanks alot.

FFA702
  • 29
  • 3
  • 3
    If you debug your series of `if` statements, do any of the conditions actually evaluate to `true`? I have a feeling that none of your pixel colors match exactly to those eight values. – Andrew Sep 19 '14 at 15:06
  • 1
    Tip: Adding `else` will stop the testing after the first match is found. In this case there shouldn't be any colors that match more than one `if` test, but in other applications it can be a problem. It also lets you have a final `else` to catch any leftovers as suggested by Andrew Arnold. An alternative is a `switch` statement with a `default` clause. – HABO Sep 19 '14 at 15:10
  • I'm curious as to why you are using a `map` variable at all. Why not just replace all the `map` assignments in your `if` conditions with `file.Write` – Icemanind Sep 19 '14 at 15:34
  • Turns out none of my values return true. This is extremely weird because my bitmap is filled with Color.Green at the start of the application and the only color that can be applied on it are the one that are listed in my code above. I use the map variable because i may have to manipulate it afterward. – FFA702 Sep 19 '14 at 16:26
  • Try looking at the pixcolor variable in hex. The named color for Green is FF008000. Was the bitmap filled with green using the named color? – KC-NH Sep 19 '14 at 17:17
  • you may have to check on the pixelformat, it may or may not contain an alpha channel.. – TaW Sep 19 '14 at 17:17
  • When i use drawg.myBitmap.GetPixel(0,0).ToString() to fetch and display the color of the bitmap at 0,0 , it displays the right color in ARGB format. For example green is displayed [A=255, R=0, G=128, B=0] and black is simply displayed [A=255, R=0, G=0, B=0], but it still wont work and i can see no reason why. :'( – FFA702 Sep 19 '14 at 21:02
  • A switch statement would also be faster – reggaeguitar Sep 19 '14 at 21:13
  • A switch statement apparently can't be implanted because Color can't be a switch... – FFA702 Sep 20 '14 at 13:35

1 Answers1

1

"This method compares more than the ARGB values of the Color structures. It also does a comparison of some state flags. If you want to compare just the ARGB values of two Color structures, compare them using the ToArgb method."

http://social.msdn.microsoft.com/Forums/en-US/6c8a5907-edd3-43b9-b7e7-7d8aac6ad7fa/weird-issue-regarding-getpixel?forum=csharpgeneral&prof=required

FFA702
  • 29
  • 3