Results 1 to 6 of 6

Thread: [Help]Picturebox/Image Transparency Issues.

  1. #1

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Question [Help]Picturebox/Image Transparency Issues.

    Hi,

    I've been trying to figure this out for some time now. I'm having problems making an image properly "transparent"

    Code:
     Dim x As Integer
        Dim y As Integer
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
            Dim blah As New PictureBox
            blah.Parent = PictureBox1
            blah.Image = My.Resources.Mal1t1a_Mediaplayer
            blah.SizeMode = PictureBoxSizeMode.StretchImage
            blah.BackColor = Color.Transparent
            blah.Left = x - (blah.Width / 2)
            blah.Top = y - (blah.Height / 2)
            blah.BorderStyle = BorderStyle.None
        End Sub
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            x = e.X
            y = e.Y
        End Sub
    The inital image is Transparent, however, once you create another image very close to it, there is a "box" around it. That "box" is what I am trying to get rid of. I have no clue on how to get rid of it. Yes, I have made the image itself, transparent, as the initial image is transparent. Just any image that makes contact with the other images show the "box". Any idea's thoughts? Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [Help]Picturebox/Image Transparency Issues.

    its because you're displaying your image in a picturebox (the visible box).
    you could draw the transparent image directly onto the form or reshape the picturebox so it is the exact shape of the visible part of your image.

  3. #3

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: [Help]Picturebox/Image Transparency Issues.

    Well, now that I "really" need this done, how would I go about actually drawing the transparent image onto the form? Also, my images are round, not square. So I can't shape the picturebox to match the image.

  4. #4
    Addicted Member Dark Anima's Avatar
    Join Date
    Sep 2008
    Posts
    183

    Re: [Help]Picturebox/Image Transparency Issues.

    I think the only way for you do what you want without using a graphics engine, is to create a new borderless form, draw the image on it(using picturebox or whatever you want), use its transparency key property to make a certain color of your image completely invisible, which will(given that your image is round and the background is colored as your transparency key) cut the form into a shape of a circle. Then just modify the Opacity(0 - 1) property to set it as transparent as you want.

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [Help]Picturebox/Image Transparency Issues.

    Hi Mal1t1a,
    You're getting some contradictory information here, so I'll have a go at clearing it up. You can put background images on all kinds of controls -- Forms, PictureBoxes, Panels, Buttons, TabPages etc. When it comes to flexibility in drawing images on controls, most controls score about 2 -- you can show a background image and scale it in different ways. PictureBoxes score about 4 and a half -- you can superimpose two scalable images, and they are double buffered (less flickering) by default.

    But ALL these controls score around 200 for versatility when you use Graphics methods in the control's Paint event. You can superimpose any number of images, with alpha blending (=variable transparency), scaling, positioning, rotation, warping, clipping to a desired shape, fading, recolouring and loads of other things. That applies to Panels, Buttons etc., but in practice you would probably use either a Form or a PictureBox for that purpose. You'd use a Form if all you are doing is drawing on it, or a PictureBox if you want to share the Form with Buttons, ToolBars etc. (Some people favour Panels for this purpose but you have to take special measures to prevent flickering.)

    Some controls can be transparent and show transparent images. But when you put one control on top of another, .Net takes a very simple-minded approach to transparency. In your code, PictureBox1 and blah are a good example. The transparent areas of a control (blah) show up as the colours of its Parent control (PictureBox1) where they overlap. Where they don't overlap, you just see the background colour and background image of the containing Form.

    Here's a simple example of how you could superimpose two images using the Paint event of PictureBox1. Both images are scaled to fit the PictureBox, and the top image (which I assume is partly transparent) moves with the mouse).
    Code:
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            PictureBox1.Refresh()
        End Sub
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.DrawImage(My.Resources.Image1, PictureBox1.ClientRectangle)
            Dim p As Point = PictureBox1.PointToClient(MousePosition)
            p.Offset(-PictureBox1.Width \ 2, -PictureBox1.Height \ 2)
            Dim rect As New Rectangle(p, PictureBox1.ClientSize)
            e.Graphics.DrawImage(My.Resources.Image2, rect)
        End Sub
    I think what .Paul means by drawing directly on the Form is to superimpose the two images using the Form's Paint event. (I'm not sure what he means by reshaping the picture box, since they are always rectangular.) If you use a Form instead of the PictureBox, set its DoubleBuffered=True to prevent flickering when you move the mouse.

    One thing you can't do this way is to have different images in different PictureBoxes or other controls, and have another image move transparently over them. You'd have to paint all the images on the same PictureBox or Form. That's because only one of the controls can be the Parent of the moving control.

    Using superimposed forms, suggested by DarkAnima, offers an alternative there. You can't get proper partial transparency (alpha blending) that way, but you can change the overall opacity of the top Form and make parts of it totally transparent using the TransparencyKey property. Controls on the top form (PictureBoxes, RichTextBoxes and all kinds of things) are also affected by its opacity setting, which is something you can play with. A tip here: make TopForm.Owner=BottomForm (in code only). That way the forms will always stay together.

    I hope this sheds some light.

    Cheers, BB
    Last edited by boops boops; Nov 13th, 2009 at 01:02 PM. Reason: image names

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [Help]Picturebox/Image Transparency Issues.

    I had uploaded an example of having transparent images overlap eachother. You can find it here:

    http://www.vbforums.com/showthread.p...ht=transparent

    It is from VS2003, but you should be able to open it (let it convert if it asks) and run it without problems on VS2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width