How can I read image pixels' values as RGB into 2d array?

108,842

Solution 1

Well, if I understood correctly, you want to iterate through the pixels in the image, perform some kind of test, and if it passes you want to store that pixel in an array. Here´s how you could do that:

using System.Drawing;

Bitmap img = new Bitmap("*imagePath*");
for (int i = 0; i < img.Width; i++)
{
    for (int j = 0; j < img.Height; j++)
    {
        Color pixel = img.GetPixel(i,j);

        if (pixel == *somecondition*)
        {
            **Store pixel here in a array or list or whatever** 
        }
    }
} 

Don´t think you need anything else. If you need the specific RGB values you can get them from the corresponding methods in the pixel object.

Solution 2

 public Color[][] getBitmapColorMatrix(string filePath)
    {
        Bitmap bmp = new Bitmap(filePath);
        Color[][] matrix;
        int height = bmp.Height;
        int width = bmp.Width;
        if (height > width)
        {
            matrix = new Color[bmp.Width][];
            for (int i = 0; i <= bmp.Width - 1; i++)
            {
                matrix[i] = new Color[bmp.Height];
                for (int j = 0; j < bmp.Height - 1; j++)
                {
                    matrix[i][j] = bmp.GetPixel(i, j);
                }
            }
        }
        else
        {
            matrix = new Color[bmp.Height][];
            for (int i = 0; i <= bmp.Height - 1; i++)
            {
                matrix[i] = new Color[bmp.Width];
                for (int j = 0; j < bmp.Width - 1; j++)
                {
                    matrix[i][j] = bmp.GetPixel(i, j);
                }
            }
        }
        return matrix;
    }
Share:
108,842
user1306322
Author by

user1306322

I've come to learn things and teach stuff. And I'm all outta stuff. And don't take anything I say personally, because… I don't give it to you personally! Feel free to improve my posts by editing or commenting. helpful links: http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/getting_started http://jsbeautifier.org/ (also acceptable for C#) http://regex101.com/ I strongly recommend you include in your question all additional details that might help us answer your question properly and completely.

Updated on August 06, 2020

Comments

  • user1306322
    user1306322 almost 4 years

    I was making a 2d map editor for my square tile platformer game, when I realized I could really use an image editor with its abilities to repaint adjacent pixels and many more, so I figured I should try and read a painted level by an app that will then convert it into a lightweigh format.

    I'm not sure if using a bitmap format is mandatory for such thing, but I guess, reading a specific pixel would be easier than with PNG for example.

    So my goal is to open an image, iterate through every pixel, look for those which colors fit my tile scheme and put corresponding tile into the array of blocks.

    Note: I already have my lightweigh format, so I need only reading pixels values into array.


    **Solution:** My sketch looks like this:
    var myBitmap = new Bitmap(@"input.png");  
    
    for (int x = 0; x < myBitmap.Width; x++)
        for (int y = 0; y < myBitmap.Height; y++)
        {                    
            Color pixelColor = myBitmap.GetPixel(x, y);
            // things we do with pixelColor
        }
    

    Example 2:
    var myBitmap = new Bitmap(@"input.png");
    
    for (int x = 0; x < myBitmap.Width; x++)
    {
        for (int y = 0; y < myBitmap.Height; y++)
        {
            // Get the color of a pixel within myBitmap.
            Color pixelColor = myBitmap.GetPixel(x, y);
            string pixelColorStringValue =
                pixelColor.R.ToString("D3") + " " +
                pixelColor.G.ToString("D3") + " " +
                pixelColor.B.ToString("D3") + ", ";
    
            switch (pixelColorStringValue)
            {
                case "255 255 255":
                    {
                        // white pixel
                        break;
                    }
                case "000 000 000":
                    {
                        // black pixel
                        break;
                    }
            }
        }
    }