Link to home
Start Free TrialLog in
Avatar of OnlineGuitarist
OnlineGuitarist

asked on

How to set picture box image transparency.

I have a picturebox with an image in it.  I am using a gif file, and I set the transparency to pallette #255.  I put the image on the picturebox, and there is no transparency.  How can I view the background forms and controls with the picturebox on top of them, with one color on the image in the picturebox set to transparent?
Avatar of beji
beji

hmm

Maybe you can set the properti Autodraw=true

or use the bitblt API

/bj
ASKER CERTIFIED SOLUTION
Avatar of felipe_51
felipe_51

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
wrote a quick app that works as such, when you click on the picturebox the color under your mouse pointer becomes the transparent color in that image. I didnt use API calls so it would be to slow for anything but small images.

Private Sub Form_Load()
    Picture1.Appearance = 0
    Picture1.BorderStyle = 0
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    makeTransparent Picture1.Point(X, Y)
End Sub

Private Sub makeTransparent(tcolor)


    For X = 0 To Picture1.Width
          For Y = 0 To Picture1.Height
              If Picture1.Point(X, Y) = tcolor Then
                   Picture1.PSet (X, Y), Form1.BackColor
              End If
          Next Y
    Next X

End Sub
THe best thing to do would be to take your source image into a paint program and replace the color you wish to be transparent with the background color of your form.....
Avatar of OnlineGuitarist

ASKER

Maybe i should explain what I'm trying to accomplish to make things clearer.

I have a picture of a musical staff on the form.  it's prety much dormant.

________________
________________
________________
________________
________________

What I did was create a gif image of a musical note
   -\
   |
   O

with the background set to transparent.  I would like to move the note around the staff, in different positions, while displaying the form with the picture on it, so the staff appears in the back of the musical note.  Is there anyway around this without having to create hundreds of different musical notes with lines on them in a paint program and have to program a ton of extra code to accomodate these extra images?  It seems I should be able to do this with one picture box, and set a color of the musical note gif on the picture box, transparent.  Do you think I may have created the transparency incorrectly?
Hi OnlineGuitarist

I’ve been working on this, and find a way
to do it, but it seams that you need a picturebox!

I think that you can achieve this effect with an
Image control. Imagebox seams to work different
than a Picturebox. It don’t have as much feature
as a PictureBox but load faster at least load faster and have a
"Stretch" property, which Picturebox don’t.

****Concerning transparency ****
Fist try setting the Transparent effect of your gif image with Microsoft Photo Editor, using the Set Transparent Color pencil in the toolbar, to discard any possibility that you have set the transparent color incorrectly.
*****************************

To try the example, open a new project and add an (Imagebox)
Rather than a PictureBox. Set the Imagebox.Picture
To your gif image. And set the Form.picture to the staff picture.

And add the following code which will let you drag the image
at runtime, so you could move the image where you want,
and see the transparent effect.


Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        Image1.Move Image1.Left + X, Image1.Top + Y
        Debug.Print X
    End If
End Sub

Also If you would like to place a lable with some text remembet to change
the BackStyle of your lable to “Transparent”. So your effect preserves over
the labels.

Hope this helps
Best of lucks
Regards, Felipe.
i did something similar.  Instead of using an image for the music staff, I just used some line controls, and I send the transparent gif, an image box works with transparency, but the picture box did not.  so anyhow, now i can send the staff in front of the image boxes', and i was able to achieve the use of only 1 musical note.  Thanks for the insight.