Allpax
asked on
GIFs lose transparency in ActiveX control
I have an activex control that allows the user to dynamically add image boxes to the control. The pictures look great at first, but after they are saved to the propertybags and then reloaded, the gifs somehow lose their transparency. Their background becomes a solid color. Does anybody know how to stop this from happening. The following code shows how I am saving and loading from the propertybags:
readproperties event:
Image.Picture = propbag.ReadProperty("Imag e")
writeproperties event:
Call propbag.WriteProperty("Ima ge", Image.Picture)
readproperties event:
Image.Picture = propbag.ReadProperty("Imag
writeproperties event:
Call propbag.WriteProperty("Ima
Give this a try..
You need to use UseMaskColor property in the ImageList Control
====Set UseMaskColor property and set it to true. This will allow Visual Basic to make the background colour of the icons transparent.
http://www.glyfx.com/guide_vb6.html
You have to put the images in a ImageList control and load the image boxes from there.
You must think of using controls other than imagebox, like a picture box to do this... but this is well possible
Cheers
You need to use UseMaskColor property in the ImageList Control
====Set UseMaskColor property and set it to true. This will allow Visual Basic to make the background colour of the icons transparent.
http://www.glyfx.com/guide_vb6.html
You have to put the images in a ImageList control and load the image boxes from there.
You must think of using controls other than imagebox, like a picture box to do this... but this is well possible
Cheers
ASKER
The user selects the gifs while using the activex control. The images that are static, and have a specific property related to them are fine. It's only the dynamically loaded images into dynamically loaded image boxes that have the problem
ASKER
Can the imagelist control display all pictures to a user. If so how? Do I need to use something else to show the user the pictures?
ASKER
The user dynamically adds images to the form. I was originally using windows common dialog to show the file dialog box. The user would select a picture from the file dialog. If I use an imagelist I can no longer use the file dialog box. Does anyone have a suggestion?
Is it feasable in your application to only save the path to the original file and reload the original GIF file in ReadProperties event?
ASKER
No, because once the activex control is set up by the user, it will be installed on other systems. Therefore the path will no longer ne good.
Hi Allpax
I have been trying to reproduce the problem you described, but the transparency does seem to be retained using this code in my test usercontrol. The transparent areas of the GIFs show the backcolor of the UserControl.
Option Explicit
Private m_pic As StdPicture
Public Property Get Pic() As StdPicture
Set Pic = m_pic
End Property
Public Property Set Pic(NewPic As StdPicture)
Set m_pic = NewPic
Set Image1.Picture = m_pic
PropertyChanged "Pic"
End Property
Private Sub UserControl_Initialize()
'random backcolor
Randomize
UserControl.BackColor = RGB(Int(Rnd * 255), Int(Rnd * 255), Int(Rnd * 255))
End Sub
Private Sub UserControl_ReadProperties (PropBag As PropertyBag)
Debug.Print "ReadProperties", Now
Set m_pic = PropBag.ReadProperty("Pic" , Nothing)
Set Image1.Picture = m_pic
End Sub
Private Sub UserControl_WritePropertie s(PropBag As PropertyBag)
Debug.Print "WriteProperties", Now
PropBag.WriteProperty "Pic", m_pic, Nothing
End Sub
I have been trying to reproduce the problem you described, but the transparency does seem to be retained using this code in my test usercontrol. The transparent areas of the GIFs show the backcolor of the UserControl.
Option Explicit
Private m_pic As StdPicture
Public Property Get Pic() As StdPicture
Set Pic = m_pic
End Property
Public Property Set Pic(NewPic As StdPicture)
Set m_pic = NewPic
Set Image1.Picture = m_pic
PropertyChanged "Pic"
End Property
Private Sub UserControl_Initialize()
'random backcolor
Randomize
UserControl.BackColor = RGB(Int(Rnd * 255), Int(Rnd * 255), Int(Rnd * 255))
End Sub
Private Sub UserControl_ReadProperties
Debug.Print "ReadProperties", Now
Set m_pic = PropBag.ReadProperty("Pic"
Set Image1.Picture = m_pic
End Sub
Private Sub UserControl_WritePropertie
Debug.Print "WriteProperties", Now
PropBag.WriteProperty "Pic", m_pic, Nothing
End Sub
ASKER
I'm sorry I should have been more specific. The reason your code works is that you have a specific property related to the picture property. My problem is that I might have to load 100 different image boxes dynamically, so I can't really have a defined property related to each of them. If you try to save the image.picture to the propertybag you get the problem.
Well I modified it to dynamically add image controls with pictures, and I still do not see the problem.
Option Explicit
Private m_pic As Picture
Private NumPix As Long
Private Sub UserControl_Initialize()
'random backcolor
Randomize
UserControl.BackColor = RGB(Int(Rnd * 255), Int(Rnd * 255), Int(Rnd * 255))
End Sub
Private Sub UserControl_ReadProperties (PropBag As PropertyBag)
Debug.Print "ReadProperties", Now
Dim pic As StdPicture
Dim i As Long
NumPix = PropBag.ReadProperty("NumP ix", 0)
If (NumPix > 0) Then
For i = 1 To NumPix
Set pic = PropBag.ReadProperty("Pic" & CStr(i), Nothing)
Call Add(pic)
Next
End If
End Sub
Private Sub UserControl_Terminate()
Dim ndx As Long
If Image1.UBound = 0 Then Exit Sub
'unload all the dynamic image controls
For ndx = Image1.UBound To 1 Step -1
Unload Image1(ndx)
Next
End Sub
Private Sub UserControl_WritePropertie s(PropBag As PropertyBag)
Debug.Print "WriteProperties", Now
Dim i As Long
NumPix = Image1.UBound
If (NumPix > 0) Then
For i = 1 To NumPix
PropBag.WriteProperty "Pic" & CStr(i), Image1(i).Picture, Nothing
Next
End If
PropBag.WriteProperty "NumPix", NumPix, 0
End Sub
Public Property Set NewPic(NewPicture As StdPicture)
Set m_pic = NewPicture
Call Add(m_pic)
PropertyChanged "NewPic"
End Property
Public Property Get NewPic() As StdPicture
Set NewPic = m_pic
End Property
Public Sub Add(NewPicture As StdPicture)
'm_ic.Add NewPicture
Dim ndx As Long
ndx = Image1.UBound + 1
Load Image1(ndx)
With Image1(ndx)
Set .Picture = NewPicture
.Top = Int(Rnd * (UserControl.ScaleHeight / 1.5))
.Left = Int(Rnd * (UserControl.ScaleWidth / 1.5))
.Visible = True
End With
End Sub
Option Explicit
Private m_pic As Picture
Private NumPix As Long
Private Sub UserControl_Initialize()
'random backcolor
Randomize
UserControl.BackColor = RGB(Int(Rnd * 255), Int(Rnd * 255), Int(Rnd * 255))
End Sub
Private Sub UserControl_ReadProperties
Debug.Print "ReadProperties", Now
Dim pic As StdPicture
Dim i As Long
NumPix = PropBag.ReadProperty("NumP
If (NumPix > 0) Then
For i = 1 To NumPix
Set pic = PropBag.ReadProperty("Pic"
Call Add(pic)
Next
End If
End Sub
Private Sub UserControl_Terminate()
Dim ndx As Long
If Image1.UBound = 0 Then Exit Sub
'unload all the dynamic image controls
For ndx = Image1.UBound To 1 Step -1
Unload Image1(ndx)
Next
End Sub
Private Sub UserControl_WritePropertie
Debug.Print "WriteProperties", Now
Dim i As Long
NumPix = Image1.UBound
If (NumPix > 0) Then
For i = 1 To NumPix
PropBag.WriteProperty "Pic" & CStr(i), Image1(i).Picture, Nothing
Next
End If
PropBag.WriteProperty "NumPix", NumPix, 0
End Sub
Public Property Set NewPic(NewPicture As StdPicture)
Set m_pic = NewPicture
Call Add(m_pic)
PropertyChanged "NewPic"
End Property
Public Property Get NewPic() As StdPicture
Set NewPic = m_pic
End Property
Public Sub Add(NewPicture As StdPicture)
'm_ic.Add NewPicture
Dim ndx As Long
ndx = Image1.UBound + 1
Load Image1(ndx)
With Image1(ndx)
Set .Picture = NewPicture
.Top = Int(Rnd * (UserControl.ScaleHeight / 1.5))
.Left = Int(Rnd * (UserControl.ScaleWidth / 1.5))
.Visible = True
End With
End Sub
ASKER
I used the same code you have and did the following
I enabled edit at design time.
In the click event of the user control I added code to add picture.
Dim x As StdPicture
Set x = LoadPicture("c:\crap.gif")
Set NewPic = x
When I minimize the form, then maximize it I lose the transparency.
I enabled edit at design time.
In the click event of the user control I added code to add picture.
Dim x As StdPicture
Set x = LoadPicture("c:\crap.gif")
Set NewPic = x
When I minimize the form, then maximize it I lose the transparency.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Erik37
Thanks for your help. I just went ahead and added an imagelist to the usercontrol and added the images to that. It seems to work fine with that.
Thanks for your help. I just went ahead and added an imagelist to the usercontrol and added the images to that. It seems to work fine with that.
Once you save the file, it is converted to a Windows bitmap and is no longer a GIF file. That means the transparent property is lost.
Does the user select the GIF file, or is it a set file that you specify in code?