How to increase the scale of hatching in a rectangle?

I am using .Net drawing to draw a chart. This is essentially a complex histogram. The problem is that I want to reduce the number of lines in the hatch style so that I can scale it to make it more clear. I looked around, but did not come across anything that could help me.

I draw a rectangle and then use the hatch brush to fill it, but because of the size of the image, the hatch filter becomes less clear. Thank you for any suggestions.

The hatchStyles and brush types are stored in db, and I use a helper function to return them. So I draw a rectangle, and after getting the brush I fill the rectangle. Essentially, I want to increase the fill of the fill, if it 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; brush type, hatch style and color are stored in db and are 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; } 

Hatch Style Return Function:

  //=================================== 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 hatching style is not clear.

enter image description here

+5
source share
1 answer

The most direct, but probably not very useful answer: No, you cannot scale the HatchBrush hatch HatchBrush .

It should always look sharp at the pixel level and does not even depend on the scaling of the Graphics object.

Looking at your question, I wonder: are you sure you really use HatchBrush ? You get a brush from the GetBoreholeBrush function. If you really saved indexes in 50 HatchStyle , then I think you really use HatchBrush .

Now that using HatchBrush not working, I think you could use TextureBrush instead.

You can convert hatch patterns to larger versions by scaling them; this is not a very simple conversion. A direct approach when drawing a larger integer factor and without smoothing is simple and can be quite good.

But you may need to fine-tune them, since in this case all the pixels, which are both line pixels and background pixels , will increase, and all the diagonals will look jagged,

Thus, you will need to balance the size of the hatch and the width of the stroke and recreate all the templates that you need from scratch, in large sizes.

Here is an example illustrating problems with a simple solution; the first line is the original hatching pattern, the rest are simple texture brush results, scalable 1x, 2x and 3x ..:

enter image description here

First you need to convert a HatchBrush to 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 although TextureBrush has nice methods for changing textures, HatchBrush has nothing of the kind.

+7
source

Source: https://habr.com/ru/post/1236899/


All Articles