简体   繁体   English

将互操作颜色转换为System.Drawing.Color

[英]Convert Interop Color to System.Drawing.Color

I am looking how to convert Microsoft.Office.Interop.Word/Excel/PowerPoint.Color to System.Drawing.Color in C#. 我正在寻找如何在C#中将Microsoft.Office.Interop.Word / Excel / PowerPoint.Color转换为System.Drawing.Color。

I have found the contrary in this forum here or here but I don't find how to convert from Interop Color to System.Drawing.Color. 我在这里这里的论坛中发现了相反的情况,但是我没有找到如何从Interop Color转换为System.Drawing.Color。

I have understood that Interop color is expressed in RGB considering: 我了解Interop颜色以RGB表示,考虑到:

RGBvalue = Red + 256*Green + 256*256*Blue RGBvalue =红色+ 256 *绿色+ 256 * 256 *蓝色

but it is not very easy from a RGBvalue to find the value of Red Green Blue (for example if the value is 5652 I don't know how to find Red is 20 and Green is 22). 但是从RGB值中查找Red Green Blue的值并不容易(例如,如果值为5652,我不知道如何找到Red为20,Green为22)。

Do you know a function to convert Microsoft.Office.Interop.Word/Excel/PowerPoint.Color to System.Drawing.Color (or to OLE and then, I know how to convert) ? 您是否知道将Microsoft.Office.Interop.Word / Excel / PowerPoint.Color转换为System.Drawing.Color(或转换为OLE,然后知道如何转换)的功能?

This is all much simpler if you look at the data in hexadecimal format. 如果您以十六进制格式查看数据,那么这一切将更加简单。 5652 becomes 0x1614, which is clearly bytes 0x16 and 0x14. 5652变为0x1614,显然是字节0x16和0x14。 Or, 0x16 * 0x100 + 0x14 . 或者, 0x16 * 0x100 + 0x14

The opposite of multiplying with 0x100 should simply be dividing it by 0x100, but to actually cut off the excess, I advise using the AND bit operation. 与0x100相乘的相反方法应该只是将其除以0x100,但实际上要切断多余的部分,我建议使用AND位运算。

AND will keep only the bits that are common in both values, so, in binary representation, to keep only the lowest 8 bits, you need to AND it with a value with all 8 bits enabled, which would be 0xFF. AND将仅保留两个值中共有的位,因此,以二进制表示,仅保留最低的8位,您需要将其与启用了所有8位的值进行AND运算,即0xFF。 The AND between 0x1614 (your "5652" example) and 0xFF, viewed in binary format, would go like this: 以二进制格式查看,0x1614(以您的“ 5652”示例为例)与0xFF之间的AND如下所示:

00010110 00010100
00000000 11111111
v------AND------v
00000000 00010100

As you see, it effectively cuts off everything higher than the lowest byte, resulting in 00010100b, or 0x14, or 20. 如您所见,它有效地切断了高于最低字节的所有内容,结果为00010100b或0x14或20。


For dividing by multiples of 2, there is another bit operation that is very handy and really efficient: bit shifting. 对于2的倍数除法,还有另一种非常方便且非常有效的位运算:位移位。 0x100 is actually '1', shifted up by 8 bits. 0x100实际上是“ 1”,上移了8位。 So to get the original value back, it just needs to be shifted down by 8 bits. 因此,要取回原始值,只需将其下移 8位。


if your colour is B * 0x10000 + G * 0x100 + R , then to reverse it: 如果您的颜色是B * 0x10000 + G * 0x100 + R ,则将其反转:

public static Color GetColorFromInterop(Int32 val)
{
    // Limit to a value containing only the bits in 0xFF
    Int32 r = val & 0xFF;
    // Shift down by 8 bits (meaning, divide by 0x100), then limit to 0xFF
    Int32 g = (val >> 8) & 0xFF;
    // Shift down by 16 bits (meaning, divide by 0x10000), then limit to 0xFF
    Int32 b = (val >> 16) & 0xFF;
    return Color.FromArgb(r, g, b);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM