【问题标题】:System Drawing Invert Region build out of Path系统绘图反转区域构建路径
【发布时间】:2020-11-18 02:00:13
【问题描述】:

我有一个panel,我在那个panel 上画了一颗心。 但我不想画心我想画除了心之外的一切,所以心是透明的。 我可以反转从Path 中选择的Region 吗?

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
            path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
            path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
            this.Region = new Region(path);
            this.BackColor = Color.Black;

长什么样子(白色=透明):

我希望它看起来像什么(白色 = 透明):

【问题讨论】:

    标签: c# winforms drawing


    【解决方案1】:

    我认为您可以将 2 个图形路径一起添加。

    你可以试试这个代码:

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        GraphicsPath path = new GraphicsPath();
        path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
        path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
        path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
    
        GraphicsPath path2 = new GraphicsPath();
        path2.AddRectangle(new Rectangle(new Point(0, 0), panel1.Size));
    
        path2.AddPath(path, false);
    
        e.Graphics.FillPath(Brushes.Black, path2);
    }
    

    结果是:

    【讨论】:

      【解决方案2】:

      您可以尝试从面板的 Paint 事件的 Graphic 对象中排除一个区域:

      GraphicsPath path = new GraphicsPath();
      path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
      path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
      path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
      
      using (Region r = new Region(path)) {
        e.Graphics.ExcludeClip(r);
      }
      
      // continue drawing...
      e.Graphics.Clear(Color.Yellow);
      

      或者如果尝试修改控件的区域,则只需使用区域的 Exclude 属性:

      GraphicsPath path = new GraphicsPath();
      path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
      path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
      path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
      
      Region r = new Region(new Rectangle(Point.Empty, this.ClientSize));
      r.Exclude(path);
      
      this.Region = r;
      

      【讨论】:

        【解决方案3】:

        我不知道反转选择的方法,但你总是可以在背景色中画一颗心,然后将背景色改为黑色。

        this.BackColor = Form.BackColor;
        Form.BackColor = Color.Black;
        

        【讨论】:

        • 我相信透明是一种有效的颜色选择。但这对外面的黑色没有帮助,所以这不会很有帮助。
        【解决方案4】:

        原解决方案here。我已根据您的需要对其进行了修改

         this.Region = InvertRegion(new Region(path), this.Width, this.Height);
        

        private Region InvertRegion(Region region, int width, int height)
        {
            Bitmap mask = new Bitmap(width, height);
            Graphics.FromImage(mask).FillRegion(Brushes.Black, region);
        
            int matchColor = Color.Black.ToArgb();
        
            Region inverted = new System.Drawing.Region();
            inverted.MakeEmpty();
            Rectangle rc = new Rectangle(0, 0, 0, 0);
            bool inimage = false;
            for (int y = 0; y < mask.Height; y++)
            {
                for (int x = 0; x < mask.Width; x++)
                {
                    if (!inimage)
                    {
                        if (mask.GetPixel(x, y).ToArgb() != matchColor)
                        {
                            inimage = true;
                            rc.X = x;
                            rc.Y = y;
                            rc.Height = 1;
                        }
                    }
                    else
                    {
                        if (mask.GetPixel(x, y).ToArgb()  == matchColor)
                        {
                            inimage = false;
                            rc.Width = x - rc.X;
                            inverted.Union(rc);
                        }
                    }
        
                }
                if (inimage)
                {
                    inimage = false;
                    rc.Width = mask.Width - rc.X;
                    inverted.Union(rc);
                }
            }
            return inverted;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多