如何从原始区域填充线性渐变画笔区域?

我正在使用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://sofzh.miximages.com/c%23/B4CHB.png 

但我得到的结果是这样的

  http://sofzh.miximages.com/c%23/Pxwiw.png 

在第一张图像中,填充绿色的圆圈超出了线性渐变画笔的界限。

在第二幅图像中,渐变画笔再次填充该区域。

我想在线性渐变画笔的终点处停止渐变画笔填充,并用另一种颜色填充剩余区域。

提前致谢,

Parthi

有几种方法可以完成此任务:

  • 您可以分两步完成,首先填充圆,然后填充三角形,将圆形设置为Graphics对象的ClippingPathGraphics.SetClip(path) )。

  • 您可以分两步完成,首先按角度旋转Graphics对象,然后填充圆,然后填充Rectangle ,再次将圆设置为Graphics对象的ClippingPath 。 最后回转。

  • 或者你可以一步完成:创建一个多色的 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); } 

请注意,我没有使用您的坐标设置..我相信您可以调整数字。

另请注意, Brush不使用构造函数中的Colors

解决方案二使用第二种方法。 注意到绿色的急剧过渡:

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

再次由你决定最好的数字..

要填充渐变,您必须指定要填充的确切区域 。 在你的情况下,我相信这是linearGradientregion

 graphics.FillRectangle(linearGradientBrush, linearGradientregion); 

要填充剩余区域,请在备用填充模式下使用GraphicsPath (默认值)。 将两个矩形添加到路径中,然后将填充外部形状并将剪切区域外部的内部形状单独留下。 例如:

 using(var externalPath = new GraphicsPath(FillMode.Alternate)) { externalPath.AddRectangle(pathRegion); externalPath.AddRectangle(linearGradientregion); graphics.FillPath(Brushes.Black, externalPath); }