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

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.


 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;
    }

Tags:

C#

Image

Rgb

Bitmap