CType to Long for API FloodFill

Invictus

Member
Joined
Apr 2, 2004
Messages
5
I want to use the API function FloodFill, but I am unable to convert the device context from an IntPtr to a Long and the Color type to a long. The resulting error messages are commented below the offending code. Any suggestions? :confused:

Code:
Private Sub FloodFill(ByVal X, ByVal Y)
        Dim grPictureBox As Graphics = Me.picMapArea.CreateGraphics
        Dim ipDC As IntPtr = grPictureBox.GetHdc

        Dim lDC = CType(ipDC, Long) 
        Value of type System.IntPtr cannot be converted to Long.

        Dim lColor = CType(Color.Black, Long)
        Value of type System.Drawing.Color cannot be converted to Long.

        FloodFill(lDC, X, Y, lColor)
        grPictureBox.ReleaseHdc(ipDC)
End Sub
 
you shouldnt really use Longs in vb.net ( it doesnt like them much ) , try using the vb.net version of the Api FloodFill putting IntPtr as the hdc , Color as crColor etc... in the Api , eg:
Code:
Private Declare Function FloodFill Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal crColor As Color) As Int32

if you want to convert an IntPtr to a Long , you can do this ...
Code:
Dim ipDC As IntPtr = grPictureBox.GetHdc
Dim lng As Int64 = Convert.ToInt64(ipDc.ToInt32) /// int64 is Long in .net
 
So heres what I finally came up with for anyone else out there who stumbled on the lack of FloodFill in GDI+. There may be a more elegant solution, so feel free to comment. This code fills an area surrounded by a black border by combining new VB.NET and old API code:

Code:
    Public Declare Function FloodFill Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal crColor As Int64) As Int32

    Public Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hObject As Int64) As Int32

    Public Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Int64) As Int32

    Public Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Object) As Int32

    Private Sub FloodFill(ByVal FillColor As Color, ByVal X As Integer, ByVal Y As Integer)

        Dim grPictureBox As Graphics = Me.picMapArea.CreateGraphics
        Dim hdc As IntPtr = grPictureBox.GetHdc
        Dim BlackBorder As Int64 = Color.Black.R & Color.Black.G & Color.Black.B
        Dim lFillColor As Int64 = FillColor.R & FillColor.G & FillColor.B

        Dim brFill = CreateSolidBrush(lFillColor)
        SelectObject(hdc, brFill)
        FloodFill(hdc, X, Y, BlackBorder)
        grPictureBox.ReleaseHdc(hdc)
        DeleteObject(brFill)

    End Sub
 
Correction:
The RGB has to be converted to hexadecimal form.

Code:
Public Sub FloodFill(ByVal grDest As Graphics, ByVal FillColor As Color, ByVal X As Integer, ByVal Y As Integer)
        Dim intColor As Integer
        Dim hdc As IntPtr = grDest.GetHdc
        Dim BlackBorder As Integer = "&H" & Hex(Color.Black.R) & Hex(Color.Black.G) & Hex(Color.Black.B)
        Dim lFillColor As Integer = "&H" & Hex(FillColor.R) & Hex(FillColor.G) & Hex(FillColor.B)
        Dim brFill = CreateSolidBrush(lFillColor)
        SelectObject(hdc, brFill)
        FloodFill(hdc, X, Y, BlackBorder)
        grDest.ReleaseHdc(hdc)
        DeleteObject(brFill)
    End Sub

Unfortunately, this only seems to work on visible Graphics objects. If you try to use it on a Graphics object in memory, the FillColor fills the whole bitmap. :(
 
Yet another correction. Change this:

Code:
Dim lFillColor As Integer = "&H" & Hex(FillColor.R) & Hex(FillColor.G) & Hex(FillColor.B)

to this:

Code:
Dim lFillColor As Integer = FillColor.R + FillColor.G * 256 + FillColor.B * 256 ^ 2

Sorry for the trial and error in this thread.
 
have you not tried the ColorTranslator class then? , eg:
Code:
    /// specify Integer / Int32 for crColor in the Api 
    Public Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Int32) As Int32

    Private Sub FloodFill(ByVal FillColor As Color, ByVal X As Integer, ByVal Y As Integer)
        /// get the integer value of your color using the ColorTranslator which is part of .NET
        Dim lFillColor As Int32 = ColorTranslator.ToWin32(FillColor)
        /// rest of your code

    End Sub
 
Back
Top