开发者

converting rgb values to System.Drawing

I've looked around, but don't see this question - maybe its too easy, but I don't have 开发者_如何学运维it.

If I have file containing color values like:

255, 65535, 65280 ( I think this is green)

is there a way to either:

  1. convert these to a System.Drawing.Color type or....better yet..
  2. have .NET accept them as is to represent the color?

thanks,


This is how you use the color type:

dim someColor as Color = color.FromArgb(red, green, blue)
  • Be sure to use Imports System.Drawing
  • Change red, green and blue out for their respective rgb values (ie: 0-255)


From your comments on an earlier version of this answer it seems that each number in the file represents one colour, and that these are packed bytes:

65280 (= 0xFF00) -> (0, 255, 0)
65535 (= 0xFFFF) -> (255, 255, 0)

So you want the low byte in the first (red?) part of the triple, and the next higher byte in the second (blue?) part of the triple. I am guessing that values over 65535 would go into the third byte of the triple.

You can do this easily using bit masking and bit shift operators:

int r = n && 0xFF;
int g = (n >> 8) & 0xFF;
int b = (n >> 16) & 0xFF;

i.e. shift it right by 8 bits each time, and select out the bottom 8 bits.

NOTE: You can also do this directly using Color.FromArgb(Int32), which saves you from having to do the unpacking. But this will only work if the numbers in your file are packed in the right way. They would need to be in AARRGGBB format. For example, 255 (0x000000FF) = Blue, 65280 (0x0000FF00) = Green, 16711680 (0x00FF0000) = Red. I am not sure whether your numbers are in the right order which is why I have covered the explicit unpacking technique.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜