如何放大矩形绘制的图案填充样式? [英] How can I scale up the hatchstyle drawn in a rectangle?

查看:66
本文介绍了如何放大矩形绘制的图案填充样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.Net绘图绘制图表.它本质上是堆积的条形图.我的问题是,我想减少图案填充样式中的线条数量,以便以某种方式将其放大以使其更清晰.我环顾四周,但没有发现任何对我有帮助的东西.

我先绘制一个矩形,然后使用阴影刷填充它,但是由于图像大小,阴影填充变得不太清晰.谢谢您的任何建议.

hatchStyles和笔刷类型存储在db中,我使用辅助函数将其返回.因此,我绘制了矩形,并在获得画笔后填充了矩形.本质上,如果可以的话,我想扩大填充图案.

  g.DrawRectangle(gridpen,startX,startY,BOREHOLE_RECT_WIDTH,layerRectHeight);画笔= GetBoreholeBrush(l.SoilTypeMatrixLevel1Id.PrimaryBrushType,l.SoilTypeMatrixLevel1Id.PrimaryFillStyle,l.SoilTypeMatrixLevel1Id.PrimaryColour);g.FillRectangle(brush,startX,startY,BOREHOLE_RECT_WIDTH,layerRectHeight); 

和getBrush函数;画笔的类型,填充样式和颜色存储在数据库中,并用于创建返回的画笔:

 //==================================专用Brush GetBoreholeBrush(字符串BrushType,字符串HatchStyle,字符串颜色)//=================================={//决定选择哪种笔刷类型.毛刷如果(BrushType.ToLower()== BrushTypes.HatchBrush.ToString().ToLower()){画笔=新的HatchBrush(GetHatchStyle(HatchStyle),Color.Black,ColorTranslator.FromHtml(Colour));}否则(BrushType.ToLower()== BrushTypes.SolidBrush.ToString().ToLower()){画笔=新的HatchBrush(GetHatchStyle(HatchStyle),Color.Black,ColorTranslator.FromHtml(Colour));}否则((BrushType.ToLower()== BrushTypes.TextureBrush.ToString().ToLower()){画笔=新的HatchBrush(GetHatchStyle(HatchStyle),Color.Black,ColorTranslator.FromHtml(Colour));}别的{画笔=新的HatchBrush(GetHatchStyle(HatchStyle),Color.Black,ColorTranslator.FromHtml(Colour));}回程刷} 

返回填充样式的函数:

 //==================================私人HatchStyle GetHatchStyle(字串FillStyle)//=================================={//遍历每个孵化样式并返回正确的样式.foreach(Enum.GetValues(typeof(HatchStyle))中的HatchStyle样式){如果(style.ToString().ToLower()== FillStyle.ToLower()){回归风格}}返回HatchStyle.Vertical;} 

如您在图像中看到的那样,阴影样式不清楚.

解决方案

最直接但可能不是很有帮助的答案是:不,您不能缩放 HatchBrush 的图案填充样式.

它应该始终在像素级别看起来清晰,甚至不受缩放 Graphics 对象的影响.

看着您的问题,我想知道:您确定您确实在使用 HatchBrush 吗?您可以从功能 GetBoreholeBrush 获得画笔.如果您确实已将索引存储到50个

第一个将 HatchBrush 转换为 TextureBrush

的函数

  TextureBrush TBrush(HatchBrush HBrush){使用(位图bmp =新位图(8,8))使用(Graphics G = Graphics.FromImage(bmp)){G.FillRectangle(HBrush,0,0,8,8);TextureBrush tb =新的TextureBrush(bmp);返回tb;}} 

请注意,填充图案为 8x8 像素.

现在用于上一张图片的 Paint 代码:

  private void panel1_Paint(对象发送者,PaintEventArgs e){var hs =(HatchStyle [])Enum.GetValues(typeof(HatchStyle));;对于(int i = 0; i< hs.Length; i ++)使用(HatchBrush hbr = new HatchBrush(hs [i],Color.GreenYellow))使用(HatchBrush hbr2 = new HatchBrush(hs [i],Color.LightCyan)){e.Graphics.FillRectangle(hbr,new Rectangle(i * 20,10,16,60));使用(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));}}} 

请注意,虽然 TextureBrush 具有修改纹理的好方法,但 HatchBrush 却根本没有这样的功能..

I'm using .Net drawing to draw a diagram. It is essentially a stacked bar chart. The issue I have is that I want to reduce the amount of lines in the hatch style so in a way scale it up to make it clearer. I've looked around but didn't come across anything that could help me.

I draw a rectangle and then use a hatchbrush to fill it but due to image size the hatchfill becomes less clearer. Thank you for any suggestions.

The hatchStyles and brush types are stored in the db and I use a helper function to return them. So I draw the rectangle and after getting the brush I fill the rectangle. Essentially I want to scale up the hatch fill if that can be done.

 g.DrawRectangle(gridpen, startX, startY, BOREHOLE_RECT_WIDTH, layerRectHeight);

 brush = GetBoreholeBrush(l.SoilTypeMatrixLevel1Id.PrimaryBrushType,
                          l.SoilTypeMatrixLevel1Id.PrimaryFillStyle, 
                          l.SoilTypeMatrixLevel1Id.PrimaryColour);

 g.FillRectangle(brush, startX, startY, BOREHOLE_RECT_WIDTH, layerRectHeight);

And the getBrush function; the brush type, hatch style and colour are stored in the db and used to create the returned brush:

    //===================================
    private Brush GetBoreholeBrush(string BrushType, string HatchStyle, string Colour)
    //===================================
    {
        //Decide on what brush type has been chosen.
        Brush brush;
        if (BrushType.ToLower() == BrushTypes.HatchBrush.ToString().ToLower())
        {
            brush = new HatchBrush(GetHatchStyle(HatchStyle), 
                                   Color.Black, ColorTranslator.FromHtml(Colour));
        }
        else if (BrushType.ToLower() == BrushTypes.SolidBrush.ToString().ToLower())
        {
            brush = new HatchBrush(GetHatchStyle(HatchStyle), 
                                   Color.Black, ColorTranslator.FromHtml(Colour));
        }
        else if (BrushType.ToLower() == BrushTypes.TextureBrush.ToString().ToLower())
        {
            brush = new HatchBrush(GetHatchStyle(HatchStyle), 
                                   Color.Black, ColorTranslator.FromHtml(Colour));
        }
        else
        {
            brush = new HatchBrush(GetHatchStyle(HatchStyle), 
                                   Color.Black, ColorTranslator.FromHtml(Colour));
        }

        return brush;
    }

Function to return the hatch style:

   //===================================
    private HatchStyle GetHatchStyle(string FillStyle)
    //===================================
    {
        //Loop through each hatch tyle and return the correct one.
        foreach (HatchStyle style in Enum.GetValues(typeof(HatchStyle)))
        {
            if (style.ToString().ToLower() == FillStyle.ToLower())
            {
                return style;
            }
        }
        return HatchStyle.Vertical;
    }

As you can see in the image below the hatch style is not clear.

解决方案

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..

这篇关于如何放大矩形绘制的图案填充样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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