【问题标题】:Rounded edges in picturebox C#图片框C#中的圆角边缘
【发布时间】:2021-04-06 13:51:53
【问题描述】:

如何在图片框控件中圆边。我想得到像椭圆这样的角度,但我不知道该怎么做。我使用 C#。谢谢!

【问题讨论】:

    标签: c# picturebox edges


    【解决方案1】:

    是的,没问题,您可以使用其 Region 属性为控件赋予任意形状。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    
    class OvalPictureBox : PictureBox {
        public OvalPictureBox() {
            this.BackColor = Color.DarkGray;
        }
        protected override void OnResize(EventArgs e) {
            base.OnResize(e);
            using (var gp = new GraphicsPath()) {
                gp.AddEllipse(new Rectangle(0, 0, this.Width-1, this.Height-1));
                this.Region = new Region(gp);
            }
        }
    }
    

    【讨论】:

    • 啊,谢谢!当你扩展一个类时,它确实给出了你所拥有的可能性的形象。以及在您的工具箱中获得修改后的组件是多么容易。 :)
    【解决方案2】:

    将 1 个图片框放在表单上并编写此代码 您也可以更改宽度和高度旁边的负数以获得最佳效果

     System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
                gp.AddEllipse(0, 0, pictureBox1.Width - 3, pictureBox1.Height - 3);
                Region rg = new Region(gp);
                pictureBox1.Region = rg;
    

    【讨论】:

    • 谢谢,这是一个很好的解决必要语法数量的解决方案。我还没有测试过这会消耗多少资源,但我现在喜欢它。还有,漂亮的图片:P
    【解决方案3】:

    圆的和圆的一样?

    如果是这样,请查看http://social.msdn.microsoft.com/forums/en-US/winforms/thread/603084bb-1aae-45d1-84ae-8544386d58fd

    Rectangle r = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
    System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
    int d = 50;
    gp.AddArc(r.X, r.Y, d, d, 180, 90);
    gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90);
    gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
    gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90);
    pictureBox1.Region = new Region(gp);
    

    【讨论】:

      【解决方案4】:

      谢谢你,汉斯。但我也需要一个流畅的外观。我对此主题进行了一些研究,但找不到解决方案。然后我尝试自己做,并在下面找到了解决方案。也许其他人需要它。

      protected override void OnPaint(PaintEventArgs e)
          {
              base.OnPaint(e);
              using (GraphicsPath gp = new GraphicsPath())
              {
                  gp.AddEllipse(0, 0, this.Width - 1, this.Height - 1);
                  Region = new Region(gp);
                  e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                  e.Graphics.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 1), 0, 0, this.Width - 1, this.Height - 1);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2013-06-24
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-21
        • 2011-07-02
        相关资源
        最近更新 更多