Articles in this section
Category / Section

How to apply hatch style to a node with a custom distance and angle in Winforms Diagram?

3 mins read

In our WinForms Diagram control, you can apply hatch texture to a node’s style with a custom distance and angle using the ApplyHatchStyle method. This method allows you to specify the distance and angle as parameters. In the code snippet below, a bitmap is created for the hatch texture with the specified distance and angle, and then it is assigned to the shape’s FillStyle.Texture. We have provided the code snippet to achieve this.

Code Snippet:

var rectangle = new Syncfusion.Windows.Forms.Diagram.Rectangle(300, 300, 200, 200);

// Set the FillStyle.Type to Texture
rectangle.FillStyle.Type = FillStyleType.Texture;
rectangle.FillStyle.TextureWrapMode = WrapMode.Clamp;
rectangle.ApplyHatchStyle(hatchDistance, hatchAngle, new Pen(Color.Red, 2));
          
this.diagram1.Model.AppendChild(rectangle);


public static void ApplyHatchStyle(this FilledPath node, int distance, float angle, Pen pen)
{
   //Create a Bitmap with the size similar to shape's size
   var width = node.Size.Width;
   var height = node.Size.Height;
   var bitmap = new Bitmap((int)width, (int)height);

   //Set the Graphics object to the Bitmap
   var graphics = Graphics.FromImage(bitmap);
          
   var maxDiagonalSize = Math.Max(width, height) * 1.4142F;
   var radianAngle = (angle * (Math.PI)) / 180;
   var xLength = (float)(Math.Cos(radianAngle) * maxDiagonalSize);
   var yLength = (float)(Math.Sin(radianAngle) * maxDiagonalSize);
   var xDistance = (float)(Math.Cos(radianAngle) * distance * 2) ;
   var yDistance = (float)(Math.Sin(radianAngle) * distance * 2);

   // Draw the line on the Graphics object for the specified distance and angle
   var i = 0f;
   var j = yDistance;
   while (i < width || j < height)
   {
       if (i < width)
       {
           graphics.DrawLine(pen, i, 0, i + xLength, yLength );
           i += xDistance;
       }

       if (j < height)
       {
           graphics.DrawLine(pen, 0, j, xLength, j + yLength);
           j += yDistance;
       }
   }

   // Set the Bitmap to the FillStyle.Texture
   node.FillStyle.Texture = bitmap;
} 

image.png

Conclusion:

I hope you enjoyed learning about how to create a hatch with a custom distance and angle in Winforms Diagram

You can refer to our WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation. You can also explore our WinForms Diagram example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied