Results 1 to 14 of 14

Thread: Transparent Bitmaps

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Adelaide - Australia
    Posts
    150
    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.

  2. #2
    Guest
    How about using the Refresh method to fix the color?

    Code:
    Picture1.Refresh

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Adelaide - Australia
    Posts
    150

    I

    I dont have a transparent bitmap. I would like to know if there is a way to create one.

  4. #4
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    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)

  5. #5
    Guest
    Use the BitBlt API.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Adelaide - Australia
    Posts
    150
    How do I use the BitBlt API?

  7. #7
    Guest
    Add 2 PictureBoxes. Call then Picture1 and Picture2. Load your picture (make sure the background is white) in Picture1.
    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
    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.

  8. #8
    Hyperactive Member Warmaster199's Avatar
    Join Date
    Aug 2000
    Location
    Canada
    Posts
    306

    That's right

    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.
    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
    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.
    Designer/Programmer of the Comtech Operating System(CTOS)

  9. #9
    Guest
    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

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Please be aware that TransparentBlt is only supported on Win98 and Win2000. Win95 and WinNT lacks this function.

  11. #11
    Guest
    Yes, this API is quite new so the API Text Viewer does not have it...But the PlatForm SDK does.

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by Megatron
    Yes, this API is quite new so the API Text Viewer does not have it...But the PlatForm SDK does.
    So does MSDN library.

  13. #13
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553

    Lightbulb

    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()...

  14. #14
    Guest
    Originally posted by Joacim Andersson
    Originally posted by Megatron
    Yes, this API is quite new so the API Text Viewer does not have it...But the PlatForm SDK does.
    So does MSDN library.
    PSDK is part of MSDN.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width