Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GIFs lose transparency in ActiveX control

Posted on 2004-11-08
13
Medium Priority
?
293 Views
Last Modified: 2008-02-01
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("Image")

writeproperties event:

        Call propbag.WriteProperty("Image", Image.Picture)
0
Comment
Question by:Allpax
  • 7
  • 5
13 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 12524678
The problem is this:

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?
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 12525185
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
0
 
LVL 1

Author Comment

by:Allpax
ID: 12526066
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Allpax
ID: 12526205
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?
0
 
LVL 1

Author Comment

by:Allpax
ID: 12527795
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?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12528004
Is it feasable in your application to only save the path to the original file and reload the original GIF file in ReadProperties event?
0
 
LVL 1

Author Comment

by:Allpax
ID: 12528465
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.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12534398
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_WriteProperties(PropBag As PropertyBag)
    Debug.Print "WriteProperties", Now
    PropBag.WriteProperty "Pic", m_pic, Nothing
End Sub
0
 
LVL 1

Author Comment

by:Allpax
ID: 12534581
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.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12535731
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("NumPix", 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_WriteProperties(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
0
 
LVL 1

Author Comment

by:Allpax
ID: 12536296
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.
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 2000 total points
ID: 12546699
Hi Allpax

Well I was able to see the problem by using the LoadPicture call in the UserControl_Click event in Edit mode.  I do believe that it is related to how LoadPicture translates/saves the picture.

I was able to load transparent GIFs by using the AsyncRead method of the usercontrol and it seems to retain the transparency through the WriteProperties/ReadProperties cycle.  Give it a try and see if it works out for you.

Private Sub UserControl_Click()
   
    'Trigger a "download" of the picture
    AsyncRead "c:\globe1.gif", vbAsyncTypePicture, "NewPicture", vbAsyncReadSynchronousDownload
   
End Sub

Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
   
    'This event is fired when the picture is downloaded
    If AsyncProp.PropertyName = "NewPicture" Then
        Set NewPic = AsyncProp.Value
    End If
   
End Sub
0
 
LVL 1

Author Comment

by:Allpax
ID: 12547605
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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question