VS2010 中C++ 和C# 颜色转化

在.net 中,用C++开发时候用到了COLORREF 而在C# 中没有这个只要Color,所以需要进行转化

 

COLORREF类型颜色的值COLORREF cr=RGB(123,200,12); 其中的R、G、B三个分量的排列顺序是BGR。

.NET中通过数据类型Color表示颜色,该类有一个函数FromArgb(int,int,int),可以通过输入RGB三个值得到一个Color类型的颜色。同时也有一个ToArgb()函数,得到一个32位的整数值.

 

32 位 ARGB 值的字节顺序为 AARRGGBB。由 AA 表示的最高有效字节 (MSB) 是 alpha 分量值。分别由 RR、GG 和 BB 表示的第二、第三和第四个字节分别为红色、绿色和蓝色颜色分量

 

1、从Color到COLORREF

                   int nColor = crColor.ToArgb();
                   int blue = nColor & 255;
                   int green = nColor >> 8 & 255;
                   int red = nColor >> 16 & 255;

//注意COLORREF中颜色的排列是 BGR,而通过Color.ToArgb()得到的数值中颜色排列是AARRGGBB

int nColorRef = blue << 16 | green << 8 | red;

还要在进行无符号数的强制类型转化。

2、从COLORREF到Color(注意COLORREF中颜色的排列是BGR,红色分量在最后面)

int red=nColorRef & 255;

int green= nColorRef >> 8 & 255;

int blue= nColor Ref>> 16 & 255;

Color crColor=Color.FromArgb(red,green,blue);

或者直接通过下面的代码:

Color.FromArgb(nColorRef & 255, nColorRef >> 8 & 255, nColor Ref>> 16 & 255);

 

详细可以看 http://www.menjn.com/wzkf/asp/2012/0531/2352.html

 

posted @ 2012-07-18 09:57  sinian  阅读(948)  评论(0编辑  收藏  举报