本文介绍了如何从原来的区域填充线性渐变画笔区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的LinearGradient刷填充区域,我使用出发点和落脚点,颜色创建的LinearGradient画笔。

I’m using LinearGradient brush to fill the region and I created the linearGradient brush using starting and ending points, Color.

  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边界面积大。

And the region size is greater than linearGradientBrush bounds area.

现在我想知道怎么填只用使用一个LinearGradientBrush和剩余距离充满另一种颜色的区域区一个LinearGradientBrush边界区域的区域。

Now I want to know how to fill the region using linearGradientBrush bounds area only using linearGradientBrush and remaining area from the region filled with another color.

下面的图像输出的期望。

Below image is expect output.

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

不过,我得到这样的结果。

But I get the result like this

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

在第一个图像圈充满绿色超出线性渐变画笔的界限。

In first image the circle filled with green color is beyond the bounds of linear gradient brush.

在第二图像渐变画笔再次出现,以填补该区域。

In second image the gradient brush again comes to fill that region.

我想停止刷渐变填充的线性渐变刷终点和填充另一种颜色剩余的区域。

I want to stop the gradient brush fill at end point of linear gradient brush and fill the remaining area with another color.

在此先感谢,

Parthi

推荐答案

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

There are several ways to accomplish this task:


  • 您可以做两步骤,先填补了一圈,然后填充设置为 ClippingPath Graphics.SetClip(路径))的图形的对象。

您可以做的两个步骤,第一旋转图形通过你的角度,则填补了一圈,然后填充对象的长方形,再次设置的 ClippingPath 圆在图形对象。最后旋转回

You could do it in two steps, first rotating the Graphics object by your angle, then filling the circle and then filling a Rectangle, again with the circle set as the ClippingPath of the Graphics object. Finally rotate back.

或者你可以在一个步骤做到这一点:创建一个多彩 一个LinearGradientBrush ,并让绿色区域与旁边没有过渡开始设置的位置。

Or you can do it in one step: Create a multicolored LinearGradientBrush, and set the positions so that the green area starts with next to no transition.

下面是利用第三方法(左侧图像)和第二个(右图像)两种解决方案:

Here are two solutions using the third method (left image) and the second one (right image):

解决方案一使用第三种方法用五彩梯度:

Solution one using the third method with a multicolored gradient:

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

请注意,我没有用你的坐标设置。我敢肯定,您可以调整数字。

Note that I didn't use your coordinate setup.. I'm sure you can adapt the numbers.

另外请注意,颜色从构造函数中不使用

Also note that the Colors from the constructor are not used by the Brush!

解决方案二使用第二种方法。注意绿色的急剧转变:

Solution two using the second method. Note the sharp transition to green:

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

同样是由你来找出最佳的数字..

Again it is up to you to figure out the best numbers..

这篇关于如何从原来的区域填充线性渐变画笔区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:13