Share via

how to create Bitmap from Array?

mc 6,921 Reputation points
2026-05-08T13:45:07.8733333+00:00

I have array of int or Color how to create bitmap?

if the bitmap width = 48 and height=12 then the array is Color

line 1 48 colors

line 2 48 colors

.....

how to create it to Bitmap?

Developer technologies | Windows Forms
0 comments No comments

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-05-08T13:45:17.0833333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    In Windows Forms (.NET), create a Bitmap and set its pixels from the array.

    For an int[] of ARGB values (length = width * height):

    int width = 48;
    int height = 12;
    int[] colors = new int[width * height];
    // fill colors[y * width + x] with ARGB values
    
    var bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            int argb = colors[y * width + x];
            bmp.SetPixel(x, y, Color.FromArgb(argb));
        }
    }
    

    For a Color[] (length = width * height):

    int width = 48;
    int height = 12;
    Color[] colors = new Color[width * height];
    // fill colors[y * width + x]
    
    var bmp = new Bitmap(width, height);
    
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            bmp.SetPixel(x, y, colors[y * width + x]);
        }
    }
    

    If performance is critical, use LockBits and copy the raw bytes instead of SetPixel, but the above is the simplest approach for a 48×12 image.


    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.