How to assign System.Drawing.Color to Microsoft.Of

2019-09-06 11:09发布

Sample code as follow,

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor = System.Drawing.Color.Red;

Return an error. Cannot implicitly convert type, one is a Struct, the other is ColorFormat.

ForeColor.RGB is Int type, I can only get 3 ints from Color.Red.R, Color.Red.G and Color.Red.B. How to assign the color I want to ForeColor property?

2条回答
狗以群分
2楼-- · 2019-09-06 11:28

Try RGB property ForeColor.RGB

E.g.

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = System.Drawing.Color.Red.ToArgb();
查看更多
Juvenile、少年°
3楼-- · 2019-09-06 11:29

As suggested in the comments, use ColorTranslator.ToOle

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.Red);

Or, entering the RGB values:

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255, 0, 0);

Some additional information, so I'm not just repeating the comments:

ForeColor.RGB can be represented in Hex as 0xBBGGRR (source). Knowing that, you could create the following function:

public int Color_to_RGB(Color color) {
    return (color.B << 16) | (color.G << 8) | Color.R;
}

Use:

Chart.SerialCollection(1).Point(1).Format.Fill.ForeColor.RGB = Color_to_RGB(Color.Red);

The reason ToArgb(), used in the other answer, does not work because its Hex representation is 0xAARRGGBB

查看更多
登录 后发表回答