Converting an array of Pixels to an image in C#

前端 未结 6 1533
孤城傲影
孤城傲影 2020-12-28 11:20

I have an array of int pixels in my C# program and I want to convert it into an image. The problem is I am converting Java source code for a program into equiva

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 11:48

    I would recommend using LockBits but a slower SetPixel based algorithm might look something like

    
    // width - how many int's per row        
    // array - array of integers
    Bitmap createImage(int width, int[] array)
    {            
      int height = array.Length / width;
      Bitmap bmp = new Bitmap(width, height);
      for (int y = 0; y < height; y++)
      {
        for (int x = 0; x < array.Length; x += width)
        {
          bmp.SetPixel(x, y, Color.FromArgb(array[i]));
        }
      }
      return bmp;
    }
    

提交回复
热议问题