【问题标题】:How to append two brush classes [closed]如何附加两个画笔类[关闭]
【发布时间】:2014-10-02 16:18:44
【问题描述】:

是否有任何方法或解决方法可以将两个 System.Drawing.Brush 类组合/附加在一起?

例如Brush b1 = GetFromSomewhere();

Brush b2 = GetFromSomewhereElse();

(类似的东西......)

Brush b3 = b1 + b2; 

最终我的目的是做这样的事情:

Graphics graphics = new Graphics; 
graphics.FillRectangle(b3, rectangle);

更新: 我有一个第三方库(无法控制它),它为我提供了一个预定义的 Brush 实例(表示填充模式,例如 ++++ 或#####)。我想用我的“自己的”画笔图案“覆盖”那个实例。

【问题讨论】:

  • 它应该是什么样子的?
  • 目前没有“组合”或“附加”两个画笔。在有人可以提供解决方法之前,您必须定义这意味着什么。
  • 如果你想混合颜色,那很容易。我们在这里谈论 SolidBrush 吗?还有其他更复杂的画笔类型,混合起来毫无意义。或者你的意思是CompoundPen
  • 为什么要投反对票?这是一个完美解释的问题
  • 不是我的反对意见,而是: - 现在是,但您花时间澄清了您的需求。投反对票的人不会回来查看问题,他们真的很赶时间,有这么多帖子,要投反对票的也太多了.. ;-)

标签: c# graphics brush


【解决方案1】:

更新:

既然你终于澄清了你想要的是混合两个TextureBrushes的解决方案:

我假设您在Image img2 中有TextureBrush 的模式。使用下面的简单方法混合两个相同大小的图像,并使用组合图案创建新画笔:

TextureBrush brush3 = new TextureBrush(
                      mixBitmaps( (Bitmap)(brush1.Image), (Bitmap) img2)  );

现在你可以用它来绘画或填充东西了..

Bitmap mixBitmaps(Bitmap bmp1, Bitmap bmp2)
{
    using (Graphics G = Graphics.FromImage(bmp1) )
    {
        G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
        G.DrawImage(bmp2, Point.Empty);
    }
    return bmp1;
}

这是一个例子:


我保留旧答案,因为有些人也对此感兴趣:

如果你想混合颜色,你可以很容易地做到这一点,也许像这样:

SolidBrush MixColor(SolidBrush b1, SolidBrush b2)
{
    return new SolidBrush(Color.FromArgb(Math.Max(b1.Color.A, b2.Color.A),
                         (b1.Color.R + b2.Color.R) / 2, (b1.Color.G + b2.Color.G) / 2,
                         (b1.Color.B + b2.Color.B) / 2));
}

您可能希望将 Alpha 通道设置为固定值 255。

但是,这是一个简单的平均计算,如果颜色不接近,它将无法正常工作。

为了获得更好的混合效果,您可以将色调与饱和度和亮度值分开混合,也就是说,您必须转换为 HSL 或 HSV,在那里混合并转换回 RGB..

这是一个应该做到这一点的版本:

SolidBrush MixBrushes(SolidBrush br1, SolidBrush br2)
{   
    return new SolidBrush ( MixColorHSV( br1.Color, br2.Color) );
} 

Color MixColorHSV(Color c1 , Color c2 )
{
    double h1 = c1.GetHue();
    double h2 = c2.GetHue();
    double d = (h2 - h1) / 2d;
    double h = h1 + d;
    if (d > 90) h -= 180;  else if (d < -90) h += 180;  // correction 1!
    if (h < 0) h += 360; else if (h > 360) h -= 360;    // correction 2!

    int max1 = Math.Max(c1.R, Math.Max(c1.G, c1.B));
    int min1 = Math.Min(c1.R, Math.Min(c1.G, c1.B));
    double s1 = (max1 == 0) ? 0 : 1d - (1d * min1 / max1);
    double v1 = max1 / 255d;

    int max2 = Math.Max(c2.R, Math.Max(c2.G, c2.B));
    int min2 = Math.Min(c2.R, Math.Min(c2.G, c2.B));
    double s2 = (max2 == 0) ? 0 : 1d - (1d * min2 / max2);
    double v2 = max2 / 255d;

    double s = (s1 + s2) / 2d;
    double v = (v1 + v2) / 2d;

    return ColorFromHSV(h,s,v);
}

public static Color ColorFromHSV(double hue, double saturation, double value)
{
    int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
    double f = hue / 60 - Math.Floor(hue / 60);

    value = value * 255;
    int v = Convert.ToInt32(value);
    int p = Convert.ToInt32(value * (1 - saturation));
    int q = Convert.ToInt32(value * (1 - f * saturation));
    int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));

    if (hi == 0)       return Color.FromArgb(255, v, t, p);
    else if (hi == 1)  return Color.FromArgb(255, q, v, p);
    else if (hi == 2)  return Color.FromArgb(255, p, v, t);
    else if (hi == 3)  return Color.FromArgb(255, p, q, v);
    else if (hi == 4)  return Color.FromArgb(255, t, p, v);
    else               return Color.FromArgb(255, v, p, q);
}

请参阅this post 了解部分转换!

这是一个颜色图表,比较了添加红色的两种混合颜色,颜色各相差 30°。请注意有多少简单的混音更模糊,饱和度更低:

【讨论】:

  • 就是这样!完美,谢谢
猜你喜欢
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多