HatchBrush class

A brush that fills a shape with a pattern of lines or dots in a foreground colour over a background colour.

A HatchBrush implements IBrush. The pattern is one of the 53 styles of GpHatchStyle, a small tile that repeats across the shape. The foreground colour draws the lines or dots of the tile, and the background colour fills the rest. The class mirrors the GDI+ HatchBrush class.

Creating

Syntax: brush = New HatchBrush( hatchStyle, foreColor, backColor )

Syntax: brush = New HatchBrush( hatchStyle, foreRgb, backRgb )

hatchStyle
required A GpHatchStyle value for the pattern.
foreColor, backColor
required Color values for the lines or dots and for the background. The alpha values are kept.
foreRgb, backRgb
required Long values for the same colours, as &H00RRGGBB. The alpha values are always 255.
Dim user As GdiPlusUser = GdiPlusUser()

Dim red As Color = Color(255, 255, 0, 0)
Dim blue As Color = Color(255, 0, 0, 255)
Dim brush As HatchBrush = New HatchBrush(HatchStyleHorizontal, red, blue)

Dim bmp As Bitmap = Bitmap(8!, 8!)
Dim g As Graphics = Graphics(bmp)
g.FillRectangle(brush, 0, 0, 8, 8)

Dim y As Long, row As String
For y = 0 To 3
    row = row & IIf(bmp.Pixel(3, y).argb = red.argb, "F", "B")
Next
Debug.Print row
' Output:
' FBBB

Clone

Syntax: copy = brush.Clone( )

Returns a new HatchBrush with the same style and colours.

Properties

The properties are read-only: a hatch brush is created with its style and colours, and they do not change.

HatchStyle

Syntax: style = brush.HatchStyle

Returns the GpHatchStyle of the brush.

ForegroundColor

Syntax: color = brush.ForegroundColor

Returns the colour of the lines or dots, as a Color.

BackgroundColor

Syntax: color = brush.BackgroundColor

Returns the background colour, as a Color.

Type

Syntax: type = brush.Type

Returns BrushTypeHatchFill (GpBrushType). The property is read-only.

Example

The example fills two 8-by-8 bitmaps with the cross and the vertical styles and prints the top row of each. F is a pixel of the foreground colour and B one of the background colour.

Dim user As GdiPlusUser = GdiPlusUser()

Dim red As Color = Color(255, 255, 0, 0)
Dim blue As Color = Color(255, 0, 0, 255)
Dim styles(1) As GpHatchStyle
styles(0) = HatchStyleCross
styles(1) = HatchStyleVertical

Dim bmp As Bitmap = Bitmap(8!, 8!)
Dim g As Graphics = Graphics(bmp)
Dim i As Long, x As Long, row As String
For i = 0 To 1
    Dim brush As HatchBrush = New HatchBrush(styles(i), red, blue)
    g.FillRectangle(brush, 0, 0, 8, 8)
    row = ""
    For x = 0 To 7
        row = row & IIf(bmp.Pixel(x, 0).argb = red.argb, "F", "B")
    Next
    Debug.Print row
Next

Dim hatch As HatchBrush = New HatchBrush(HatchStyle50Percent, red, blue)
Debug.Print hatch.HatchStyle & " " & Hex$(hatch.ForegroundColor.argb) & " " & Hex$(hatch.BackgroundColor.argb)
' Output:
' FFFFFFFF
' FBBBBBBB
' 12 FFFF0000 FF0000FF

See Also