【问题标题】:Fill graphics path with two colors用两种颜色填充图形路径
【发布时间】:2013-06-07 16:25:57
【问题描述】:

我正在 WinForms 应用程序中绘制一些字形。每个字形由一个图形路径定义,基本上是一个圆角矩形。

现在我用一种颜色填充图形路径,但我需要用两种颜色填充。下面的例子解释了我需要什么:

我想避免创建新的GraphicsPath,因为应用程序的性能可能会受到影响。

在不创建新图形路径的情况下绘制第二种填充颜色是否有任何棘手的选项?

这是我的图形路径的代码:

public class RoundedRectangle
{
    public static GraphicsPath Create(int x, int y, int width, int height)
    {
        int radius = height / 2;
        int xw = x + width;
        int yh = y + height;
        int xwr = xw - radius;
        int xr = x + radius;
        int r2 = radius * 2;
        int xwr2 = xw - r2;

        GraphicsPath p = new GraphicsPath();

        p.StartFigure();

        // Right arc
        p.AddArc(xwr2, y, r2, r2, 270, 180);

        //Bottom Edge
        p.AddLine(xwr, yh, xr, yh);

        // Left arc
        p.AddArc(x, y, r2, r2, 90, 180);

        //closing the figure adds the top Edge automatically
        p.CloseFigure();

        return p;
    }
}

【问题讨论】:

    标签: .net winforms drawing graphicspath


    【解决方案1】:

    “在不创建新图形路径的情况下绘制第二种填充颜色是否有任何棘手的选项?”

    您以非矩形方式分割中间区域,因此您需要一个 GraphicsPath 来表示它。

    这是我想出的:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(Form1_Paint);
        }
    
        GraphicsPath rect = RoundedRectangle.Create(100, 100, 100, 35);
    
        void Form1_Paint(object sender, PaintEventArgs e)
        {
            TwoColorFill(e.Graphics, rect, Color.Yellow, Color.Blue, Color.Gray, 5);
        }
    
        private void TwoColorFill(Graphics G, GraphicsPath roundRect, Color FillColorLeft, Color FillColorRight, Color BorderColor, float BorderThickness)
        {
            using (SolidBrush RightFill = new SolidBrush(FillColorRight))
            {
                G.FillPath(RightFill, roundRect);
            }
    
            using (SolidBrush LeftFill = new SolidBrush(FillColorLeft))
            {
                GraphicsPath gp = new GraphicsPath();
                gp.AddPolygon(new Point[] { 
                    new Point((int)roundRect.GetBounds().Left, (int)roundRect.GetBounds().Top), 
                    new Point((int)roundRect.GetBounds().Right, (int)roundRect.GetBounds().Top), 
                    new Point((int)roundRect.GetBounds().Left, (int)roundRect.GetBounds().Bottom)
                });
                G.SetClip(gp);
                G.FillPath(LeftFill, rect);
                G.ResetClip();
            }
    
            using (Pen p = new Pen(BorderColor, BorderThickness))
            {
                G.DrawPath(p, roundRect);
            }
        }
    
    }
    

    *经过进一步思考,技术上可能可以通过使用 GraphicsPath 本身进行剪切,平移到其中心,执行旋转,然后绘制一个边缘沿 x 轴的填充矩形。不过,您必须以某种方式计算正确的角度,而且我不确定这是否真的会比创建上面的额外 GraphicsPath 节省任何性能。

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多