奇怪的是画的GraphicsPath与Graphics.FillPath [英] Oddly drawn GraphicsPath with Graphics.FillPath

查看:5171
本文介绍了奇怪的是画的GraphicsPath与Graphics.FillPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些代码,创建了一个圆角矩形的GraphicsPath ,基于自定义结构, BorderRadius (其中让我来定义左上角,右上角,矩形的左下角和右下角的半径),初始长方形本身:

 公共静态的GraphicsPath CreateRoundRectanglePath(BorderRadius半径,长方形矩形)
{
GraphicsPath的结果=新的GraphicsPath();

如果(radius.TopLeft大于0)
{
result.AddArc(rectangle.X,rectangle.Y,radius.TopLeft,radius.TopLeft,180,90) ;
}
,否则
{
result.AddLine(新System.Drawing.Point(rectangle.X,rectangle.Y),新System.Drawing.Point(rectangle.X, rectangle.Y));
}
如果(radius.TopRight大于0)
{
result.AddArc(rectangle.X + rectangle.Width - radius.TopRight,rectangle.Y,radius.TopRight ,radius.TopRight,270,90);
}
,否则
{
result.AddLine(新System.Drawing.Point(rectangle.X + rectangle.Width,rectangle.Y),新System.Drawing.Point( rectangle.X + rectangle.Width,rectangle.Y));
}
如果(radius.BottomRight大于0)
{
result.AddArc(rectangle.X + rectangle.Width - radius.BottomRight,rectangle.Y + rectangle.Height - radius.BottomRight,radius.BottomRight,radius.BottomRight,0,90);
}
,否则
{
result.AddLine(新System.Drawing.Point(rectangle.X + rectangle.Width,rectangle.Y + rectangle.Height),新的系统。 Drawing.Point(rectangle.X + rectangle.Width,rectangle.Y + rectangle.Height));
}
如果(radius.BottomLeft大于0)
{
result.AddArc(rectangle.X,rectangle.Y + rectangle.Height - radius.BottomLeft,radius.BottomLeft ,radius.BottomLeft,90,90);
}
,否则
{
result.AddLine(新System.Drawing.Point(rectangle.X,rectangle.Y + rectangle.Height),新System.Drawing.Point( rectangle.X,rectangle.Y + rectangle.Height));
}

返回结果;
}

现在,如果我用这个与FillPath和DrawPath以来,我注意到一些奇怪的结果

  GraphicsPath的路径= CreateRoundRectanglePath(新BorderRadius(8)新的Rectangle(10,10,100,100)); 
e.Graphics.DrawPath(新朋(Color.Black,1),路径);
e.Graphics.FillPath(新SolidBrush(Color.Black),路径);



我已经放大到每个结果长方形 (右手边),这样你可以清楚地看到,问题:



矩形



我想知道的是:为什么都在绘制的矩形弧平等的,所有的弧上的填充矩形奇?



更妙的是,可以将其固定,从而使填充矩形绘制正确?



编辑:是否有可能填补的GraphicsPath内不使用FillPath



编辑:根据意见....这里是BorderRadius结构

 公共结构BorderRadius 
{
公众的Int32左上{搞定;组; }
公众的Int32 TopRight {搞定;组; }
公众的Int32 BOTTOMLEFT {搞定;组; }
公众的Int32 BottomRight {搞定;组; }

公共BorderRadius(INT全部):这个()
{
this.TopLeft = this.TopRight = this.BottomLeft = this.BottomRight =所有;
}
}


解决方案

我经历了同样的问题,并找到了解决办法。可能是你来不及@seriesOne但如果他们有这个问题,它可以给其他人有用。使用基本上当填充的方法(也设置圆角矩形​​与Graphics.SetClip剪贴路径时),我们有一个像素的右侧和底部线移动
。所以,我想出了一个接受的参数来解决这个矩形是使用填充与否的方法。在这里,它是:



 私有静态的GraphicsPath CreateRoundedRectangle(矩形B,INT R,布尔补= FALSE)
{
VAR路径=新的GraphicsPath();
变种R2 =(int)的R / 2;
VAR修复=填? 1:0;

b.Location =新的点(b.X - 1,b.Y - 1);
如果
b.Size =新的大小(b.Width - 1,b.Height - 1)(个够!);

path.AddArc(b.Left,b.Top,R,R,180,90);
path.AddLine(b.Left + R2,b.Top,b.Right - R2 - 修复,b.Top);

path.AddArc(b.Right - R的 - 修复,b.Top,R,R,270,90);
path.AddLine(b.Right,b.Top + R2,b.Right,b.Bottom - R2);

path.AddArc(b.Right - R的 - 修复,b.Bottom - R的 - 修复,R,R,0,90);
path.AddLine(b.Right - R2,b.Bottom,b.Left + R2,b.Bottom);

path.AddArc(b.Left,b.Bottom - R的 - 修复,R,R,90,90);
path.AddLine(b.Left,b.Bottom - R2,b.Left,b.Top + R2);

返回路径;
}



因此​​,这是你如何使用它:

  g.DrawPath(新笔(Color.Red),CreateRoundedRectangle(RECT,24,FALSE)); 
g.FillPath(新SolidBrush(Color.Red),CreateRoundedRectangle(RECT,24,真正的));


I have written some code which creates a rounded rectangle GraphicsPath, based on a custom structure, BorderRadius (which allows me to define the top left, top right, bottom left and bottom right radius of the rectangle), and the initial Rectangle itself:

public static GraphicsPath CreateRoundRectanglePath(BorderRadius radius, Rectangle rectangle)
{
    GraphicsPath result = new GraphicsPath();

    if (radius.TopLeft > 0)
    {
        result.AddArc(rectangle.X, rectangle.Y, radius.TopLeft, radius.TopLeft, 180, 90);
    }
    else
    {
        result.AddLine(new System.Drawing.Point(rectangle.X, rectangle.Y), new System.Drawing.Point(rectangle.X, rectangle.Y));
    }
    if (radius.TopRight > 0)
    {
        result.AddArc(rectangle.X + rectangle.Width - radius.TopRight, rectangle.Y, radius.TopRight, radius.TopRight, 270, 90);
    }
    else
    {
        result.AddLine(new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y), new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y));
    }
    if (radius.BottomRight > 0)
    {
        result.AddArc(rectangle.X + rectangle.Width - radius.BottomRight, rectangle.Y + rectangle.Height - radius.BottomRight, radius.BottomRight, radius.BottomRight, 0, 90);
    }
    else
    {
        result.AddLine(new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height));
    }
    if (radius.BottomLeft > 0)
    {
        result.AddArc(rectangle.X, rectangle.Y + rectangle.Height - radius.BottomLeft, radius.BottomLeft, radius.BottomLeft, 90, 90);
    }
    else
    {
        result.AddLine(new System.Drawing.Point(rectangle.X, rectangle.Y + rectangle.Height), new System.Drawing.Point(rectangle.X, rectangle.Y + rectangle.Height));
    }

    return result;
}

Now if I use this along with FillPath and DrawPath, I notice some odd results:

GraphicsPath path = CreateRoundRectanglePath(new BorderRadius(8), new Rectangle(10, 10, 100, 100));
e.Graphics.DrawPath(new Pen(Color.Black, 1), path);
e.Graphics.FillPath(new SolidBrush(Color.Black), path);

I've zoomed into each resulting Rectangle (right hand side) so you can see clearly, the problem:

What I would like to know is: Why are all of the arcs on the drawn rectangle equal, and all of the arcs on the filled rectangle, odd?

Better still, can it be fixed, so that the filled rectangle draws correctly?

EDIT: Is it possible to fill the inside of a GraphicsPath without using FillPath?

EDIT: As per comments....here is an example of the BorderRadius struct

public struct BorderRadius
{
    public Int32 TopLeft { get; set; }
    public Int32 TopRight { get; set; }
    public Int32 BottomLeft { get; set; }
    public Int32 BottomRight { get; set; }

    public BorderRadius(int all) : this()
    {
        this.TopLeft = this.TopRight = this.BottomLeft = this.BottomRight = all;
    }
}

解决方案

I experienced the same problem and found a solution. Might be too late for you @seriesOne but it can be useful to other people if they have this problem. Basically when using the fill methods (and also when setting the rounded rectangle as the clipping path with Graphics.SetClip) we have to move by one pixel the right and bottom lines. So I came up with a method that accepts a parameter to fix the rectangle is using the fill or not. Here it is:

private static GraphicsPath CreateRoundedRectangle(Rectangle b, int r, bool fill = false)
{
    var path = new GraphicsPath();
    var r2 = (int)r / 2;
    var fix = fill ? 1 : 0;

    b.Location = new Point(b.X - 1, b.Y - 1);
    if (!fill)
        b.Size = new Size(b.Width - 1, b.Height - 1);

    path.AddArc(b.Left, b.Top, r, r, 180, 90);
    path.AddLine(b.Left + r2, b.Top, b.Right - r2 - fix, b.Top);

    path.AddArc(b.Right - r - fix, b.Top, r, r, 270, 90);
    path.AddLine(b.Right, b.Top + r2, b.Right, b.Bottom - r2);

    path.AddArc(b.Right - r - fix, b.Bottom - r - fix, r, r, 0, 90);
    path.AddLine(b.Right - r2, b.Bottom, b.Left + r2, b.Bottom);

    path.AddArc(b.Left, b.Bottom - r - fix, r, r, 90, 90);
    path.AddLine(b.Left, b.Bottom - r2, b.Left, b.Top + r2);

    return path;
}

So this is how you use it:

g.DrawPath(new Pen(Color.Red), CreateRoundedRectangle(rect, 24, false));
g.FillPath(new SolidBrush(Color.Red), CreateRoundedRectangle(rect, 24, true));

这篇关于奇怪的是画的GraphicsPath与Graphics.FillPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆