我发现了一个article说 Interior.Color
返回的值是 BGR,我试图将其转换为 RGB,但我仍然得到奇怪的颜色
我正在尝试检索 Excel 单元格中的基色,以使用 ControlPaint.Light
照亮它,如下所示:
int rgbColor = ExcelDoc.BGRToRGB((int)contentCanonical.Interior.Color);
Color canonicalColor = ColorTranslator.FromOle(rgbColor);
Color backgroundColor = ControlPaint.Light(canonicalColor, 75);
这是我的转换方法
public static int BGRToRGB(int bgr)
{
byte[] hexBGR;
byte[] hexRGB = new byte[4] {0,0,0,0};
hexBGR = BitConverter.GetBytes(bgr);
hexRGB[0] = hexBGR[0];
hexRGB[1] = hexBGR[3];
hexRGB[2] = hexBGR[2];
hexRGB[3] = hexBGR[1];
return BitConverter.ToInt32(hexRGB, 0);
}
我做错了什么?
最佳答案
这个method返回颜色
public static Color From0BGR(int bgrColor)
{
// Get the color bytes
var bytes = BitConverter.GetBytes(bgrColor);
// Return the color from the byte array
return Color.FromArgb(bytes[0], bytes[1], bytes[2]);
}
这会从 rgb 颜色返回 bgr color int
public static int Get0BGR(Color rgbColor)
{
// Return a zero-alpha 24-bit BGR color integer
return (0 << 24) + (rgbColor.B << 16) + (rgbColor.G << 8) + rgbColor.R;
}
关于c# - 将 Excel 颜色 BGR 转换为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24672889/