Displaying Transparent GIF's

M

Marge

After importing a VB6 project into Express 2008 I have two gif's with
transparency do not display correctly.

In VB6 these pictures where used with the image object which had
different properties to the picturebox unfortunately there only seems
to be the picturebox option in the toolbox of Express 2008?

any ideas/help PLEASE :blush:)
 
S

SurturZ

Set the .BackColor to "Transparent".

It's at the top of the "Web" tab when you click the BackColor dropdown button.
 
S

SurturZ

Ugh. Well I got it working, sorta. Add a new class:

Public Class PanelTransparent
Inherits Panel

Protected Overrides ReadOnly Property CreateParams() As
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = &H20 'WS_EX_TRANSPARENT
Return cp
End Get
End Property

Protected Sub InvalidateEx()
If Parent Is Nothing Then Exit Sub
Dim rc As New Rectangle(Me.Location, Me.Size)
Parent.Invalidate(rc, True)
End Sub

Protected Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)
'do not allow the background to be painted
End Sub

Private mimg As Image
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawImage(mimg, 0, 0)
End Sub
Public Sub New(ByVal img As Image)
mimg = img
End Sub
End Class

You then need to add an instance of the new control at runtime:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim img As Image =
Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\pitransgif.gif")
Dim pnlTransparent As New PanelTransparent(img)
Me.Controls.Add(pnlTransparent)
pnlTransparent.BringToFront()

End Sub
 
M

Marge

Ugh. Well I got it working, sorta. Add a new class:

Public Class PanelTransparent
    Inherits Panel

    Protected Overrides ReadOnly Property CreateParams() As
System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = &H20 'WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property

    Protected Sub InvalidateEx()
        If Parent Is Nothing Then Exit Sub
        Dim rc As New Rectangle(Me.Location, Me.Size)
        Parent.Invalidate(rc, True)
    End Sub

    Protected Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)
        'do not allow the background to be painted  
    End Sub

    Private mimg As Image
    Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
        e.Graphics.DrawImage(mimg, 0, 0)
    End Sub
    Public Sub New(ByVal img As Image)
        mimg = img
    End Sub
End Class

You then need to add an instance of the new control at runtime:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

        Dim img As Image =
Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\pitransgif.gif")
        Dim pnlTransparent As New PanelTransparent(img)
        Me.Controls.Add(pnlTransparent)
        pnlTransparent.BringToFront()

    End Sub

--
David Streeter
Synchrotech Software
Sydney Australia









- Show quoted text -

IS THIS PROGRESS IN BASIC PROGRAMMING?

all I had to do before was drag an image object onto my form & set its
properties & tell it which image to use.
 
M

Marge

Hmm. That doesn't actually work.

I know, I found that & was hopeful :blush:)

Thanks for the bobpowell link but I'm not sure this is the same, what
he is doings seems much more complicated that what I want to do.

I have a .gif file which has transparency set, the gif is actually a
drawn pupil of an eye, black & round with a little white dot for
reflection. Obviously the image is square so the background is made
transparent so that when my pupil is added to the face image the
corners of the pupils are not visible.

In VB6 the gif was loaded into an image object, VBE does not have an
image object only picturebox which does not have the same properties
list.
 
M

Martin H.

Hello Marge,

I checked that with a small VB5 project. You are correct that after a
conversion this problem occurs. It seems that the import assistant
removes the transparency from the file. If you still have the original
file, just add it to the PictureBox again (the PictureBox control
handles transparency correctly). If you do not have it anymore, then you
would need to save it again and restore the transparency with a graphics
editor.

Best regards,

Martin
 
R

rowe_newsgroups

IS THIS PROGRESS IN BASIC PROGRAMMING?

Just a casual reminder that Visual Basic .NET is not an update to
classic Visual Basic (yes, Microsoft marketing may tell you otherwise,
but I'm more involved with VB than their marketing department is). I
urge all "new comers" to never think they are working with an updated
language, but rather an entirely new one.

With that said, you will find many things you prefer in classic VB,
but after a while you will grasp the new concepts and start
likely .NET more than you ever like classic VB.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
M

Marge

Replies to:
Martin H
Tried it does sort of reset the transparency but not quite, there
are images on the form in total, 1 large that almost fills the form &
2 smaller images which are of pupils for eyes. when the form first
runs after conversion all the background colours are very strange, if
I right click each image and import them from file loacation
everything start to look ok but when I run the pupils which I want to
be round are still square images.(tried setting image background
colour to webtransparent & setting form transparencyKey to the same
but with no luck)

rowe_newsgroups,
Yes you are correct I know this is very different, Sorry, Just
frustrating at times.


I first had a look at VB Express 2008 because I downloaded it onto my
sons PC hoping to get him to gain an interest in it, I was pleasantly
suprised at some of the new editions so decided to download it for
myself too.

I got my son to add a couple of images to a form, add a couple of
radio buttons and a cmdButton then right the code to change BgColor
and select image. I realised today that one of his images was an
animated transparent Gif !!!!!!! & it works perfectly :blush:)

Martin H was correct again, it is the import assistant that seems to
cause the problem, I decided to remove all trace of the importeed
project and recreate it from scratch, that would solve the problem ;o)

No, somewhere some how VBExpress seems to know that this very same
project once existed before & will not run for multiple errors. :blush:(

My last hope is to remove everting again, restart the PC, recreate the
project using completely different file names & obj names but some how
I still don't think its going to allow me.

Another strange thing was also discovered today, In my desperation I
decided to make a VB6 exe of this proj & a VBExpress exe. save them
both to seperate folders on a floppy and install them on my sons PC.

The VB6 file was just that one item an exe file, I didn't need to
install it I could run it from the floppy or just coppy onto hard
drive if I wished and run it, no problems at all.

The VBExpress version had 3 items, a Apllication files folder,
setup.exe, and project1.application. If you click the application or
the setup.exe they both do the same and install the program. Clicking
the shortcut in star/programes etc runs the program ok with the faulty
non transparent gifs.

Here is the strange part, normally if you right click a start menu
shortcu you can "find target folder" so you can find the exe file, you
don't get the option, I also tried to search the whole hard drive for
the file name, it did not exsist? I finally went to controlpanel add/
remove to remove the program(it was listed there) and I did uninstall
it but how can you run an .Exe that doesn't exsist????

If anyone is interested in what is causing all this fuss, below is a
link to the VB6Exe and an image of the resulting VBExpress project.

http://www.ochwat.co.uk/exe/eyes.exe

http://www.ochwat.co.uk/exe/eyesimg.jpg
 
S

SurturZ

If all you are trying to do is layer one image on top of another, you might
be better off just drawing them on the same picturebox.

Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
Dim imgBG As Image =
Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\background.gif")
Dim imgFG As Image =
Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\pitransgif.gif")
PictureBox2.BackgroundImage = imgBG
e.Graphics.DrawImage(imgFG, New Rectangle(0, 0, imgFG.Width,
imgFG.Height))
End Sub

I tested this and it supports GIF transparency fine. Change the "0, 0" in
"New Rectangle(0, 0," to locate the foreground image.

I can't believe I didn't suggest this in the first instance. This also
allows animation, just call PictureBox2.Refresh() whenever you want to update
the display.

Simpler and better than subclassing the Panel control.
 
M

Martin H.

Hello Marge,
Tried it does sort of reset the transparency but not quite, there
are images on the form in total, 1 large that almost fills the form&
2 smaller images which are of pupils for eyes. when the form first
runs after conversion all the background colours are very strange, if
I right click each image and import them from file loacation
everything start to look ok but when I run the pupils which I want to
be round are still square images.(tried setting image background
colour to webtransparent& setting form transparencyKey to the same
but with no luck)

I just sent you an e-mail with a small VB.NET Eyes project.
I would expect that it should solve your problem.

Best regards,

Martin
 
M

Marge

If all you are trying to do is layer one image on top of another, you might
be better off just drawing them on the same picturebox.

    Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        Dim imgBG As Image =
Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\background.gif")
        Dim imgFG As Image =
Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\pitransgif.gif")
        PictureBox2.BackgroundImage = imgBG
        e.Graphics.DrawImage(imgFG, New Rectangle(0, 0, imgFG.Width,
imgFG.Height))
    End Sub

I tested this and it supports GIF transparency fine. Change the "0, 0" in
"New Rectangle(0, 0," to locate the foreground image.

I can't believe I didn't suggest this in the first instance. This also
allows animation, just call PictureBox2.Refresh() whenever you want to update
the display.

Simpler and better than subclassing the Panel control.

I didn't even know you could have more than 1 image per picturebox :blush:)
Thanks I'll have a go at that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top