Results 1 to 9 of 9

Thread: Picturebox Transparency???

  1. #1

    Thread Starter
    Hyperactive Member LawnNinja's Avatar
    Join Date
    Aug 2006
    Posts
    446

    Picturebox Transparency???

    I have a checkbox and I want it so when you click it all the perfectly black colours become completly transparent???????
    Can this be done???????

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Picturebox Transparency???

    Depending on what you mean by "Transparent". You can remove the black pixels to look like an empty picturebox. The easiest way being..
    VB Code:
    1. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    2. Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
    3. Private Declare Function OleTranslateColor Lib "OLEPRO32.DLL" _
    4.     (ByVal OLE_COLOR As Long, ByVal HPALETTE As Long, _
    5.     pccolorref As Long) As Long
    6. Private Const CLR_INVALID = -1
    7.  
    8. Private Function TranslateColor(ByVal oClr As OLE_COLOR, _
    9.                         Optional hPal As Long = 0) As Long
    10.     If OleTranslateColor(oClr, hPal, TranslateColor) Then
    11.         TranslateColor = CLR_INVALID
    12.     End If
    13. End Function
    14.  
    15. Private Sub Command1_Click()
    16. Dim x As Long
    17. Dim y As Long
    18. Dim pixelColour As Long
    19.  
    20. For x = 0 To Picture1.ScaleWidth
    21.     For y = 0 To Picture1.ScaleHeight
    22.         pixelColour = GetPixel(Picture1.hdc, x, y)
    23.         If (pixelColour = RGB(0, 0, 0)) Then
    24.             SetPixelV Picture1.hdc, x, y, TranslateColor(vbButtonFace)
    25.         End If
    26.     Next y
    27.     Picture1.Refresh
    28. Next x
    29. End Sub
    Or faster..
    VB Code:
    1. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    2. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    3. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    4. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    5. Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    6.  
    7. Private Type BITMAP
    8.   bmType        As Long
    9.   bmWidth       As Long
    10.   bmHeight      As Long
    11.   bmWidthBytes  As Long
    12.   bmPlanes      As Integer
    13.   bmBitsPixel   As Integer
    14.   bmBits        As Long
    15. End Type
    16.  
    17. Private Type BITMAPINFOHEADER
    18.    biSize           As Long
    19.    biWidth          As Long
    20.    biHeight         As Long
    21.    biPlanes         As Integer
    22.    biBitCount       As Integer
    23.    biCompression    As Long
    24.    biSizeImage      As Long
    25.    biXPelsPerMeter  As Long
    26.    biYPelsPerMeter  As Long
    27.    biClrUsed        As Long
    28.    biClrImportant   As Long
    29. End Type
    30.  
    31. Private Type RGBQUAD
    32.    rgbBlue      As Byte
    33.    rgbGreen     As Byte
    34.    rgbRed       As Byte
    35.    rgbReserved  As Byte
    36. End Type
    37.  
    38. Private Type BITMAPINFO
    39.   bmiHeader As BITMAPINFOHEADER
    40.   bmiColors As RGBQUAD
    41. End Type
    42.  
    43. Private PicInfo         As BITMAP
    44. Private DIBInfo         As BITMAPINFO
    45.  
    46. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    47. Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
    48. Private Declare Function OleTranslateColor Lib "OLEPRO32.DLL" _
    49.     (ByVal OLE_COLOR As Long, ByVal HPALETTE As Long, _
    50.     pccolorref As Long) As Long
    51. Private Const CLR_INVALID = -1
    52.  
    53. Private Function TranslateColor(ByVal oClr As OLE_COLOR, _
    54.                         Optional hPal As Long = 0) As Long
    55.     If OleTranslateColor(oClr, hPal, TranslateColor) Then
    56.         TranslateColor = CLR_INVALID
    57.     End If
    58. End Function
    59.  
    60. ' I know these look yuck. Leave them alone...
    61.  
    62. Private Function GetBlue(ByVal Color As Long) As Byte
    63.     Dim blue As Long
    64.     Dim green As Long
    65.    
    66.     blue = Int(Color / 65536)
    67.     green = Int((Color - (blue * 65536)) / 256)
    68.     GetBlue = Int(Color - (blue * 65536) - (green * 256))
    69. End Function
    70.  
    71. Private Function GetGreen(ByVal Color As Long) As Byte
    72.     Dim blue As Long
    73.    
    74.     blue = Int(Color / 65536)
    75.     GetGreen = Int((Color - (blue * 65536)) / 256)
    76. End Function
    77.  
    78. Private Function GetRed(ByVal Color As Long) As Byte
    79.     GetRed = Int(Color \ 65536)
    80. End Function
    81.  
    82. Private Function RemoveBlackStuff(Pic As Long) As Long
    83. Dim hdcNew          As Long
    84. Dim oldhand         As Long
    85. Dim ret             As Long
    86. Dim DIB_RGB_COLORS  As Long
    87.  
    88. 'On Error GoTo Err:
    89. Call GetObject(Pic, Len(PicInfo), PicInfo)
    90. hdcNew = CreateCompatibleDC(0&)
    91. oldhand = SelectObject(hdcNew, Pic)
    92.  
    93. With DIBInfo.bmiHeader
    94.     .biSize = 40
    95.     .biWidth = PicInfo.bmWidth
    96.     .biHeight = -PicInfo.bmHeight
    97.     .biPlanes = 1
    98.     .biBitCount = 32
    99. End With
    100.  
    101. ReDim ImgData(1 To 4, 1 To PicInfo.bmWidth, 1 To PicInfo.bmHeight) As Byte
    102.  
    103. Dim i As Long, j As Long
    104. Dim r As Byte, g As Byte, b As Byte
    105.  
    106. ret = GetDIBits(hdcNew, Pic, 0, PicInfo.bmHeight, ImgData(1, 1, 1), DIBInfo, DIB_RGB_COLORS)
    107. For i = 1 To PicInfo.bmWidth
    108.     For j = 1 To PicInfo.bmHeight
    109.         r = ImgData(1, i, j)
    110.         g = ImgData(2, i, j)
    111.         b = ImgData(3, i, j)
    112.  
    113.         If (r = 0) Then r = GetRed(TranslateColor(vbButtonFace))
    114.         If (g = 0) Then g = GetGreen(TranslateColor(vbButtonFace))
    115.         If (b = 0) Then b = GetBlue(TranslateColor(vbButtonFace))
    116.        
    117.         ImgData(1, i, j) = r
    118.         ImgData(2, i, j) = g
    119.         ImgData(3, i, j) = b
    120.     Next j
    121. Next i
    122. ret = SetDIBits(hdcNew, Pic, 0, PicInfo.bmHeight, ImgData(1, 1, 1), DIBInfo, DIB_RGB_COLORS)
    123. End Function
    124.  
    125. ' NOTE: Ripped straight from our MiniShop (Photoshop clone) project we discontinued a while back...
    There is also the option of just transparent blitting, which is done like so..
    VB Code:
    1. Private Declare Function CreateCompatibleDC Lib "gdi32" Alias "CreateCompatibleDC" (ByVal hdc As Long) As Long
    2. Private Declare Function CreateBitmap Lib "gdi32" Alias "CreateBitmap" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
    3. Private Declare Function CreateCompatibleBitmap Lib "gdi32" Alias "CreateCompatibleBitmap" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    4. Private Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As Long, ByVal hObject As Long) As Long
    5. Private Declare Function SetBkColor Lib "gdi32" Alias "SetBkColor" (ByVal hdc As Long, ByVal crColor As Long) As Long
    6. Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (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
    7. Private Declare Function DeleteDC Lib "gdi32" Alias "DeleteDC" (ByVal hdc As Long) As Long
    8. Private Declare Function DeleteObject Lib "gdi32" Alias "DeleteObject" (ByVal hObject As Long) As Long
    9.  
    10. Sub TransparentBlt(dsthDC As Long, srchDC As Long, X As Integer, _
    11.                    Y As Integer, Width As Integer, _
    12.                    Height As Integer, TransColor As Long)
    13.     Dim maskDC As Long      'DC for the mask
    14.     Dim tempDC As Long      'DC for temporary data
    15.     Dim hMaskBmp As Long    'Bitmap for mask
    16.     Dim hTempBmp As Long    'Bitmap for temporary data
    17.  
    18.     'First, create some DC's. These are our gateways to associated
    19.     'bitmaps in RAM
    20.     maskDC = CreateCompatibleDC(dsthDC)
    21.     tempDC = CreateCompatibleDC(dsthDC)
    22.  
    23.     'Then, we need the bitmaps. Note that we create a monochrome
    24.     'bitmap here!
    25.     'This is a trick we use for creating a mask fast enough.
    26.     hMaskBmp = CreateBitmap(Width, Height, 1, 1, ByVal 0&)
    27.     hTempBmp = CreateCompatibleBitmap(dsthDC, Width, Height)
    28.  
    29.     'Then we can assign the bitmaps to the DCs
    30.     hMaskBmp = SelectObject(maskDC, hMaskBmp)
    31.     hTempBmp = SelectObject(tempDC, hTempBmp)
    32.  
    33.     'Now we can create a mask. First, we set the background color
    34.     'to the transparent color; then we copy the image into the
    35.     'monochrome bitmap.
    36.     'When we are done, we reset the background color of the
    37.     'original source.
    38.     TransColor = SetBkColor(srchDC, TransColor)
    39.     BitBlt maskDC, 0, 0, Width, Height, srchDC, 0, 0, vbSrcCopy
    40.     TransColor = SetBkColor(srchDC, TransColor)
    41.  
    42.     'The first we do with the mask is to MergePaint it into the
    43.     'destination.
    44.     'This will punch a WHITE hole in the background exactly were
    45.     'we want the graphics to be painted in.
    46.     BitBlt tempDC, 0, 0, Width, Height, maskDC, 0, 0, vbSrcCopy
    47.     BitBlt dsthDC, X, Y, Width, Height, tempDC, 0, 0, vbMergePaint
    48.  
    49.     'Now we delete the transparent part of our source image. To do
    50.     'this, we must invert the mask and MergePaint it into the
    51.     'source image. The transparent area will now appear as WHITE.
    52.     BitBlt maskDC, 0, 0, Width, Height, maskDC, 0, 0, vbNotSrcCopy
    53.     BitBlt tempDC, 0, 0, Width, Height, srchDC, 0, 0, vbSrcCopy
    54.     BitBlt tempDC, 0, 0, Width, Height, maskDC, 0, 0, vbMergePaint
    55.  
    56.     'Both target and source are clean. All we have to do is to AND
    57.     'them together!
    58.     BitBlt dsthDC, X, Y, Width, Height, tempDC, 0, 0, vbSrcAnd
    59.  
    60.     'Now all we have to do is to clean up after us and free system
    61.     'resources..
    62.     DeleteObject (hMaskBmp)
    63.     DeleteObject (hTempBmp)
    64.     DeleteDC (maskDC)
    65.     DeleteDC (tempDC)
    66. End Sub
    ...

    Either way, Pictureboxes don't have true transparency, only imageboxes do. To simulate it with ImageBoxes, you could make it switch between images, one with transparency, and one without.

    Imageboxes are yucky though, so stick with the PictureBox method.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Hyperactive Member LawnNinja's Avatar
    Join Date
    Aug 2006
    Posts
    446

    Re: Picturebox Transparency???

    I tried your top code and it gave me this,
    Compile error: User-defined type not defined

    and then Highlights this,
    VB Code:
    1. Private Function TranslateColor(ByVal oClr As OLE_COLOR, _
    2.                         Optional hPal As Long = 0) As Long

    On your second code it says the same thing?

    On the final code it doesn't say anything. But Nothing happens.
    And when I add in the code from the top one,
    VB Code:
    1. Dim x As Long
    2. Dim y As Long
    3. Dim pixelColour As Long
    4.  
    5. For x = 0 To Picture1.ScaleWidth
    6.     For y = 0 To Picture1.ScaleHeight
    7.         pixelColour = GetPixel(Picture1.hdc, x, y)
    8.         If (pixelColour = RGB(0, 0, 0)) Then
    9.             SetPixelV Picture1.hdc, x, y, TranslateColor(vbButtonFace)
    10.         End If
    11.     Next y
    12.     Picture1.Refresh
    13. Next x
    It says,
    Compile error:
    Sub or Fuction not defined and then highlights
    VB Code:
    1. GetPixel
    ???????????

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Picturebox Transparency???

    Replace this:
    VB Code:
    1. ByVal oClr As OLE_COLOR
    with this..
    VB Code:
    1. ByVal oClr As Long
    And the definition of getpixel is there, add it to your module.

  5. #5
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Picturebox Transparency???

    Quote Originally Posted by LawnNinja
    I tried your top code and it gave me this,
    Compile error: User-defined type not defined

    and then Highlights this,
    VB Code:
    1. Private Function TranslateColor(ByVal oClr As OLE_COLOR, _
    2.                         Optional hPal As Long = 0) As Long
    What version of Visual Basic are you using? It should work fine..
    Quote Originally Posted by LawnNinja
    On your second code it says the same thing?
    Again...this must be the version of Visual Basic you are using. You're not using the VB Editor within a Microsoft Office application are you?
    Quote Originally Posted by LawnNinja
    On the final code it doesn't say anything. But Nothing happens.
    And when I add in the code from the top one,
    VB Code:
    1. Dim x As Long
    2. Dim y As Long
    3. Dim pixelColour As Long
    4.  
    5. For x = 0 To Picture1.ScaleWidth
    6.     For y = 0 To Picture1.ScaleHeight
    7.         pixelColour = GetPixel(Picture1.hdc, x, y)
    8.         If (pixelColour = RGB(0, 0, 0)) Then
    9.             SetPixelV Picture1.hdc, x, y, TranslateColor(vbButtonFace)
    10.         End If
    11.     Next y
    12.     Picture1.Refresh
    13. Next x
    It says,
    Compile error:
    Sub or Fuction not defined and then highlights
    VB Code:
    1. GetPixel
    ???????????
    Yes thats because "GetPixel" and "SetPixelV" are both API's. To stop that error, place these at the top of that code..
    VB Code:
    1. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    2. Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
    If you copy the code exactly (and not just pick what looks good), it will work. Make sure your picturebox has its ScaleMode property set to Pixels (PictureBox.ScaleMode = vbPixels), and that its AutoRedraw property is set to true. Also, obviously, make sure you have the correct object names...and if in my code you see "Private Sub Command1_Click()", you should probably add a commandbutton aswell...since thats what is used in my example.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  6. #6

    Thread Starter
    Hyperactive Member LawnNinja's Avatar
    Join Date
    Aug 2006
    Posts
    446

    Re: Picturebox Transparency???

    I have Visual Basic 6.0 Enterprise Edition,
    I added in those 2 api's and know it is saying,
    Compile error:
    Sub or Function not defined
    Do I need more api's??????
    -------------------------------------------------------------------------------
    I tried what you said jcis, and I apperas to work until you press my command button then a line apears and slowly moves across my picture box, And for some reason it doesn't show up in printscreen???????????????????????????????????????????????/

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Picturebox Transparency???

    Quote Originally Posted by LawnNinja
    I have Visual Basic 6.0 Enterprise Edition,
    I added in those 2 api's and know it is saying,
    Compile error:
    Sub or Function not defined
    Do I need more api's??????
    Yes. Depending on what peice of code you used, you need the different API's. Read over what I said above, carefully, and you should get it.
    Quote Originally Posted by LawnNinja
    -------------------------------------------------------------------------------
    I tried what you said jcis, and I apperas to work until you press my command button then a line apears and slowly moves across my picture box, And for some reason it doesn't show up in printscreen???????????????????????????????????????????????/
    That means that one is working properly. It should change all the black to the default grey seen on forms and the back of pictureboxes...thats as good as you'll get, unless you Blit directly to the Forms Device Context... The first two pieces of code I gave you above, just change the black to grey. The last one, can be used to draw the image again, without the colour you want (black). If you want it to actually look transparent though, that is when you make it go to the forms DC.

    If you still don't understand, tell me, I can type you up a quick program if you want, and attach it here for you to look over.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    Hyperactive Member LawnNinja's Avatar
    Join Date
    Aug 2006
    Posts
    446

    Re: Picturebox Transparency???

    That would be appriciated.

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Picturebox Transparency???

    Ok here it is. It utilizes all the code I've explained above. I tried to make it a bit easier to read for you, by moving the two main big pieces of code into their own modules. The smaller one is in the main forms code.

    Usage:

    Button 1 will use Method 1 - GetPixel/SetPixel (Slow)
    Button 2 will use Method 2 - GetDIBits/SetDIBits (Medium/Fast...from my calculations)
    Button 3 will use Method 3 - TransparentBlt/Masks (Fast)

    Button 4 will blit the image onto the form, and change the backcolor of the form each time, just to show you that the image does appear to be there, with the black bits transparent.

    Hope this helps...if not, transparency might be a bit advanced for you :/

    chem
    Attached Files Attached Files

    Visual Studio 6, Visual Studio.NET 2005, MASM

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