简体   繁体   English

如何以十六进制颜色在图形对象上绘制实心圆?

[英]How do I draw a filled circle onto a graphics object in a hexadecimal colour?

I need to draw a circle onto a bitmap in a specific colour given in Hex. 我需要在Hex中给出的特定颜色的位图上绘制一个圆圈。 The "Brushes" class only gives specific colours 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 a way of doing this? 有办法做到这一点吗?

The exact problem: I am generating KML (for Google earth) and I am generating lots of lines with different Hex colours. 确切的问题:我正在生成KML(谷歌地球),我正在生成许多具有不同十六进制颜色的行。 The colours are generated mathematically and I need to keep it that way so I can make as many colours as I want. 颜色是以数学方式生成的,我需要保持这种颜色,这样我就能制作出我想要的颜色。 I need to generate a PNG icon for each of the lines that is the same colour exactly. 我需要为每条完全相同颜色的线生成一个PNG图标。

ColorTranslator.FromHtml will give you the corresponding System.Drawing.Color: ColorTranslator.FromHtml将为您提供相应的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);
      }
   }
}

Use a SolidBrush constructed with the appropiate Color . 使用使用适当颜色构造的SolidBrush

Example: 例:

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.

You may have to manually parse the color 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 in one integer: 正如Aaronaught指出的那样,如果您的ARGB按此顺序排列,则FromARGB会出现一个超载,它接受一个整数中的所有组件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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