split ARGB into byte values

送分小仙女□ 提交于 2019-12-21 09:34:48

问题


I have a ARGB value stored as an int type. It was stored by calling ToArgb.

I now want the byte values of the individual color channels from the int value.

for example

int mycolor = -16744448;
byte r,g,b,a;

GetBytesFromColor(mycolor,out a, out r, out g, out b);

How would you implement GetBytesFromColor?

To give the context I am passing a color value persisted in db as int to a silverlight application which needs the individual byte values to construct a color object.

System.Windows.Media.Color.FromArgb(byte a, byte r, byte g, byte b)

回答1:


You are after the 4 successive 8-bit chunks from a 32-bit integer; so a combination of masking and shifting:

b = (byte)(myColor & 0xFF);
g = (byte)((myColor >> 8) & 0xFF);
r = (byte)((myColor >> 16) & 0xFF);
a = (byte)((myColor >> 24) & 0xFF);



回答2:


public void GetBytesFromColor(int color, out a, out r, out g, out b)
{
    Color c = Color.FromArgb(color);
    a = c.A;
    r = c.R;
    g = c.G;
    b = c.B;
}


来源:https://stackoverflow.com/questions/1328220/split-argb-into-byte-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!