How to create a Bitmap from a Color [] []?

4

I needed to transform a Bitmap into Color [] [] to apply some algorithms and I need to recreate the bitmap. Is there an easy way to do this?

To transform the bitmap into Color [] [] I did what is below. Is it an easier way?

    public Color[][] GetPixels(Bitmap b1)
    {
        int hight = b1.Height;
        int width = b1.Width;

        Color[][] colorMatrix = new Color[width][];
        for (int i = 0; i < width; i++)
        {
            colorMatrix[i] = new Color[hight];
            for (int j = 0; j < hight; j++)
            {
                colorMatrix[i][j] = b1.GetPixel(i, j);
            }
        }
        return colorMatrix;
    }
    
asked by anonymous 17.02.2016 / 13:29

1 answer

0

Unfortunately there is no constructor or method of Bitmap that even accepts a two-dimensional array (as noted in the comments). The solution given in Luiz Vieira's comments would be an appropriate method. Something like (not tested!) :

public Bitmap SetPixels(Color[][] colors)
{
    int hight = colors.GetLength(1);
    int width = colors.GetLength(0);

    Bitmap b1 = new Bitmap();
    for (int i = 0; i < width; i++)
    {
        colorMatrix[i] = new Color[hight];
        for (int j = 0; j < hight; j++)
        {
            b1.SetPixel(i,j,colors[i][j]);
        }
    }
    return b1;
}
    
13.02.2017 / 18:57