c# - 将 Excel 颜色 BGR 转换为 RGB

标签 c# excel

我发现了一个articleInterior.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/

相关文章:

c# - 属性和方法的区别

c# - 运行时代码编译出错 - 进程无法访问文件

c# - 获取光标相对于控件的位置 - C#

HTML 到 Excel : How can tell Excel to treat columns as numbers?

java - JXL(Java)更改为1904日期系统

vba - 使用VBA连接远程机器

python - 使用 pandas 读取和更新 XLSM 文件中的工作表,同时保留 VBA 代码

c# - 在 C# 中实现组合和聚合?

C# 将包含 c 样式字符串的 byte[] 转换为字符串 - NOT Encoding.GetString(byte[])

excel - 机器人框架和 Excel : Constructing full path to Excel-file of different parts of the path (Open Pyxl Library)