使用 TextureBrush 时线条粗细看起来不规则

Lines look irregular in thickness when using TextureBrush

我打算用Texture Brush画出同样粗细的线条。 但是线条的粗细看起来不规则。

如果可以的话,我不想以 0.1mm 为单位停止绘图。 因为用户的代码快完成了。

如果你有想法,我将不胜感激。

The lines in display

    //Brush User's Code
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        // I need to draw by 0.1mm unit like CAD.
        e.Graphics.PageUnit = GraphicsUnit.Millimeter;
        e.Graphics.PageScale = 0.1F;

        Point leftTop = new Point(2, 2);
        using(TextureBrush aBrush = myBrush()){
            aBrush.TranslateTransform(leftTop.X, leftTop.Y);
            e.Graphics.FillRectangle(aBrush, leftTop.X,leftTop.Y, 90, 1000);
        }
    }

    //BrushMaker Code
    private  TextureBrush myBrush()
    {

        Bitmap bitmapCanvas = new Bitmap(20, 10);
        Graphics g = Graphics.FromImage(bitmapCanvas);

        Pen myPen = new Pen(Brushes.Black, 2F);
        g.DrawLine(myPen, 2, 5, 18, 5);

        var brush = new TextureBrush(bitmapCanvas);

        myPen.Dispose();
        g.Dispose();
        bitmapCanvas.Dispose();

        return brush;
    }

绘图模糊,因为 e.Graphics.PageScale 与 1 不同。在屏幕上绘图时,您确实必须只使用 GraphicsUnit.Pixel,所有其他值都必须转换为像素。

由于惯例是所有显示器每英寸有 96 个像素,根据定义 1 英寸 == 25.4 毫米,我们有 0.1 毫米 == 0.1 毫米 * 96(像素/英寸)*(1 英寸/25.4毫米)== 0.378 像素(大约)。让我们以0.4像素为基本单位。通过将它们乘以 0.4 来转换代码中的所有数字。而不是 Point(2, 2),而是 Point(1, 1)90, 1000 变为 36, 400Bitmap(20, 10) 现在是 Bitmap(8, 4)2F2, 5, 18, 5 变成 1F1, 2, 7, 2.

毕竟这张图片不会再模糊了。你付出的代价是 0.4 并不完全是 0.378(实际上是 0.3779527559...),因此,一切都会显得大 5.5%,但没有人会注意到。