将互操作颜色转换为System.Drawing.Color [英] Convert Interop Color to System.Drawing.Color

查看:77
本文介绍了将互操作颜色转换为System.Drawing.Color的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在C#中将Microsoft.Office.Interop.Word/Excel/PowerPoint.Color转换为System.Drawing.Color.

I am looking how to convert Microsoft.Office.Interop.Word/Excel/PowerPoint.Color to System.Drawing.Color in C#.

我在此论坛中发现了相反的情况此处此处但我找不到如何将Interop 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的颜色以RGB表示:

I have understood that Interop color is expressed in RGB considering:

RGBvalue =红色+ 256 *绿色+ 256 * 256 *蓝色

RGBvalue = Red + 256*Green + 256*256*Blue

但是从RGB值中查找Red Green Blue的值并不容易(例如,如果值为5652,我不知道如何找到Red为20,Green为22).

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).

您知道将Microsoft.Office.Interop.Word/Excel/PowerPoint.Color转换为System.Drawing.Color(或转换为OLE,然后知道如何转换)的功能吗?

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) ?

推荐答案

如果您以十六进制格式查看数据,则这一切都更加简单. 5652变为0x1614,显然是字节0x16和0x14.或者,0x16 * 0x100 + 0x14.

This is all much simpler if you look at the data in hexadecimal format. 5652 becomes 0x1614, which is clearly bytes 0x16 and 0x14. Or, 0x16 * 0x100 + 0x14.

与0x100相乘的相反操作应该只是将其除以0x100,但实际上要切掉多余的部分,我建议使用AND位运算.

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.

AND将仅保留两个值中共有的位,因此,以二进制表示,仅保留最低的8位,您需要将其与启用了所有8位的值进行AND运算,即0xFF.以二进制格式查看,0x1614(以您的"5652"示例为例)与0xFF之间的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. The AND between 0x1614 (your "5652" example) and 0xFF, viewed in binary format, would go like this:

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

如您所见,它有效地切断了高于最低字节的所有内容,结果为00010100b或0x14或20.

As you see, it effectively cuts off everything higher than the lowest byte, resulting in 00010100b, or 0x14, or 20.

要除以2的倍数,还有另一种非常方便且非常有效的位运算:位移位. 0x100实际上是"1",上移了8位.因此,要取回原始值,只需将其下移8位即可.

For dividing by multiples of 2, there is another bit operation that is very handy and really efficient: bit shifting. 0x100 is actually '1', shifted up by 8 bits. So to get the original value back, it just needs to be shifted down by 8 bits.

如果您的颜色是B * 0x10000 + G * 0x100 + R,则将其反转:

if your colour is B * 0x10000 + G * 0x100 + R, then to reverse it:

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);
}

这篇关于将互操作颜色转换为System.Drawing.Color的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆