Results 1 to 17 of 17

Thread: Image Transparent Background

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    48

    Image Transparent Background

    Hey,

    I have a GIF with a transparent background that I want to put on a form overtop another image. When I do that, the background of the PictureBox control shows up even when I set the BackColor property to Transparent.

    Is there a way to do this?

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

    Re: Image Transparent Background

    what do you have 2 pictureboxes over eachother?

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    48

    Re: Image Transparent Background

    Yup, thats how I have it now.

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

    Re: Image Transparent Background

    You can draw the images on the form itself, which will retain any image transparency that exsits. I did you a quick sample to show you how.

    there are 3 gif files in the bin directory that the app loads onto the forms canvas in its paint event.
    Attached Files Attached Files

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    48

    Re: Image Transparent Background

    Thanks a lot, but I think I need to use a control because the user has to be able to move the images around and then I need to store the final position. This is easy using the different Mouse events but not possible if I draw the image onto the form. Any other suggestions?

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    48

    Re: Image Transparent Background

    I've read about the Forms transparencykey property which sounds like if I set the backcolor of the picturebox to a color (Red) and the transparencykey to the same color then it should make the picturebox backcolor transparent. But this doesn't work for some reason. Any idea why?

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

    Re: Image Transparent Background

    yes, that is because the forms transparency key is if you want to make the FORM transparent, for example if you wanted to give the form a non rectangular shape, adobe reader's 6/7 splash screen is a good example of this.

    I also know that 2003 had some weird bug with transparency when the PC was on 32bit color instead of 16bit. and you had to use the .MakeTransparent method of the image you were actually trying to make transparent.

    Unless someone has a better solution, I think you may have to rethink your strategy. Perhaps you will need to paint the images on the form, and track user mouse movements to move the images via code?

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Image Transparent Background

    I'm only a visitor in this forum since I don't know .net yet, but if you can make a usercontrol in .net like you can in VB6 then see this thread
    http://www.vbforums.com/showthread.php?t=385822

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

    Re: Image Transparent Background

    you can make user controls, but that doesn't get around the issue of making them transparent to eachother so that you can have them on top of eachother without an overlapping background of the picturebox

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Image Transparent Background

    If they act the same way as VB6 Usercontrols, then you just set the mask and maskcolor and they will be transparent to each other. In the link I posted above, I wrote some code for someone who wanted to do the same thing.

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

    Re: Image Transparent Background

    .NET user controls don't have those properties

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

    Re: Image Transparent Background

    i have been messing around and have made some progress (call it personal curiosity)

    I will see if i can finalize something for you and get you an example.

  13. #13

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    48

    Re: Image Transparent Background

    Thanks a lot!! Look forward to see what you come up with...

  14. #14
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Image Transparent Background

    Here is my try at it
    I created a usercontrol
    VB Code:
    1. Public Class UserControl1
    2.     Dim Draging As Boolean
    3.     Dim Xpos As Single
    4.     Dim Ypos As Single
    5.     Dim myBitMap As Bitmap
    6.     Public Sub New()
    7.         InitializeComponent()
    8.         myBitMap = New Bitmap("test.gif")
    9.         myBitMap.MakeTransparent(Color.White)
    10.         DoubleBuffered = True
    11.         Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
    12.         SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    13.         Me.BackColor = Color.Transparent
    14.     End Sub
    15.  
    16.     Private Sub UserControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    17.         If Draging = False Then
    18.             Draging = True
    19.             Xpos = e.X
    20.             Ypos = e.Y
    21.         End If
    22.  
    23.     End Sub
    24.  
    25.     Private Sub UserControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    26.         If Draging = True Then
    27.             Me.Left += (e.X - Xpos)
    28.             Me.Top += (e.Y - Ypos)
    29.         End If
    30.  
    31.     End Sub
    32.  
    33.     Private Sub UserControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    34.         Draging = False
    35.     End Sub
    36.  
    37.     Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    38.         e.Graphics.DrawImage(myBitMap, 0, 0)
    39.     End Sub
    40.  
    41. End Class
    Put it on a form with a picturebox and called it fish
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    4.         PictureBox1.Image = Image.FromFile("Backgnd.gif")
    5.         Fish.Parent = PictureBox1
    6.     End Sub
    7. End Class
    You can drag the usercontrol around and see that the picture from the picturebox shows through. It flickers a bit, but maybe there is something that can be done about that.

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

    Re: Image Transparent Background

    Quote Originally Posted by moeur
    You can drag the usercontrol around and see that the picture from the picturebox shows through. It flickers a bit, but maybe there is something that can be done about that.
    Yeah I was able to get the transparency working, and it looked nice, but the flicker was an issue. I found some sample C# code for drawing a backbuffer that works well, but I am currently working on converting it to VB.NET and integrating it to work with the translucent images

  16. #16
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Image Transparent Background

    The method I used is supposed to eliminate flicker since I set DoubleBuffer=true
    I made a couple of adjustments that reduced flicker even more. Try these:
    In the Usercontrol constructor (Sub New) add this line
    VB Code:
    1. SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    Also, change the Usercontrol Paint event to the following
    VB Code:
    1. If Draging = True Then
    2.             Me.Left += (e.X - Xpos)
    3.             Me.Top += (e.Y - Ypos)
    4.             Me.Refresh()
    5.             Me.Parent.Refresh()
    6.         End If
    Try it with and without the Me.Parent.Refresh() line and see which one you like better.

  17. #17
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Image Transparent Background

    I can't believe how easy this is after all that work I did trying to do it another way.
    There are basically two things you have to do to make a picturebox transparent.
    VB Code:
    1. 'put it into a container
    2. myPicturebox.Parent = bkgndPictureBox
    3. 'set its background color to be transparent
    4. myPictureBox.BackColor = Color.Transparent
    Edit: Just noticed that if you have two sprites and they overlap then back sprite does not show through transparent background of front sprite???
    Last edited by moeur; Feb 26th, 2006 at 01:28 PM.

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