将 RGB 值转换为 System.Drawing
我环顾四周,但没有看到这个问题 - 也许它太简单了,但我没有。
如果我有包含颜色值的文件,例如:
255, 65535, 65280(我认为这是绿色的)
有没有办法:
- 将它们转换为 System.Drawing.Color 类型或者....更好..
- 让 .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:
- convert these to a System.Drawing.Color type or....better yet..
- have .NET accept them as is to represent the color?
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用颜色类型的方式:
Imports System.Drawing
This is how you use the color type:
Imports System.Drawing
从您对此答案的早期版本的评论来看,文件中的每个数字似乎代表一种颜色,并且这些是打包字节:
所以您想要三元组的第一个(红色?)部分中的低字节,以及下一个三元组的第二个(蓝色?)部分中的高字节。我猜测超过 65535 的值将进入三元组的第三个字节。
您可以使用位掩码和位移位运算符轻松完成此操作:
即每次右移 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:
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:
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.