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

发布于 2024-08-27 14:41:50 字数 484 浏览 4 评论 0原文

我需要以十六进制给出的特定颜色在位图上绘制一个圆圈。 “Brushes”类仅给出特定颜色的名称。

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.

有办法做到这一点吗?

确切的问题: 我正在生成 KML(用于 Google 地球),并且正在生成许多具有不同十六进制颜色的线条。颜色是通过数学方式生成的,我需要保持这种方式,这样我就可以制作尽可能多的颜色。我需要为每条线生成一个颜色完全相同的 PNG 图标。

I need to draw a circle onto a bitmap in a specific colour given in 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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声无音无过去 2024-09-03 14:41:50

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);
      }
   }
}

ColorTranslator.FromHtml will give you the corresponding 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);
      }
   }
}
小忆控 2024-09-03 14:41:50

使用由适当的 SolidBrush 构建的="http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx" rel="nofollow noreferrer">颜色。

例子:

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.

Use a SolidBrush constructed with the appropiate Color.

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.
若相惜即相离 2024-09-03 14:41:50

您可能必须手动解析颜色字符串。

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);

正如 Aaronaught 指出的,如果你的 ARGB 是按照这个顺序,那么 FromARGB 就会有一个重载,它接受一个整数中的所有组件:

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

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:

int argb = int.Parse(colorSpec.Substring(1), System.Globalization.NumberStyles.HexNumber);
Color fillColor = Color.FromArgb(argb);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文