How to fill an irregular
area with a pattern brush in VB Net
The Vb Net graphics class
provides many methods for drawing shapes and filling area, The Vb Net
currently has no support any method to fill an irregular area with solid
brush,hatch brush or pattern brush. If you want to fill an irregular
area with some brush, the easiest and fastest way is use Window Api
ExtFloodFill function. In this article you will learn
(1)
how to use ExtFloodFill function
(2)
how to convert VB Net Bitmap class to Api int32 bitmap
(3)
how to use VB Net getHdc function
To use ExtFloodFill function,
you shoud Declareit first.
[Private/㎝Public]
Declare Function ExtFloodFill Lib "gdi32" Alias "ExtFloodFill" (ByVal
hdc As Int32, ByVal x As Int32, ByVal y As Int32, ByVal crColor As
Int32, ByVal wFillType As Int32) As Int32
hdc
A handle of a device context to
perform the flood fill on.
x
The x-coordinate of the point to
begin the flood fill at.
y
The y-coordinate of the point to
begin the flood fill at.
crColor
The RGB value of the color
determining the extent of the flood fill operation. Its exact
interpretation depends on the flag passed as wFillType .
wFillType
One of the following flags
specifying how to determine the boundary of the flood fill operation:
FLOODFILLBORDER
Fill from the beginning point in
all directions until a boundary of color crColor is reached. The flood
fill will cover over any colors within the region which do not have the
color of crColor.
FLOODFILLSURFACE
Fill from the beginning point in
all directions as long as the fill-in color crColor is encountered. The
boundary of the flood fill is made up of any color which is not
identical to crColor.
The ExtFloodFill function is
similar to FloodFill except it takes an extra parameter that determines
the fill progresses.If wFillType set to Flloodfillborder(0),it fill stop
as long as when it finds a pixel with same color crColor, If you set
wFillType equal to Flloodfillsurface(1),the fill continues until the
function finds the pixel have a different color. Hereafter is the key
snippet code of the program.
Private
Sub FloodFillAPI(ByRef
brushBmp As Bitmap,
ByVal canvas_beFill
As PictureBox,
ByVal mFillType As Int16,
ByVal colorFill
As Int32, ByVal Pt
As Point)
If m_FillType = 2
Then
Dim mbmpGetHbitmap
As Int32
Dim mBrush
As Int32
If mFillType = 2
And brushBmp Is
Nothing Then
Exit Sub
'convert VB net Bitmap class bitmap to api bitmap
mbmpGetHbitmap = brushBmp.GetHbitmap
If mFillType = 2
Then mBrush =
CreatePatternBrush(mbmpGetHbitmap)
Dim picHdc
As Int32 =
canvas_beFill.CreateGraphics.GetHdc()
Dim hmm
As Int32 = SelectObject(picHdc, mBrush)
ExtFloodFill(picHdc, Pt.X, Pt.Y, GetPixel(picHdc, Pt.X, Pt.Y),
1)
'clearing memory
DeleteDC(picHdc)
DeleteObject(mBrush)
DeleteObject(hmm)
mbmpGetHbitmap = Nothing
hmm = Nothing
Else
Dim mBrush
As Int32
Dim picHdc
As Int32
Dim hmm
As Int32
If mFillType = 0
Then mBrush = CreateSolidBrush(colorFill)
If mFillType = 1
Then mBrush =
CreateHatchBrush(m_HatchStyle, colorFill)
picHdc = canvas_beFill.CreateGraphics.GetHdc()
hmm = SelectObject(picHdc, mBrush)
Dim ret
As Int32 = ExtFloodFill(picHdc, Pt.X,
Pt.Y, GetPixel(picHdc, Pt.X, Pt.Y), 1)
DeleteDC(picHdc)
DeleteObject(mBrush)
DeleteObject(hmm)
hmm = Nothing
End If
End Sub

|