在C#中将Int转换为Color用于Silverlight的WriteableBitmap

10 c# silverlight

在Silverlight 3中,现在有一个WriteableBitmap,它提供了get/put像素功能.这可以这样做:

// setting a pixel example
WriteableBitmap bitmap = new WriteableBitmap(400, 200);
Color c = Colors.Purple;
bitmap.Pixels[0] = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
Run Code Online (Sandbox Code Playgroud)

基本上,设置Pixel涉及设置其颜色,并通过将alpha,red,blue,green值按位移位为整数来实现.

我的问题是,你如何将整数转回颜色?在这个例子中缺少的是什么:

// getting a pixel example
int colorAsInt = bitmap.Pixels[0];
Color c;
// TODO:: fill in the color c from the integer ??
Run Code Online (Sandbox Code Playgroud)

感谢您提供的任何帮助,我只是不知道我的位移,我相信其他人会在某些时候遇到这个障碍.

Sma*_*tyP 7

使用反射器我发现如何在标准.net调用中解析R,G,B(在Silverlight中不可用):

System.Drawing.ColorTranslator.FromWin32()
Run Code Online (Sandbox Code Playgroud)

从那我开始猜测如何获得alpha通道,这就完成了工作:

Color c2 = Color.FromArgb((byte)((colorAsInt >> 0x18) & 0xff), 
                          (byte)((colorAsInt >> 0x10) & 0xff), 
                          (byte)((colorAsInt >> 8) & 0xff), 
                          (byte)(colorAsInt & 0xff));
Run Code Online (Sandbox Code Playgroud)


Nel*_*son -1

Color.FromArgb(intVal)
Run Code Online (Sandbox Code Playgroud)

  • 澄清一下,Silverlight中没有FromArgb(int)方法..只有FromArgb(byte, byte, byte, byte) - 这是我问题的一部分 (3认同)