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

2 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 3,710 Reputation points Microsoft External Staff Moderator
    2026-05-11T03:33:21.2733333+00:00

    Hello @mc ,

    Thanks for your question.

    Since your array is laid out row by row, you just iterate through the array and map each index to an (x, y) pixel position.

    You can refer to following example:

    From Color[]:

    Color[] colors = new Color[48 * 12];
    
    var bmp = new Bitmap(48, 12);
    for (int i = 0; i < colors.Length; i++)
        bmp.SetPixel(i % 48, i / 48, colors[i]);
    
    pictureBox1.Image = bmp;
    

    From int[] (ARGB packed):

    int[] colors = new int[48 * 12];
    
    var bmp = new Bitmap(48, 12);
    for (int i = 0; i < colors.Length; i++)
        bmp.SetPixel(i % 48, i / 48, Color.FromArgb(colors[i]));
    
    pictureBox1.Image = bmp;
    

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.

    Was this answer helpful?

    0 comments No comments

  2. 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.