Hi,
I am trying to set fill color of a rectangle. I tried using RgbColorModelHex by setting its value - coming from RGB color. But its not working. It works when I hard code the value to something like below:
'Set Fill color
Dim rgbColorModelHex1 As New A.RgbColorModelHex()
rgbColorModelHex1.Val = "C00000" '#check
But I need to set it dynamically i.e. value will be coming in RGB from an object. So I converted RGB value to hex and tried using it but color is not applied.
Any help?
Thanks in anticipation.
When you use RgbColorModelHex, its value should be in hex string.
Try this,
using OpenXmlDrawing = DocumentFormat.OpenXml.Drawing;
......
System.Drawing.Color dynamicColor = Color.FromArgb(0,0,255);
OpenXmlDrawing.SolidFill colorObj = new OpenXmlDrawing.SolidFill() { RgbColorModelHex = new OpenXmlDrawing.RgbColorModelHex() { Val = new HexBinaryValue(dynamicColor.ToHexString()) } };
....
// an extension method
public static string ToHexString(this System.Drawing.Color color) { byte[] bytes = new byte[3]; bytes[0] = color.R; bytes[1] = color.G; bytes[2] = color.B; char[] chars = new char[bytes.Length * 2]; for (int i = 0; i < bytes.Length; i++) { int b = bytes[i]; chars[i * 2] = hexDigits[b >> 4]; chars[i * 2 + 1] = hexDigits[b & 0xF]; } return new string(chars); } Also check colorObj is getting appended to its Parent object (ShapeProperties) properly or not. The order/sequence where you append matters a lot.
public static string ToHexString(this System.Drawing.Color color) { byte[] bytes = new byte[3]; bytes[0] = color.R; bytes[1] = color.G; bytes[2] = color.B; char[] chars = new char[bytes.Length * 2]; for (int i = 0; i < bytes.Length; i++) { int b = bytes[i]; chars[i * 2] = hexDigits[b >> 4]; chars[i * 2 + 1] = hexDigits[b & 0xF]; } return new string(chars); }
Also check colorObj is getting appended to its Parent object (ShapeProperties) properly or not. The order/sequence where you append matters a lot.
Ok but where is hexDigits method defined?
static char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};