How to draw a filled circle on a graphic in hexadecimal color?

I need to draw a circle on a bitmap of a specific color specified in Hex. The Brushes class only gives specific colors with names.

Bitmap bitmap = new Bitmap(20, 20);
Graphics g = Graphics.FromImage(bitmap);
g.FillEllipse(Brushes.AliceBlue, 0, 0, 19, 19); //The input parameter is not a Hex
//g.FillEllipse(new Brush("#ff00ffff"), 0, 0, 19, 19); <<This is the kind of think I need.

Is there any way to do this?

Exact problem: I generate KML (for Google Earth) and I create many lines with different Hex colors. Colors are generated mathematically, and I need to save it so that I can make as many colors as I want. I need to create a PNG icon for each of the lines that have the same color.

+3
source share
3 answers

ColorTranslator.FromHtml will provide you with the appropriate System.Drawing.Color:

using (Bitmap bitmap = new Bitmap(20, 20))
{
   using (Graphics g = Graphics.FromImage(bitmap))
   {
      using (Brush b = new SolidBrush(ColorTranslator.FromHtml("#ff00ffff")))
      {
         g.FillEllipse(b, 0, 0, 19, 19);
      }
   }
}
+11

SolidBrush, Color.

:

Color color = Color.FromArgb(0x00,0xff,0xff,0x00); // Channels: Alpha, Red, Green, Blue.
SolidBrush brush = new SolidBrush(color);
// Use this brush in your calls to FillElipse.
+2

You may need to manually parse the colored string.

string colorSpec = "#ff00ffff";
byte alpha = byte.Parse(colorSpec.Substring(1,2), System.Globalization.NumberStyles.HexNumber);
byte red = byte.Parse(colorSpec.Substring(3, 2),System.Globalization.NumberStyles.HexNumber);
byte green = byte.Parse(colorSpec.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
byte blue = byte.Parse(colorSpec.Substring(7, 2), System.Globalization.NumberStyles.HexNumber);
Color fillColor = Color.FromArgb(alpha, red, green, blue);

As Aaronaught points out, if your ARGB is in that order, there is an overload for FromARGB that accepts all components as a whole:

int argb = int.Parse(colorSpec.Substring(1), System.Globalization.NumberStyles.HexNumber);
Color fillColor = Color.FromArgb(argb);
0
source

Source: https://habr.com/ru/post/1739758/


All Articles