当角为圆角时,DrawPath 不会返回正确的形状

问题描述 投票:0回答:1

我正在尝试绘制一个带圆角的矩形。但是当我打印布局时,线条出现问题,如下图所示:

Wrong Image of Rect

而我需要打印的图像是这样的:

Correct Image of Rect

这是代码的一部分,对于找到解决方案很有用:

private void DrawBorder(LayoutObject object, Graphics graphics)
{
    var objectX = object.X;
    var objectY = object.Y;
    if (_rotate180)
    {
        (objectX, objectY) = object.CalculateRotation180(_layoutHeight, _layoutWidth);
    }

    // Calculate object dimensions based on graphics DPI
    var xDpi = objectX * graphics.DpiX;
    var yDpi = objectY * graphics.DpiY;

    var widthDpi = object.Width * graphics.DpiX;
    var heightDpi = object.Height * graphics.DpiY;

    var borderWidthX = object.BorderThickness * graphics.DpiX;
    var borderWidthY = object.BorderThickness * graphics.DpiY;

    var cornerRadiusX = object.CornerRadius * graphics.DpiX;
    var cornerRadiusY = object.CornerRadius * graphics.DpiY;

    var rectangle = new RectangleF(xDpi, yDpi, widthDpi, heightDpi);

    // Draw the border (with possible rounding)
    if (borderWidthX > 0 || borderWidthY > 0)
    {
        if (!string.IsNullOrWhiteSpace(object.BorderColor))
        {
            var brush = object.BorderColor.FromRgbaToSolidBrush();

            if (_rotate180)
            {
                graphics.RotateCenter(180.0F, rectangle);
            }

            if (object.Orientation > 0)
            {
                graphics.RotateCenter(object.Orientation, rectangle);
            }

            // Define the rectangle for drawing the border based on the original rectangle.
            var borderRectangle = new RectangleF(xDpi + (borderWidthX / 2), yDpi + (borderWidthY / 2), widthDpi - borderWidthX, heightDpi - borderWidthY);

            if (cornerRadiusX > 0 || cornerRadiusY > 0)
            {
                // Draw the rounded border.
                cornerRadiusX -= borderWidthX / 2;
                cornerRadiusY -= borderWidthY / 2;

                graphics.DrawRoundedCornerRectangle(borderRectangle, new Pen(brush, borderWidthX), cornerRadiusX, cornerRadiusY);
            }
            else
            {
                graphics.DrawRectangle(new Pen(brush, borderWidthX), borderRectangle.X, borderRectangle.Y, borderRectangle.Width, borderRectangle.Height);
            }

            graphics.ResetTransform();
        }
    }
}

圆角矩形的静态方法有:

public static void DrawRoundedCornerRectangle(this Graphics graphics, RectangleF rectangle, Pen pen, float xradius, float yradius)
        {
            using (GraphicsPath path = MakeRoundedRect(rectangle, xradius, yradius))
            {
                graphics.DrawPath(pen, path);
            }
        }

private static GraphicsPath MakeRoundedRect(RectangleF rect, float xradius, float yradius)
        {
            // Make a GraphicsPath to draw the rectangle.
            var path = new GraphicsPath();

            // Upper left corner.
            RectangleF corner = new RectangleF(rect.X, rect.Y, 2 * xradius, 2 * yradius);
            path.AddArc(corner, 180, 90);

            // Upper right corner.
            corner = new RectangleF(rect.Right - 2 * xradius, rect.Y, 2 * xradius, 2 * yradius);
            path.AddArc(corner, 270, 90);

            // Lower right corner.
            corner = new RectangleF(rect.Right - 2 * xradius, rect.Bottom - 2 * yradius, 2 * xradius, 2 * yradius);
            path.AddArc(corner, 0, 90);

            // Lower left corner.
            corner = new RectangleF(rect.X, rect.Bottom - 2 * yradius, 2 * xradius, 2 * yradius);
            path.AddArc(corner, 90, 90);

            // Join with the start point.
            path.CloseFigure();

            return path;
        }
c# draw system.drawing drawstring
1个回答
0
投票

看起来你的角半径非常大,这是由于使用 dpi 作为缩放因子而不是 dpi/baseValue 造成的。

通常您可以将 DPI 除以 96 作为缩放因子,因为 96 是相当标准的 dpi。

© www.soinside.com 2019 - 2024. All rights reserved.