【问题标题】:How to fill the linear gradient brush region from the original region?如何从原始区域填充线性渐变画笔区域?
【发布时间】:2015-03-13 04:44:11
【问题描述】:

我正在使用 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 bounds area。

现在我想知道如何使用 linearGradientBrush 边界区域填充区域,仅使用 linearGradientBrush 和区域的剩余区域填充另一种颜色。

下图是预期输出。

   http://i.stack.imgur.com/B4CHB.png

但我得到这样的结果

  http://i.stack.imgur.com/Pxwiw.png

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

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

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

提前致谢,

帕提

【问题讨论】:

  • 每次我读到这个问题我都不太了解。你能张贴你想要的图片吗?另外:RegionGDI+ 中的一个类,与GraphicsPath 密切相关。我在您的代码中没有看到Region。如果您只是指某个区域,请不要称它为Region
  • Region 是表示区域的通用词,所以我可以在这里使用它。如果你有这个问题的答案,请与我分享。
  • Region.Net/GDI+ 中的保留字,在这里使用它只会造成混乱。如果我能理解,我可能会回答你的问题。如果 Lc 的回答还不够,请考虑上传图片!
  • 现在请找到我的问题,并请提供您的宝贵想法来解决问题。
  • 感谢您的图片。现在我明白你的意思了。 (一张图片往往胜过一千个字)看看两个例子..

标签: c# gdi+


【解决方案1】:

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

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

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

  • 或者您可以一步完成:创建一个multicolored 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();
}

再次由您决定最佳数字..

【讨论】:

    【解决方案2】:

    要填充渐变,您必须指定要填充的确切区域。在你的情况下,我相信这是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);
    }
    

    【讨论】:

    • 感谢您的帮助。我有一个圆形路径和线性画笔,现在我想使用线性渐变画笔边界填充圆形路径,并用另一种颜色填充圆形路径的剩余区域。是否可以在 GDI+ 中进行?
    最近更新 更多