如何从原始区域填充线性渐变画笔区域?
How to fill the linear gradient brush region from the original region?
我正在使用 LinearGradient 画笔填充区域,我使用起点和终点 Color 创建了 linearGradient 画笔。
Rectangle linearGradientregion= new Rectangl(20,-50,30,30);
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new PointF(20, -20), new PointF(50, -50), Color.Green, Color.Red);
Rectangle pathRegion= new Rectangl(-50,-25,50,50);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(pathRegion);
graphics.FillPath(linearGradientBrush, path);
并且区域大小大于linearGradientBrush边界区域。
现在我想知道如何使用 linearGradientBrush 边界区域仅使用 linearGradientBrush 填充区域,而该区域的剩余区域用另一种颜色填充。
下图是预期的输出。
http://i.stack.imgur.com/B4CHB.png
但是我得到这样的结果
http://i.stack.imgur.com/Pxwiw.png
在第一张图中,用绿色填充的圆圈超出了线性渐变画笔的范围。
在第二张图片中,渐变画笔再次填充该区域。
我想在线性渐变画笔的终点停止渐变画笔填充,并用另一种颜色填充剩余区域。
提前致谢,
帕蒂
要填充渐变,您必须指定要填充的确切区域。在你的情况下,我相信这是 linearGradientregion
:
graphics.FillRectangle(linearGradientBrush, linearGradientregion);
要填充剩余区域,请在 alternate fill mode(默认值)中使用 GraphicsPath
。将这两个矩形添加到路径中,然后外部形状将被填充,而内部形状将在裁剪区域之外,从而保持不变。例如:
using(var externalPath = new GraphicsPath(FillMode.Alternate))
{
externalPath.AddRectangle(pathRegion);
externalPath.AddRectangle(linearGradientregion);
graphics.FillPath(Brushes.Black, externalPath);
}
有几种方法可以完成此任务:
可以分两步完成,先填充圆,然后填充三角形,圆设置为[=14]的ClippingPath
(Graphics.SetClip(path)
) =]对象.
您可以分两步完成,首先将 Graphics 对象旋转您的角度,然后填充圆圈,然后填充 Rectangle
,再次将圆圈设置为 [= Graphics
对象的 12=]。最后旋转回来。
或者您可以一步完成:创建一个多色 LinearGradientBrush
,并设置位置,使绿色区域从下一个开始没有过渡。
下面是使用第三种方法(左图)和第二种方法(右图)的两种解决方案:
解决方法一使用第三种方法多色渐变:
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Rectangle linearGradientregion = new Rectangle(10, 10, 150, 150);
Rectangle pathRegion = new Rectangle(15, 15, 140, 140);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(pathRegion);
LinearGradientBrush linearGradientBrush =
new LinearGradientBrush(linearGradientregion, Color.Red, Color.Yellow, 45);
ColorBlend cblend = new ColorBlend(4);
cblend.Colors = new Color[4] { Color.Red, Color.Yellow, Color.Green, Color.Green };
cblend.Positions = new float[4] { 0f, 0.65f, 0.65f, 1f };
linearGradientBrush.InterpolationColors = cblend;
e.Graphics.FillPath(linearGradientBrush, path);
}
请注意,我没有使用您的坐标设置。我相信您可以调整这些数字。
另请注意,构造函数中的 Colors
未被 Brush
!
使用
解决方法二采用第二种方法。注意向绿色的急剧过渡:
private void panel3_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Rectangle linearGradientregion = new Rectangle(10, 10, 95, 95);
Rectangle pathRegion = new Rectangle(15, 15, 140, 140);
int centerX = pathRegion.X + pathRegion.Width / 2;
int centerY = pathRegion.Y + pathRegion.Height / 2;
GraphicsPath path = new GraphicsPath();
path.AddEllipse(pathRegion);
LinearGradientBrush linearGradientBrush =
new LinearGradientBrush(linearGradientregion, Color.Red, Color.Yellow, 45);
e.Graphics.FillPath(linearGradientBrush, path);
e.Graphics.SetClip(path);
e.Graphics.TranslateTransform(centerX, centerY);
e.Graphics.RotateTransform(45f);
e.Graphics.FillRectangle(Brushes.Green, 25, -90, 240, 240);
e.Graphics.ResetTransform();
}
再次由您来找出最佳数字..
我正在使用 LinearGradient 画笔填充区域,我使用起点和终点 Color 创建了 linearGradient 画笔。
Rectangle linearGradientregion= new Rectangl(20,-50,30,30);
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new PointF(20, -20), new PointF(50, -50), Color.Green, Color.Red);
Rectangle pathRegion= new Rectangl(-50,-25,50,50);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(pathRegion);
graphics.FillPath(linearGradientBrush, path);
并且区域大小大于linearGradientBrush边界区域。
现在我想知道如何使用 linearGradientBrush 边界区域仅使用 linearGradientBrush 填充区域,而该区域的剩余区域用另一种颜色填充。
下图是预期的输出。
http://i.stack.imgur.com/B4CHB.png
但是我得到这样的结果
http://i.stack.imgur.com/Pxwiw.png
在第一张图中,用绿色填充的圆圈超出了线性渐变画笔的范围。
在第二张图片中,渐变画笔再次填充该区域。
我想在线性渐变画笔的终点停止渐变画笔填充,并用另一种颜色填充剩余区域。
提前致谢,
帕蒂
要填充渐变,您必须指定要填充的确切区域。在你的情况下,我相信这是 linearGradientregion
:
graphics.FillRectangle(linearGradientBrush, linearGradientregion);
要填充剩余区域,请在 alternate fill mode(默认值)中使用 GraphicsPath
。将这两个矩形添加到路径中,然后外部形状将被填充,而内部形状将在裁剪区域之外,从而保持不变。例如:
using(var externalPath = new GraphicsPath(FillMode.Alternate))
{
externalPath.AddRectangle(pathRegion);
externalPath.AddRectangle(linearGradientregion);
graphics.FillPath(Brushes.Black, externalPath);
}
有几种方法可以完成此任务:
可以分两步完成,先填充圆,然后填充三角形,圆设置为[=14]的
ClippingPath
(Graphics.SetClip(path)
) =]对象.您可以分两步完成,首先将 Graphics 对象旋转您的角度,然后填充圆圈,然后填充
Rectangle
,再次将圆圈设置为 [=Graphics
对象的 12=]。最后旋转回来。或者您可以一步完成:创建一个多色
LinearGradientBrush
,并设置位置,使绿色区域从下一个开始没有过渡。
下面是使用第三种方法(左图)和第二种方法(右图)的两种解决方案:
解决方法一使用第三种方法多色渐变:
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Rectangle linearGradientregion = new Rectangle(10, 10, 150, 150);
Rectangle pathRegion = new Rectangle(15, 15, 140, 140);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(pathRegion);
LinearGradientBrush linearGradientBrush =
new LinearGradientBrush(linearGradientregion, Color.Red, Color.Yellow, 45);
ColorBlend cblend = new ColorBlend(4);
cblend.Colors = new Color[4] { Color.Red, Color.Yellow, Color.Green, Color.Green };
cblend.Positions = new float[4] { 0f, 0.65f, 0.65f, 1f };
linearGradientBrush.InterpolationColors = cblend;
e.Graphics.FillPath(linearGradientBrush, path);
}
请注意,我没有使用您的坐标设置。我相信您可以调整这些数字。
另请注意,构造函数中的 Colors
未被 Brush
!
解决方法二采用第二种方法。注意向绿色的急剧过渡:
private void panel3_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Rectangle linearGradientregion = new Rectangle(10, 10, 95, 95);
Rectangle pathRegion = new Rectangle(15, 15, 140, 140);
int centerX = pathRegion.X + pathRegion.Width / 2;
int centerY = pathRegion.Y + pathRegion.Height / 2;
GraphicsPath path = new GraphicsPath();
path.AddEllipse(pathRegion);
LinearGradientBrush linearGradientBrush =
new LinearGradientBrush(linearGradientregion, Color.Red, Color.Yellow, 45);
e.Graphics.FillPath(linearGradientBrush, path);
e.Graphics.SetClip(path);
e.Graphics.TranslateTransform(centerX, centerY);
e.Graphics.RotateTransform(45f);
e.Graphics.FillRectangle(Brushes.Green, 25, -90, 240, 240);
e.Graphics.ResetTransform();
}
再次由您来找出最佳数字..