如何附加两个画笔类

时间:2014-10-02 16:18:44

标签: c# graphics brush

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

e.g。 Brush b1 = GetFromSomewhere();

Brush b2 = GetFromSomewhereElse();

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

Brush b3 = b1 + b2; 

最终我的目的是做一些事情:

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

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

1 个答案:

答案 0 :(得分:3)

<强>更新

因为你最终澄清了你想要的是一个混合两个TextureBrushes的解决方案:

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

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

以下是一个例子:

texture mix


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

如果你想混合颜色,你可以轻松地完成它,也许是这样的:

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°的颜色相加。请注意有多少简单混音是较为模糊且饱和度较低的: two color mixes