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?
A:
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;
}
}
Guffa
2009-07-18 23:40:09
System.Windows.Media.Color doesn't have a ToArgb method. An in sARGB color space all the values are a single.
Wilhelm
2009-07-18 23:45:23
I didn't know which color structure you used (the System.Drawing.Color structure for example has a ToArgb method). Se my edit above for how to get the color components.
Guffa
2009-07-19 17:33:43
I used the one in WPF System.Windows.Media.Color that handles sARGB color space, not just ARGB.
Wilhelm
2009-07-23 19:41:33
The System.Windows.Media.Color value can be created from any color space, and you read the sRGB color values using the A, R, G and B properties. (There is no sARGB color space.)
Guffa
2009-07-23 20:29:12