CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    Sep 2005
    Posts
    233

    PictureBox Transparent

    Hi. I have a picturebox control with an image that is a GIF. The image is not an animation, only a single frame. The background of the image is transparent, as i erased it in photo shop. What code can i use to make the rest of the picturebox transparent, so there isnt a box around my circular GIF object? I want a result like in visual basic 6.0 with an image control. is there an image control for vb.net?
    Last edited by TCanuck42; June 15th, 2007 at 12:31 AM.
    Seed Gaming
    www.seedgaming.com

  2. #2
    Join Date
    Jun 2007
    Posts
    16

    Re: PictureBox Transparent

    If you look under the properties for the picturebox, choose "BackColor" the first choice under "Web" is Transparent. Try that.

    - Joe

  3. #3
    Join Date
    Sep 2005
    Posts
    233

    Re: PictureBox Transparent

    Thanks but that doesnt work. What that does is colour the back of the picturebox to match that of the form. If i have many picture boxes on the screen to create a larger image, there is still a rectangle surrounding the main image. The image itself has a transparent background, so what i am seeing is the picturebox's background. How can the picturebox's background, not the actual images, be transparent? Are there any controls i can download?
    Seed Gaming
    www.seedgaming.com

  4. #4
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: PictureBox Transparent

    The best way to do this is to simply draw the image directly over the form by overriding the OnPaint event:

    Code:
    Public Class Form1
    
        Private _myImage As New Bitmap("C:\myImage.gif")
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
    
            e.Graphics.DrawImageUnscaled(New Bitmap(_myImage), 100, 100)
        End Sub
    End Class
    You can get a lot more creative, but that's the idea.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: PictureBox Transparent

    TCanuck : What do you mean by :
    there is still a rectangle surrounding the main image
    ¿

    This is perhaps a dumb comment / question from my side, but Did you perhaps set the Picturebox's BorderStyle to something different than None

    Because, no matter how I try, I cannot replicate the same behaviour you've found.

    I made 2 GIFs, both with Transparent Backgrounds. I added 2 Pictureboxes to my form, then, in each Picturebox, added each respective picture. With one Picturebox ( The Arrow ), I set the BorderStyle to None.
    With the second Picturebox ( The Doughnut ), I set the BorderStyle to FixedSingle

    I did not add any code.

    And this is what I got at run time :
    Last edited by HanneSThEGreaT; June 14th, 2010 at 05:40 AM.

  6. #6
    Join Date
    Sep 2005
    Posts
    233

    Re: PictureBox Transparent

    I have a gradient set as the background to an picturebox. I meant that regardless of whether or not the GIF is transparent or not, their is still a solid colored rectangle surrounding the image. You can see what i mean if you set a picture to a picturebox behind the arrow and then set the arrows backcolor to transparent.

    I did, however, discover that if you execute the following code, the "arrow" in you example will be transparent to the picturebox behind it.

    Code:
    picArrow.Parent = PictureBox
    Now, when there is code that moves the arrow's top and left attributes, there is a slight flickering/blinking effect. How can this be removed?
    Seed Gaming
    www.seedgaming.com

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: PictureBox Transparent

    Have a look here, it explains why the flickering occur :
    http://www.codeguru.com/forum/showpo...45&postcount=4

  8. #8
    Join Date
    Sep 2005
    Posts
    233

    Re: PictureBox Transparent

    Thanks for the reply. All three options sound good. Could you please explain just how i could execute option 1, and if that doesn't work, option 2? Thanks in advance!
    Seed Gaming
    www.seedgaming.com

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: PictureBox Transparent

    For method 1, Try putting this in your Constructor :
    Code:
    SetStyle(ControlStyles.DoubleBuffer, True)
    SetStyle(ControlStyles.UserPaint, True)
    SetStyle(ControlStyles.AllPaintingInWmPaint, True)

  10. #10
    Join Date
    Sep 2005
    Posts
    233

    Re: PictureBox Transparent

    Sorry, but i haven't worked much with constructors and destructors. please explain. thanks in advance
    Last edited by TCanuck42; June 25th, 2007 at 05:22 PM.
    Seed Gaming
    www.seedgaming.com

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: PictureBox Transparent

    OK, try this :
    Code:
        Public Sub New()
            MyBase.New()
            
            SetStyle(ControlStyles.DoubleBuffer, True)
            SetStyle(ControlStyles.UserPaint, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        End Sub

  12. #12
    Join Date
    Sep 2005
    Posts
    233

    Re: PictureBox Transparent

    Thanks for the prompt reply. I added that code to my Form and ran the program and there was an error. It said it was missing the "InitializeComponent" command so i added it in and then there were no more errors, but the flickering still occurred. Should i have done something else, or so i need to call the sub somewhere?
    Seed Gaming
    www.seedgaming.com

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: PictureBox Transparent

    Hmm...
    Could you perhaps attach your project ¿

  14. #14
    Join Date
    Sep 2005
    Posts
    233

    Re: PictureBox Transparent

    My project is very large, so i will create a sample of what i'm using and what is happening. As you walk around in the demo I made (its like a game), you'll notice once you go over objects or you are walking against a collision detector, there is a lot of flickering. Hope this helps!

    By the way, even the small portion sample i created exceeds the codeguru 2mb limit, even while zipped, so please download it at this link: http://rapidshare.com/files/39539835...oblem.rar.html
    Seed Gaming
    www.seedgaming.com

  15. #15
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: PictureBox Transparent

    The only reason I can see why the zip may be so big is because of the EXE files included in it, is that so ¿

    Just remove the EXE files, and see if you can upload it then

Page 1 of 3 123 LastLast

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

Featured