Wilhelm
Wilhelm

Reputation: 1886

Writing a Color sARGB array to a WriteableBitmap

I have an two-dimensioanl array of sARGB colors (System.Windows.Media.Color) describing what I want to write in a WritableBitmap. The array is the same width and height than the expected bitmap. The problem is that, as long as I can see, the WritePixels Method of the WritableBitmap expects an array of integers. How do I convert my colors to said array?

Upvotes: 1

Views: 1137

Answers (1)

Guffa
Guffa

Reputation: 700680

What is the data type of the elements in the array? If they are Color values, the Color structure has a ToArgb method that returns the color as an integer.

The WritePixels method accepts a one-dimentional array of most any simple type, like byte, short, int, long. For an ARGB format each pixel needs four bytes, or one int.

Edit:
As you have System.Window.Media.Color values, you can use the A, R, G and B properties to get byte values for the components of the color:

byte[] pixelData = new byte[colorArray.Length * 4];
int ofs = 0;
for (int y = 0; y < colorArray.GetLength(1); y++) {
   for (int x = 0; x < colorArray.GetLenth(0); x++) {
      Color c = colorArray[x, y];
      pixelData[ofs++] = c.A;
      pixelData[ofs++] = c.R;
      pixelData[ofs++] = c.G;
      pixelData[ofs++] = c.B;
   }
}

Upvotes: 1

Related Questions