将 RGB 值转换为 System.Drawing

发布于 2024-08-17 18:33:10 字数 219 浏览 6 评论 0原文

我环顾四周,但没有看到这个问题 - 也许它太简单了,但我没有。

如果我有包含颜色值的文件,例如:

255, 65535, 65280(我认为这是绿色的)

有没有办法:

  1. 将它们转换为 System.Drawing.Color 类型或者....更好..
  2. 让 .NET 按原样接受它们来表示颜色?

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-08-24 18:33:10

这是使用颜色类型的方式:

dim someColor as Color = color.FromArgb(red, green, blue)
  • 请务必使用 Imports System.Drawing
  • 更改红色、绿色和蓝色各自的 RGB 值(即:0-255)

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)
—━☆沉默づ 2024-08-24 18:33:10

从您对此答案的早期版本的评论来看,文件中的每个数字似乎代表一种颜色,并且这些是打包字节:

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

所以您想要三元组的第一个(红色?)部分中的低字节,以及下一个三元组的第二个(蓝色?)部分中的高字节。我猜测超过 65535 的值将进入三元组的第三个字节。

您可以使用位掩码和位移位运算符轻松完成此操作:

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

即每次右移 8 位,然后选择底部 8 位。

注意:您还可以直接使用 Color.FromArgb(Int32) 执行此操作,这样您就不必进行解包。但这只有在文件中的数字以正确的方式打包时才有效。它们需要采用 AARRGBB 格式。例如,255 (0x000000FF) = 蓝色、65280 (0x0000FF00) = 绿色、16711680 (0x00FF0000) = 红色。我不确定您的数字顺序是否正确,这就是为什么我介绍了显式解包技术。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文