Link to home
Start Free TrialLog in
Avatar of Milkus1
Milkus1Flag for Australia

asked on

Can i get true transparency with pictureboxes?

I have an app that places pictureboxes onto another larger picturebox.
My transparent gifs take on the backcolor of the larger picturebox.
Problem is when the smaller ones overlap, i dont get 'true' transparency...meaning the small picturebox below doesnt show thru...is it possible??
Avatar of RonaldBiemans
RonaldBiemans

You can't with 2 or more pictureboxes, but if you draw the second image on the picturebox than you can

like

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim x As Image
    x = Image.FromFile("c:\yoursecondtransparentimage.gif")

    e.Graphics.DrawImage(x, 1, 1)
    End Sub

In this example I load the image in the paint event, this ofcourse isn't  advisable,  load it outside of the paint
HMMMM, I was wrong you CAN get it also with 2 pictureboxes

Private Sub Form14_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As New PictureBox
        x.BackColor = Color.Transparent
        x.SizeMode = PictureBoxSizeMode.AutoSize
        Dim Y As Image
        Y = Image.FromFile("c:\yoursecondtransparentimage.gif")
        x.Image = Y
        PictureBox1.Controls.Add(x)

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A picturebox simulates transparency by painting the "transparent" areas the color of the parent control.  This is a problem when you want to overlap pictureboxes.

Another approach would be to get rid of the child pictureboxes completely and draw the images to your main picturebox directly.

        'Load up an image from disk
        Dim img As Image = Image.FromFile("c:\bw.gif")
        Dim bmp As New Bitmap(img)
        img.Dispose()

        'Create the graphics object to draw onto
        Dim g As Graphics = PictureBox1.CreateGraphics

        'Draw the images overlapping
        g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height)
        g.DrawImage(bmp, 20, 30, bmp.Width, bmp.Height)
Avatar of Milkus1

ASKER

Ronald...Sorry but I think you missed the point, I need true transparency so if I drag a small picturebox over another the image below is visible. The Gifs in the small pictureboxes have transparent backgrounds, but this just picks up the parent picturebox's backcolor.

Erik ...I need these things to be moveable...not painted onto one big box.

As usually i'm off to try Idlemind's suggestion...hope its as excellent as always!
I just pointed you to a VB6 sample.
The concept is the same for .NET.
Let's see if I can find a .NET sample...
Avatar of Milkus1

ASKER

IdleMind comes with the goods again. Basically set the unwanted areas in my gifs to a color (bright pink) and then used his method to remove all areas of this color.