Free Support Forum - aspose.com

System.Drawing.ColorTranslator question

Colors are sometimes shown in HTML as their hexadecimal code, e.g. color="#990099". How can I translate a color code such as "#990099" and use it as the color for one of the series of a line graph?

Do I need to use ColorTranslator.FromHtml ? When should I use Color.FromName ?

Can ColorTranslator.FromHtml and Color.FromName be used for color names like "Red" as well as hexadecimal color codes?

A few lines of sample code would be of great help.

In your case, you need to call ColorTranslator.FromHtml. If you use a pre-define color, you can use Color.FromName. Please check MSDN for reference.

Following is a piece of sample code:

Excel excel = new Excel();
Cells cells = excel.Worksheets[0].Cells;
cells["a1"].PutValue(2);
cells["a2"].PutValue(5);
cells["a3"].PutValue(3);
cells["a4"].PutValue(6);
cells["b1"].PutValue(4);
cells["b2"].PutValue(3);
cells["b3"].PutValue(6);
cells["b4"].PutValue(7);

int chartIndex = excel.Worksheets[0].Charts.Add(ChartType.Column, 11, 0, 27, 13);

Chart chart = excel.Worksheets[0].Charts[chartIndex];
chart.NSeries.Add("A1:B4", true);

excel.ChangePalette(ColorTranslator.FromHtml("#990099"), 55); // call ChangePalette method for color #990099 is not in standard color palette


chart.NSeries[0].Area.ForegroundColor = ColorTranslator.FromHtml("#990099");

excle.Save("d:\\book1.xls");