How can I make part of my bitmap transparent so that when someone changes the system colors the background of my bitmap is not the wrong color.
Printable View
How can I make part of my bitmap transparent so that when someone changes the system colors the background of my bitmap is not the wrong color.
How about using the Refresh method to fix the color?
Code:Picture1.Refresh
I dont have a transparent bitmap. I would like to know if there is a way to create one.
There are a lot of methods to make an image transparent. In VB6, the most easiest way to do this, is to put an ImageBox control on the form, which contains a GIF file. Since you can create transparent GIF files (like in Paint Shop Pro) it would be very easy to accomplish this.
VB6 does the transparency automatically. There could be any flickering when the form activates if you are use big transparent 256 colour pictures.
Other simple methods that don't require any code, are i.e. using a ImageList control. This OCX lets you choose the transparent colour (the MaskColor property)
Use the BitBlt API.
How do I use the BitBlt API?
Add 2 PictureBoxes. Call then Picture1 and Picture2. Load your picture (make sure the background is white) in Picture1.
If the colour turns out a little "dark", then you must create a Mask for the picture and draw it on before you draw your original picture.Code:Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Sub Command1_Click()
Me.ScaleMode = vbPixels
BitBlt Picture2.hDC, 0, 0, Picture1.Width, Picture1.Height, Picture1.hDC, 0, 0, vbSrcAnd
End Sub
That's right. Now, before you ask how to Blt with a mask, I'll tell you. First, make a mask of the image. Make sure that the source image has a black backround. Pure black. Next, you have to make a mask of the picture. To make a mask, simply take the source picture, make it's backround white. Whatever Is not going to be transparent, must be black. THE SOURCE AND MASK IMAGES MUST BE THE SAME SIZE. Now that you have the source with the black backround, and the Mask, black with a white backround, it's time to make 2 picture boxes. One named picSprite(load the source image here), one named picMask(Load the mask in here). Add a command button(caption="Blt", name="cmdBlt"). Now, Add this code.
Now what this does is it takes the source and the mask and then it makes the backround of the source image transparent. It then draws this transparent image on the top left corner of your form. I hope this works for what you need it for.Code:Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Sub cmdBlt_Click()
Me.Cls
With picSprite
BitBlt Me.hdc, 0, 0, .ScaleWidth, .ScaleHeight, picMask.hdc, 0, 0, vbSrcAnd
BitBlt Me.hdc, 0, 0, .ScaleWidth, .ScaleHeight, picSprite.hdc, 0, 0, vbSrcPaint
End With
me.Refresh
End Sub
Or if you don't want to use a mask, you can use TransparentBlt instead. Also, TransparentBlt will let you use any colour for transparency whereas BitBlt is limited to only black and white.
Code:Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
Private Sub Command1_Click()
Me.ScaleMode = vbPixels
TransparentBlt Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, vbWhite
End Sub
Please be aware that TransparentBlt is only supported on Win98 and Win2000. Win95 and WinNT lacks this function.
Yes, this API is quite new so the API Text Viewer does not have it...But the PlatForm SDK does.
So does MSDN library.Quote:
Originally posted by Megatron
Yes, this API is quite new so the API Text Viewer does not have it...But the PlatForm SDK does.
TransparentBlt is stupid. I used it in a GUI app and found out that each call to this routine uses a lot of system resources - which are irreversible!!! So I had to write my own routine with For..Next iterations and GetPixel() and SetPixel()...
PSDK is part of MSDN.Quote:
Originally posted by Joacim Andersson
So does MSDN library.Quote:
Originally posted by Megatron
Yes, this API is quite new so the API Text Viewer does not have it...But the PlatForm SDK does.