Private Sub MakeMask(ByVal from_picture As PictureBox, _
ByVal mask_picture As PictureBox, ByVal from_color As _
OLE_COLOR, ByVal to_color As OLE_COLOR, ByVal _
other_color As OLE_COLOR)
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim X As Integer
Dim Y As Integer
Dim r As Byte
Dim g As Byte
Dim b As Byte
Dim to_r As Byte
Dim to_g As Byte
Dim to_b As Byte
Dim other_r As Byte
Dim other_g As Byte
Dim other_b As Byte
' Make RGBTriplet values for the colors.
UnRGB to_color, to_r, to_g, to_b
UnRGB other_color, other_r, other_g, other_b
UnRGB from_color, r, g, b
' Prepare the bitmap description.
from_picture.ScaleMode = vbPixels
With bitmap_info.bmiHeader
.biSize = 40
.biWidth = from_picture.ScaleWidth
' Use negative height to scan top-down.
.biHeight = -from_picture.ScaleHeight
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
31) \ 32) * 4)
pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
* .biBitCount) + 7) \ 8)
.biSizeImage = bytes_per_scanLine * Abs(.biHeight)
End With
' Load the bitmap's data.
ReDim pixels(1 To 4, 1 To from_picture.ScaleWidth, 1 To _
from_picture.ScaleHeight)
GetDIBits from_picture.hdc, from_picture.Image, _
0, from_picture.ScaleHeight, pixels(1, 1, 1), _
bitmap_info, DIB_RGB_COLORS
' Modify the pixels.
For Y = 1 To from_picture.ScaleHeight
For X = 1 To from_picture.ScaleWidth
' See if we need to change this pixel.
If (pixels(pixR, X, Y) = r) And _
(pixels(pixG, X, Y) = g) And _
(pixels(pixB, X, Y) = b) _
Then
pixels(pixR, X, Y) = to_r
pixels(pixG, X, Y) = to_g
pixels(pixB, X, Y) = to_b
Else
pixels(pixR, X, Y) = other_r
pixels(pixG, X, Y) = other_g
pixels(pixB, X, Y) = other_b
End If
Next X
Next Y
' Display the result.
SetDIBits picMask.hdc, picMask.Image, _
0, from_picture.ScaleHeight, pixels(1, 1, 1), _
bitmap_info, DIB_RGB_COLORS
picMask.Picture = picMask.Image
picMask.Visible = True
End Sub
|