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.
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"
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.
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.
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?
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"
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
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"
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.