问题
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?
回答1:
Try RGB property ForeColor.RGB
E.g.
Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = System.Drawing.Color.Red.ToArgb();
回答2:
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
来源:https://stackoverflow.com/questions/27518048/how-to-assign-system-drawing-color-to-microsoft-office-interop-excel-colorformat