How can I scale up the hatchstyle drawn in a rectangle? [closed]

99封情书 提交于 2019-12-01 18:33:23

The most direct but probably not very helpful answer is : No you can't scale the hatch pattern of a HatchBrush.

It is meant to always look sharp at the pixel level and is not even affected by scaling the Graphics object.

Looking at your question I wonder: Are you sure you are really using a HatchBrush? You get the brush from a function GetBoreholeBrush. If you really have stored indices into the 50 HatchStyle then I guess you really use a HatchBrush.

Now as using a HatchBrush won't work I guess you could use a TextureBrush instead..

You could transform the hatch patterns to larger versions by scaling them up; this is not exactly a simple conversion. The direct approach of drawing the larger by an integer factor and without anti-aliasing is simple and may be good enough.

But you may need to fine-tune them, as this way all pixels, that is both line pixels and background pixels get enlarged and also all diagonals will look jagged.

So you would need to balance the hatch size and the stroke width and recreate all patterns you need from scratch in larger sizes.

Here is an example that illustrates the problems with the simple solution; the first row is the original hatch pattern the others are simple texture brush results, scaled by 1x, 2x and 3x..:

First a function to transform a HatchBrush to a TextureBrush

TextureBrush TBrush(HatchBrush HBrush)
{
    using (Bitmap bmp = new Bitmap(8,8))
    using (Graphics G = Graphics.FromImage(bmp))
    {
        G.FillRectangle(HBrush, 0, 0, 8, 8);
        TextureBrush tb = new TextureBrush(bmp);
        return tb;
    }
}

Note that the hatch pattern is 8x8 pixels.

Now the Paint code used for the above image:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    var hs = (HatchStyle[])Enum.GetValues(typeof(HatchStyle));

    for (int i = 0; i < hs.Length; i++)
        using (HatchBrush hbr = new HatchBrush(hs[i], Color.GreenYellow))
        using (HatchBrush hbr2 = new HatchBrush(hs[i], Color.LightCyan))
        {
            e.Graphics.FillRectangle(hbr, new Rectangle(i * 20, 10,16,60));
            using (TextureBrush tbr = TBrush(hbr2))
            {
                e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 80, 16, 60));
                tbr.ScaleTransform(2, 2);
                e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 150, 16, 60));
                tbr.ResetTransform();
                tbr.ScaleTransform(3,3);
                e.Graphics.FillRectangle(tbr, new Rectangle(i * 20, 220, 16, 60));
            }
        }
}

Note that while the TextureBrush has nice methods to modify the texture, the HatchBrush has nothing like that at all..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!