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