
BMP - change color based on form's background color
The easy solution is to change your bmp into a gif file, which supports
transparency.
Failing that, here's some code you can use to change one or more colors in
a bitmap...
You can do this by using GetDIBits() to get what is essentially a binary
copy of the device-independent bitmap into memory, changing the palette,
and using SetDIBits() to blast it back into the picturebox.
You'll want the picturebox.autoredraw = true, and scalemode = 3 (pixel).
This routine is specifically for 16-color bitmaps
Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPel{*filter*}eter As Long
biYPel{*filter*}eter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO_4
bmiHeader As BITMAPINFOHEADER
bmiColors(15) As RGBQUAD
End Type
Private Const PIXEL As Integer = 3
Private Const DIB_RGB_COLORS As Long = 0
Private Declare Function GetDIBits4 Lib "gdi32" Alias "GetDIBits" _
(ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _
ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO_4, _
ByVal wUsage As Long) As Long
Private Declare Function SetDIBits4 Lib "gdi32" Alias "SetDIBits" _
(ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _
ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO_4, _
ByVal wUsage As Long) As Long
'size a buffer for the pixel data
BufferSize = ((pic.ScaleWidth / 2 + 3) And &HFFFC) * pic.ScaleHeight
ReDim SaveBits(0 To BufferSize - 1)
'fill the header info for the save copy
With SaveBitmapInfo_4.bmiHeader
.biSize = 40
.biWidth = pic.ScaleWidth
.biHeight = pic.ScaleHeight
.biPlanes = 1
.biBitCount = 4
.biCompression = 0
.biClrUsed = 0
.biClrImportant = 0
.biSizeImage = BufferSize
End With
nLen = Len(SaveBitmapInfo_4)
'get the bitmap from the picturebox
Retval = GetDIBits4(pic.hdc, pic.Image, 0, _
SaveBitmapInfo_4.bmiHeader.biHeight, SaveBits(0), SaveBitmapInfo_4,
DIB_RGB_COLORS)
'change the palette
SaveBitmapInfo_4.bmiColors(0).rgbRed = 255
SaveBitmapInfo_4.bmiColors(0).rgbGreen = 255
SaveBitmapInfo_4.bmiColors(0).rgbBlue = 0
'blast it back in
Retval = SetDIBits4(pic.hdc, pic.Image.Handle, 0, _
SaveBitmapInfo_4.bmiHeader.biHeight, SaveBits(0), SaveBitmapInfo_4,
DIB_RGB_COLORS)
pic.Refresh
It'll work most predictably if the original image has a standard palette.
Probably, if you want to change a particular color, you'll do best to
search the palette for it (I believe the light/dark grays have different
palette indices in Win95/NT).
Jim Deutch
MS Dev MVP
Quote:
> Hi, I have a bmp that I'm using as a background texture in a picture
> box. It has variations in tone from light to dark, but the overall
> impression of the color is of the same gray used as the default
> background color for a form.
> I'd like to be able to change the tone of the bmp to 'match' the form's
> background if the user has changed the default Windows colors. Maybe get
> the average color (hue?) and darkness of form.BackgroundColor and alter
> my bmp accordingly.
> The bmp could be monochromatic if that's easier to alter than a full
> color bmp would be.