【问题标题】:Drawing pictureBox c#绘制pictureBox c#
【发布时间】:2015-11-07 22:48:07
【问题描述】:

我正在尝试制作一个看起来像这样的自定义图片框 -

到目前为止,我所做的只是——

使用此代码 -

    protected void UpdateRegion()
    {
        var path = new GraphicsPath();
        Point[] points =
        {
            new Point( 0, 0),
            new Point(0, ClientSize.Height-80),
            new Point(80 , ClientSize.Height),
            new Point(ClientSize.Width-80, ClientSize.Height),
            new Point(ClientSize.Width,  ClientSize.Height-80),
            new Point(ClientSize.Width , 0)
        };
        path.AddPolygon(points);
        path.FillMode = FillMode.Winding;
        this.Region = new Region(path);
    }

【问题讨论】:

  • 你需要添加一个圆弧。
  • @TaW ,我该如何使用它?
  • 查看我的答案以获取示例。我也稍微清理了你的代码..

标签: c# picturebox graphicspath


【解决方案1】:

给你:

        GraphicsPath path = new GraphicsPath();
        path.FillMode = FillMode.Winding;

        int cut = 80;
        Rectangle cr = panel1.ClientRectangle;

        Point[] points =
        {
            new Point(0, cr.Height - cut),
            new Point(0, 0),
            new Point(cr.Width, 0),
            new Point(cr.Width, cr.Height - cut),
            new Point(cr.Width - cut, cr.Height),
            new Point(cut, cr.Height),
            new Point(0, cr.Height - cut),
        };
        path.AddPolygon(points);

        Rectangle arcRect = new Rectangle(0, cr.Height - 2 * cut, 2 * cut, 2 * cut);
        path.AddArc(arcRect, 90f, 90f);

弧由边界矩形定义,在我们的例子中,它的大小是切口的两倍。它从 x 轴顺时针方向 90° 开始,然后(至少)再延伸 90°。

您可以add it to a GraphicsPathdraw it with a Graphics 对象。

这是来自 MSDN 的引用:

如果图中有之前的直线或曲线,则添加一条直线 将前一段的端点连接到 弧线。

弧线沿椭圆的周长绘制,由 指定的矩形。弧的起点由下式确定 从椭圆的 x 轴顺时针测量(在 0 度 角度)按起始角度的度数。端点是 通过从起点顺时针测量类似地定位 扫角的度数。如果扫掠角为 大于 360 度或小于 -360 度,弧被扫过 分别正好 360 度或 -360 度。

请注意,我添加了圆弧的边界矩形仅用于演示。代码不包含它。

对于其他角的圆角切割,您需要更改和扩展点数组并添加更多/其他弧。

其他角圆弧采用这些矩形:

 Rectangle arcRectTL = new Rectangle(0, 0, 2 * cut, 2 * cut);
 Rectangle arcRectTR = new Rectangle(cr.Width - 2 * cut, 0, 2 * cut, 2 * cut);
 Rectangle arcRectBR = new Rectangle(cr.Width - 2*cut, cr.Height - 2*cut, 2*cut, 2*cut);

起始角度分别为:180°, 270° and 0°

尺寸和扫掠角度保持不变。

【讨论】:

  • 如果我需要在左上角和按钮右边有一条曲线并且其他边缘是正常的,这个代码也可以工作吗?
  • 我已将边界矩形添加到图像中以显示逻辑。对于其他角,您必须调整位置和起始角度。大小和扫掠角度保持不变。
  • 你能再举一个例子来说明你的意思吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-14
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2011-09-08
  • 2018-07-06
  • 1970-01-01
相关资源
最近更新 更多