简体   繁体   中英

How to assign System.Drawing.Color to Microsoft.Office.Interop.Excel.ColorFormat

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?

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

Try RGB property ForeColor.RGB

Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM