Results 1 to 5 of 5

Thread: PictureBox and transparency

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    36

    Question PictureBox and transparency

    Folks,

    This sort of question comes up once in a while. I've seen similar topics.

    I want to make a portion of PictureBox transparent. The intent is to put a few of these PictureBoxes on a form which is itself transparent. Images aren't overlapping. The disjointed partially transparent PictureBoxes will look as if they were "flying" in formation.

    My images are drawn with only a few colors, so I can use fuchsia as key color for transparency. I was really excited when I found out about Form.TransparencyKey . Thinking wishfully, I've looked for something similar in the PictureBox control. May be it's there under a different name, but I couldn't find it yet.

    I know that I can write a fairly straightforward function that will generate a region, provided a bitmap and a color key. Then this region can be assigned to Control.Region property. But is there a standard picture-box-type control that has a built-in feature like Form.TransparencyKey ?

    Color.Transparent constant does exist. Is there a way of somehow selecting Color.Transparent and drawing with it?

    Any suggestion, insight or reference is really appreciated!

    Cheers,
    - Nick

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

    Re: PictureBox and transparency

    Hi Nick,
    There isn't a standard PictureBox with a TransparencyKey, but the whole idea of Inheritance is that you can make your own. Here's an example how you could make one. I haven't considered all the details, but it shows the principle. Add a Class to your project and code it like this:
    Code:
    Public Class PictureBoxExtra
        Inherits PictureBox
    
        Private _transparencyKey As Color
        Private _image As Image
    
        Public Property TransparencyKey As Color
            Get
                Return _transparencyKey
            End Get
            Set(ByVal value As Color)
                _transparencyKey = value
             Me.Image=_Image 'Uses Image.Set to update the image with the new TransparencyKey.
            End Set
        End Property
    
        Public Shadows Property Image As Image
            Get
                Return _image
            End Get
            Set(ByVal value As Image)
                Dim bmp As Bitmap = CType(value, Bitmap)
                bmp.MakeTransparent(_transparencyKey)
                _image = bmp
                MyBase.Image = bmp
            End Set
        End Property
    
    End Class
    Then build the project. PictureBoxExtra will appear in the toolbox with a cogwheel icon and you can add it to your Form like any PictureBox -- but now you can set the TransparencyKey. Like the Form.TransparencyKey, it has some limitations:
    1. in this design, the TransparencyKey only affects the Image, not the BackgroundImage.
    2. you can't see other controls on the Parent Form through the transparent areas. Windows.Forms controls don't work that way. But you can get round it by putting the PictureBoxExtra on a separate form. Ask if you want details.
    3. You can't do partial transparency this way. For that you need a non-Forms technique such as WPF. That also means you can't have antialiased (smoothed) edges.
    Still, I think it works more or less the way you wanted on a form with transparent areas.

    BB

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

    Re: PictureBox and transparency

    no. regions are your only option.
    pre vb.net there were lightweight windowless controls that would be exactly what you're looking for, but unfortunately they were discontinued in vb.net

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    36

    Re: PictureBox and transparency

    Hi BB,

    Thanks for the code! It worked.
    Thanks for the detailed steps too. Initially, I didn't want to subclass controls, because I didn't know how to use subclassed controls.
    By the way, I have a feeling that MSDN article "How to: Give Your Control a Transparent Background" could come handy some times, although I didn't have to follow it this time.

    - Nick
    Last edited by kender; Feb 9th, 2011 at 01:40 PM. Reason: added more

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

    Re: PictureBox and transparency

    @Kender: The code was just off the cuff but it demonstrates that making a PictureBox with its own TransparencyKey is possible. I've put an improved version below, but it still has a few issues to work out.

    Quote Originally Posted by .paul. View Post
    no. regions are your only option.
    Did you try it Paul? I discovered it by accident while trying work out something with Region, and I am amazed myself that it works. But it does, apart from a few possibly minor issues. You can put multiple copies of the control on a form and give each one its own TransparencyKey, independently of the form's TransparencyKey. I agree that using a Region could be better because it doesn't mean wasting a colour, but this version is simpler to code.

    Here's the improved version:
    Code:
    Imports System.Windows.Forms
    Imports System.Drawing
    
    Public Class PictureBoxTK
    
        Inherits System.Windows.Forms.PictureBox
    
        Private _transparencyKey As Color
        Private _image As Image
    
        Public Property TransparencyKey As Color
            Get
                Return _transparencyKey
            End Get
            Set(ByVal value As Color)
                _transparencyKey = value
                Me.Image = _image 'use Image.Set to refresh image.
            End Set
        End Property
    
        Public Shadows Property Image As Image
            Get
                Return _image
            End Get
            Set(ByVal value As Image)
                _image = Me.MakeTransparent(value, _transparencyKey)
                MyBase.Image = _image
            End Set
        End Property
    
        Public Sub New()
            Me.Image = MyBase.Image
        End Sub
    
        Private Function MakeTransparent(ByVal img As Image, ByVal clr As Color) As Image
            If img Is Nothing Then Return MyBase.Image
            Dim bmp As New Bitmap(img)
            bmp.MakeTransparent(Me._transparencyKey)
            Return bmp
        End Function
    
    End Class
    BB

Tags for this Thread

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