|
-
Feb 4th, 2009, 02:58 PM
#1
Thread Starter
Member
[2008] how do i (flood-fill) in vb.net ?
hi.
I am trying to convert a VB-6 program to VB-2008.
the program displays the (us) map in a picture box.
during runtime .. the program paints certain states red .. depending on the user's input.
You can see what the (vb-6) program (and the us map) looks like
from my website.
www (dot) wa0h (dot) com
the (0) in (wa0h) is the number zero.
web searching revealed that lots of programmers wanted to do this
but no solutions were found.
it seems that (gdi+) does (not) have a (ext-flood-fill) function.
since states do not have a regular boundary, i can't use ..fill-region
??? can anyone help me with this simple tasks ???
searching your forum .. i saw similar postings from several years ago,
but no apparent solution.
Thanks for you time.
Jerry Gentry
-
Feb 4th, 2009, 03:08 PM
#2
Re: [2008] how do i (flood-fill) in vb.net ?
try this, its the fastest way of floodfilling a part of a bitmap:
vb Code:
' Flood the area at this point.
Public Sub UnsafeFloodFill(ByVal bm As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal new_color As Color)
' Get the old and new colors' components.
Dim old_r As Byte = bm.GetPixel(x, y).R
Dim old_g As Byte = bm.GetPixel(x, y).G
Dim old_b As Byte = bm.GetPixel(x, y).B
Dim new_r As Byte = new_color.R
Dim new_g As Byte = new_color.G
Dim new_b As Byte = new_color.B
' Start with the original point in the stack.
Dim pts As New Stack(1000)
pts.Push(New Point(x, y))
bm.SetPixel(x, y, new_color)
' Make a BitmapBytesARGB32 object.
Dim bm_bytes As New BitmapBytesARGB32(bm)
' Lock the bitmap.
bm_bytes.LockBitmap()
' While the stack is not empty, process a point.
Do While pts.Count > 0
Dim pt As Point = DirectCast(pts.Pop(), Point)
If pt.X > 0 Then UnsafeCheckPoint(bm_bytes, pts, pt.X - 1, pt.Y, old_r, old_g, old_b, new_r, new_g, new_b)
If pt.Y > 0 Then UnsafeCheckPoint(bm_bytes, pts, pt.X, pt.Y - 1, old_r, old_g, old_b, new_r, new_g, new_b)
If pt.X < bm.Width - 1 Then UnsafeCheckPoint(bm_bytes, pts, pt.X + 1, pt.Y, old_r, old_g, old_b, new_r, new_g, new_b)
If pt.Y < bm.Height - 1 Then UnsafeCheckPoint(bm_bytes, pts, pt.X, pt.Y + 1, old_r, old_g, old_b, new_r, new_g, new_b)
Loop
' Unlock the bitmap.
bm_bytes.UnlockBitmap()
End Sub
vb Code:
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class BitmapBytesARGB32
' Provide public access to the picture's byte data.
Public ImageBytes() As Byte
Public RowSizeBytes As Integer
Public Const PixelSizeBytes As Integer = 4 ' 4 bytes/pixel.
Public Const PixelSizeBits As Integer = PixelSizeBytes * 8 ' 32 bits per pixel.
' A reference to the Bitmap.
Private m_Bitmap As Bitmap
' Save a reference to the bitmap.
Public Sub New(ByVal bm As Bitmap)
m_Bitmap = bm
End Sub
' Bitmap data.
Private m_BitmapData As BitmapData
' Lock the bitmap's data.
Public Sub LockBitmap()
' Lock the bitmap data.
Dim bounds As Rectangle = New Rectangle( _
0, 0, m_Bitmap.Width, m_Bitmap.Height)
m_BitmapData = m_Bitmap.LockBits(bounds, _
Imaging.ImageLockMode.ReadWrite, _
Imaging.PixelFormat.Format32bppArgb)
RowSizeBytes = m_BitmapData.Stride
' Allocate room for the data.
Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
ReDim ImageBytes(total_size)
' Copy the data into the ImageBytes array.
Marshal.Copy(m_BitmapData.Scan0, ImageBytes, _
0, total_size)
End Sub
' Copy the data back into the Bitmap
' and release resources.
Public Sub UnlockBitmap()
' Copy the data back into the bitmap.
Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
Marshal.Copy(ImageBytes, 0, _
m_BitmapData.Scan0, total_size)
' Unlock the bitmap.
m_Bitmap.UnlockBits(m_BitmapData)
' Release resources.
ImageBytes = Nothing
m_BitmapData = Nothing
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 4th, 2009, 03:12 PM
#3
Re: [2008] how do i (flood-fill) in vb.net ?
you need this sub too:
vb Code:
' See if this point should be added to the stack.
Private Sub UnsafeCheckPoint(ByVal bm_bytes As BitmapBytesARGB32, ByVal pts As Stack, ByVal x As Integer, ByVal y As Integer, ByVal old_r As Byte, ByVal old_g As Byte, ByVal old_b As Byte, ByVal new_r As Byte, ByVal new_g As Byte, ByVal new_b As Byte)
Dim pix As Integer = y * bm_bytes.RowSizeBytes + x * 4
Dim b As Byte = bm_bytes.ImageBytes(pix)
Dim g As Byte = bm_bytes.ImageBytes(pix + 1)
Dim r As Byte = bm_bytes.ImageBytes(pix + 2)
If (r = old_r) AndAlso (g = old_g) AndAlso (b = old_b) Then
pts.Push(New Point(x, y))
bm_bytes.ImageBytes(pix) = new_b
bm_bytes.ImageBytes(pix + 1) = new_g
bm_bytes.ImageBytes(pix + 2) = new_r
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 4th, 2009, 05:13 PM
#4
Re: [2008] how do i (flood-fill) in vb.net ?
believe me jerry, i did a lot of research on this one when i was writing a vb.net paint application. the only alternative is to write your app. in vb6.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|