locked
How can i use int[,] instead bool[,] ? RRS feed

  • Question

  • private unsafe void CreateArray(Bitmap bmp)
            {
                bool[,] bwValues = new bool[bmp.Height, bmp.Width];
    
                int[,] bwVal = new int[bmp.Height, bmp.Width];
    
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                    bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                    bmp.PixelFormat);
    
                byte* ptr = (byte*)bmpData.Scan0;
    
                int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
                byte[] rgbValues = new byte[bytes];
    
                for (int y = 0; y < bmp.Height; y++)
                {
                    var row = ptr + (y * bmpData.Stride);
    
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        var pixel = row + x * 4;
    
                        bool isWhite = (pixel[0] == 255 &&
                                        pixel[1] == 255 &&
                                        pixel[2] == 255 &&
                                        pixel[3] == 255);
    
                        bwValues[y, x] = isWhite;
                    }
                }
            }
    Now bwValues each value is false or true instead i want to use the bwVal and to add to each bwValues value 0 or 1 where 0 is black and 1 is white instead false true.
    Saturday, September 30, 2017 12:10 PM

Answers

  • bwVal[x,y] = isWhite ? 1 : 0;

    hope that's what you're looking for.

    • Marked as answer by Chocolade1972 Saturday, September 30, 2017 8:21 PM
    Saturday, September 30, 2017 12:20 PM

All replies

  • bwVal[x,y] = isWhite ? 1 : 0;

    hope that's what you're looking for.

    • Marked as answer by Chocolade1972 Saturday, September 30, 2017 8:21 PM
    Saturday, September 30, 2017 12:20 PM
  • bwVal[x,y] = isWhite ? 1 : 0;

    hope that's what you're looking for.

    I tried:

    bwVal[y, x] = isWhite ? 1 : 0;

    And

    bwVal = isWhite ? 1 : 0;

    In both cases i'm getting the same error:

    Cannot implicitly convert type 'int' to 'bool'

    Saturday, September 30, 2017 12:38 PM
  • That's strange, Chocolade ... I thought that it should work without a problem, so I just tested it to be sure. It *did* work. That would be the statement like this: bwVal[y, x] = isWhite ? 1 : 0;

    I can't imagine that the version of .NET you're using would make a difference, but maybe it does ... I'm using VS 2015 and targeting Framework 4.5. What are you using? 


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    • Edited by BonnieBMVP Saturday, September 30, 2017 4:46 PM clarification
    Saturday, September 30, 2017 4:43 PM
  • Sorry it was my type mistake. It's working fine.
    Saturday, September 30, 2017 8:22 PM