更新:
既然你终于澄清了你想要的是混合两个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°。请注意有多少简单的混音更模糊,饱和度更低: