Results 1 to 13 of 13

Thread: [RESOLVED] Make Picturebox color Transparent...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Resolved [RESOLVED] Make Picturebox color Transparent...

    I have a .gif with transparent edges in my PictureBox. The PictureBox background color is Magenta. I would like only the Magenta color to be transparent. Furthermore, I am able to drag my picture box around the Form, so I would like the PictureBox to keep it's transparency, showing the controls I have on my Form behind it as I drag. I do not want to use external user controls or .ocx's.

    As you can see here, my PictureBox on the bottom right with the transparent .gif is loaded with it's background Magenta. I would like to be able to see the command button instead of the Magenta part of the PictureBox.
    Name:  vbforums.jpg
Views: 2278
Size:  3.6 KB

    Thank you.

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Make Picturebox color Transparent...

    LaVolpe: I am using the PictureBox because I can drag it around the form without flicker.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Make Picturebox color Transparent...

    You saw my post before I deleted it. I assume it has other controls/stuff besides just the GIF. There are other ways to drag images around without flicker.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Make Picturebox color Transparent...

    The Trick: Your program is performing translucency, not transparency-by-color. I'm not understanding how it coincides with making only the background color of a movable PictureBox transparent.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Make Picturebox color Transparent...

    LaVolpe: I understand, but using an Image would not allow it to be on top of other controls, such as a Command Button. Also, if you are thinking of BitBlt, I have done that on other projects, but not incorporating a dragging mechanic.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Make Picturebox color Transparent...

    Here is a link to a "bitmap to region" class I designed ages ago. It is pretty much plug & play

    And if just wanting the code to dump to a module, it's here
    Last edited by LaVolpe; Mar 5th, 2015 at 04:22 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Make Picturebox color Transparent...

    Lavolpe: Thank you, but without an example to go along with your class/module, it is difficult to create. Also, would that method keep it's transparency when the PictureBox is dragged?

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Make Picturebox color Transparent...

    Quote Originally Posted by Conroy Vanderbluff View Post
    Lavolpe: Thank you, but without an example to go along with your class/module, it is difficult to create. Also, would that method keep it's transparency when the PictureBox is dragged?
    Example: Change names as needed in sample code
    1. Add the class to your project, assume it is named: clsRegionFromBitmap
    2. The picturebox you are messing with is named: Picture1
    3. The image to use for shaping is Picture1's .Picture object else provide the handle to the image
    Code:
    Dim cBmpToRgn As New clsRegionFromBitmap
    Call cBmpToRgn.RegionFromBitmap(Picture1.Picture.Handle, Picture1.hWnd)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Make Picturebox color Transparent...

    Got it.

    Set AutoRedraw of PictureBox to TRUE.

    Create a new module and insert the following code:
    Code:
    Private Const RGN_OR As Long = 2
    
    Private Declare Function SetWindowRgn Lib "User32" _
        (ByVal hWnd As Long, _
         ByVal hRgn As Long, _
         ByVal bRedraw As Boolean) _
        As Long
        
    Private Declare Function CreateRectRgn Lib "gdi32" _
        (ByVal X1 As Long, _
         ByVal Y1 As Long, _
         ByVal X2 As Long, _
         ByVal Y2 As Long) _
        As Long
        
    Private Declare Function CombineRgn Lib "gdi32" _
        (ByVal hDestRgn As Long, _
         ByVal hSrcRgn1 As Long, _
         ByVal hSrcRgn2 As Long, _
         ByVal nCombineMode As Long) _
        As Long
      
    Public Declare Function GetPixel Lib "gdi32" _
        (ByVal hdc As Long, _
         ByVal X As Long, _
         ByVal Y As Long) _
        As Long
        
    Private Declare Function DeleteObject Lib "gdi32" _
        (ByVal hObject As Long) _
        As Long
    
    Public Function RegionFromImage(fRgnForm As Object, picBackground As PictureBox, ByVal lBackColor As Long) As Long
                                   
        Dim hRegion         As Long
        Dim lpicHeight      As Long
        Dim lpicWidth       As Long
        Dim lRow            As Long
        Dim lCol            As Long
        Dim lStart          As Long
        Dim hTempRegion     As Long
        Dim lResult         As Long
        
        hRegion = CreateRectRgn(0, 0, 0, 0)
        
        With picBackground
            lpicHeight = .Height / Screen.TwipsPerPixelY
            lpicWidth = .Width / Screen.TwipsPerPixelX
            
            For lRow = 0 To lpicHeight - 1
                lCol = 0
                
                Do While (lCol < lpicWidth)
                    Do While (lCol < lpicWidth) And (GetPixel(.hdc, lCol, lRow) = lBackColor)
                        lCol = lCol + 1
                    Loop
                    
                    If (lCol < lpicWidth) Then
                        lStart = lCol
                        
                        Do While (lCol < lpicWidth) And _
                                 (GetPixel(.hdc, lCol, lRow) <> lBackColor)
                            lCol = lCol + 1
                        Loop
                        
                        If (lCol > lpicWidth) Then lCol = lpicWidth
                        
                        hTempRegion = CreateRectRgn(lStart, lRow, lCol, lRow + 1)
                        lResult = CombineRgn(hRegion, hRegion, hTempRegion, RGN_OR)
                        
                        DeleteObject hTempRegion
                    End If
                Loop
            Next
        End With
        
        RegionFromImage = hRegion
        SetWindowRgn fRgnForm.hWnd, hRegion, True
    End Function
    Call it like so:
    Code:
    RegionFromImage Picture1, Picture1, &HFF00FF '&HFF00FF being Magenta
    Keeps it's transparency when dragged.

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Make Picturebox color Transparent...

    Your code is fairly common on the net. Keep these few things in mind if you plan on using it in the future

    1. The picturebox's container's scalemode must be twips else you need to modify the initial values of lpicHeight, lpicWidth in that routine
    2. You may need to ensure the picturebox's AutoRedraw is true before sending it to that routine, if the picturebox is hidden or partially obscured
    3. Don't destroy the passed region handle

    Just FYI. For small images that routine should work fine. If ever wanting to apply to much larger images, consider code similar to what I posted -- it is far faster and can be tweaked a bit for more speed by removing code related to options you would never use.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Make Picturebox color Transparent...

    Without any code other than what is needed to create a user control with your gif will work quite nice


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Tags for this Thread

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