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?
[407 byte] By [TCanuck42] at [2007-11-20 8:35:01]
# 1 Re: PictureBox Transparent
If you look under the properties for the picturebox, choose "BackColor" the first choice under "Web" is Transparent. Try that.

- Joe
joe1985 at 2007-11-10 3:09:20 >
# 2 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?
TCanuck42 at 2007-11-10 3:10:20 >
# 3 Re: PictureBox Transparent
The best way to do this is to simply draw the image directly over the form by overriding the OnPaint event:

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.
Craig Gemmill at 2007-11-10 3:11:29 >
# 4 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 :
HanneSThEGreaT at 2007-11-10 3:12:29 >
# 5 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.

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?
TCanuck42 at 2007-11-10 3:13:28 >
# 6 Re: PictureBox Transparent
Have a look here, it explains why the flickering occur :
http://www.dev-archive.com/forum/showpost.php?p=1584245&postcount=4
HanneSThEGreaT at 2007-11-10 3:14:24 >
# 7 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!
TCanuck42 at 2007-11-10 3:15:28 >
# 8 Re: PictureBox Transparent
For method 1, Try putting this in your Constructor :
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
HanneSThEGreaT at 2007-11-10 3:16:26 >
# 9 Re: PictureBox Transparent
Sorry, but i haven't worked much with constructors and destructors. please explain. thanks in advance
TCanuck42 at 2007-11-10 3:17:34 >
# 10 Re: PictureBox Transparent
OK, try this :
Public Sub New()
MyBase.New()

SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
HanneSThEGreaT at 2007-11-10 3:18:35 >
# 11 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?
TCanuck42 at 2007-11-10 3:19:34 >
# 12 Re: PictureBox Transparent
Hmm...
Could you perhaps attach your project
HanneSThEGreaT at 2007-11-10 3:20:32 >
# 13 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 dev-archive 2mb limit, even while zipped, so please download it at this link: http://rapidshare.com/files/39539835/Sample_Problem.rar.html
TCanuck42 at 2007-11-10 3:21:38 >
# 14 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 :)
HanneSThEGreaT at 2007-11-10 3:22:39 >
# 15 Re: PictureBox Transparent
I deleted the exe's and zipped it to the highest compression and it is now 1.56 MB, which is more than the 250k limit. Please download it from the link in my previous post from rapidshare. No viruses.
TCanuck42 at 2007-11-10 3:23:42 >
# 16 Re: PictureBox Transparent
Hello, I am posting this so that my problem resets to the top of the forum. Please help!
TCanuck42 at 2007-11-10 3:24:33 >
# 17 Re: PictureBox Transparent
Well, to be honest, I tried to go to rapidshare.com. It seems as if I have to register or something just to be able to download files. Can I get these files without registering at that site, many people ( including me ), won't register at hundreds of places
HanneSThEGreaT at 2007-11-10 3:25:41 >
# 18 Re: PictureBox Transparent
You don't have to register and i wouldn't expect you to. All you need to do is:
1.) Scroll down to the Select Your Download section and click "Free".
2.) Scroll down till you see the characters (numbers and letters) and enter them in the box below and press Download via Level(3) #4.
3.) Select your download location and whala.
TCanuck42 at 2007-11-10 3:26:45 >
# 19 Re: PictureBox Transparent
OK, I finally had time to download your project files. I guess I miss read that web page, or I was just a bit hurried.
I have played around with it, and it seems as if I have reduced the flickering somewhat - there is still flickering. I have also noticed that the quicker the picture moves, the less flickering happens - that I found interesting, and that is what I'm investigating now.
By the weekend, I hope I could have this solved for you, that is if you don't mind ;) :)

I've also figured out why your zipped / Rarred version is still so big : it is mainly about the beautiful background image of the form. Also, by deleting the OBJ and DEBUG folders, I have reduced the zip to 1.56 MB. If the background picture wasn't so big in size, we wouldn't have had this problem in uploading the file here :) ;)
HanneSThEGreaT at 2007-11-10 3:27:43 >
# 20 Re: PictureBox Transparent
Thanks for taking the time to download the project and please take as much time as you need to investigate and solve this problem. Next time, i will make sure to delete the obj and debug folders and make sure that my graphics aren't using up so much space. Thanks again.
TCanuck42 at 2007-11-10 3:28:40 >
# 21 Re: PictureBox Transparent
Sorry, just posting again to refresh this topic to the top of the forum. Any thoughts guys?
TCanuck42 at 2007-11-10 3:29:46 >
# 22 Re: PictureBox Transparent
Hello again TCanuck42! :)

OK, firstly, I didn't have much time over the weekend ( car troubles again - but that's besides the topic ).

I ended up doing this :
Public Class isJadefortD5
Public chardir As String
Public currplayerpic As PictureBox

Private Sub isJadefortD5_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Call MovePlayer(player, e.KeyCode, Me)
CheckCols(e.KeyCode)
End Sub

Private Sub isJadefortD5_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If pANI.Enabled = True Then Call StopMovePlayer(player)
End Sub

'Public Sub New()
' MyBase.New()

' SetStyle(ControlStyles.DoubleBuffer, True)
' SetStyle(ControlStyles.UserPaint, True)
' SetStyle(ControlStyles.AllPaintingInWmPaint, True)
' Me.InitializeComponent()
'End Sub
' Set the value of the double-buffering style bits to true.
Public Sub EnableDoubleBuffering()
' Set the value of the double-buffering style bits to true.
Me.SetStyle(ControlStyles.DoubleBuffer _
Or ControlStyles.UserPaint _
Or ControlStyles.AllPaintingInWmPaint, _
True)
Me.UpdateStyles()
End Sub

Private Sub CheckCols(ByVal key As Long)
'up on home and trees
If key = Keys.Up And player.Top < 167 Then
player.Top = player.Top + 10
End If

'right on stumps
If key = Keys.Right And player.Left > 734 Then
player.Left = player.Left - 10
End If

'down on trees
If key = Keys.Down And player.Top > 431 Then
player.Top = player.Top - 10
End If

'up on tree to left
If key = Keys.Up And player.Left < 58 And player.Top < 289 Then
player.Top = player.Top + 10
End If

'left on path
If player.Left < 0 Then
player.Left = player.Left + 10
End If

End Sub

Public Sub MovePlayer(ByVal player As PictureBox, ByVal key As Long, ByVal parent As Form)

'check for keys
If key = Keys.Up Then
player.Top = player.Top - 10
CharDir = "up"
End If

If key = Keys.Down Then
player.Top = player.Top + 10
CharDir = "down"
End If

If key = Keys.Left Then
player.Left = player.Left - 10
CharDir = "left"
End If

If key = Keys.Right Then
player.Left = player.Left + 10
CharDir = "right"
End If

'transparent
player.Parent = parent

'start animation
If pANI.Enabled = False Then pANI.Enabled = True
currplayerpic = player

End Sub

Public Sub StopMovePlayer(ByVal player As PictureBox)
EnableDoubleBuffering()
'kill animation timer
If pANI.Enabled = True Then pANI.Enabled = False

'reset stand image

Select Case chardir
Case Is = "right"
player.Image = pRight.Image
Case Is = "down"
player.Image = pDown.Image
Case Is = "left"
player.Image = pLeft.Image
Case Is = "up"
player.Image = pUp.Image
End Select

End Sub


Private Sub isJadefortD5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
EnableDoubleBuffering()
End Sub

Private Sub pANI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pANI.Tick
'EnableDoubleBuffering()
Static frame As Integer = 1

Select Case frame
Case Is = 1
If chardir = "right" Then currplayerpic.Image = rightani1.Image
If chardir = "left" Then currplayerpic.Image = leftani1.Image
If chardir = "up" Then currplayerpic.Image = backani1.Image
If chardir = "down" Then currplayerpic.Image = frontani1.Image
EnableDoubleBuffering()
frame = 2
Case Is = 2
If chardir = "right" Then currplayerpic.Image = rightani2.Image
If chardir = "left" Then currplayerpic.Image = leftani2.Image
If chardir = "up" Then currplayerpic.Image = backani2.Image
If chardir = "down" Then currplayerpic.Image = frontani2.Image
EnableDoubleBuffering()
frame = 1
End Select

End Sub
End Class

Now, on my work PC, it does not show any flickering, try it, perhaps you get the same result - if not, I think I may have pinpointed another ( or the same ) problem. :)
HanneSThEGreaT at 2007-11-10 3:30:45 >
# 23 Re: PictureBox Transparent
Thanks for dedicating your time to solving this issue. Hopefully your car troubles have been fixed =). Anyways, my project now has many forms, and on the keydown event and keyup event on each of the many forms (or in game screens), a sub is called in a module that carries out the movement segment of the game. The sub takes in arguments involving the form and player picturebox so that the sub can be used for each form. how can i incorporate your buffering sub into my module so that it can be used universally so I don't need to copy and paste the code into each form.

I tried to do what i am asking of and added a form argument to the sub. I then changed the me.blah to frm.blah and it didn't compile correctly. Any thoughts?
TCanuck42 at 2007-11-10 3:31:42 >
# 24 Re: PictureBox Transparent
No need for the arguments :)

You see, the EnableDoubleBuffering is already Public, meaning it can be accessed everywhere in your program. You could just try a simple line like this :
isJadefortD5.EnableDoubleBuffering()
on the forms, and it should work OK.
I hope it helps !
HanneSThEGreaT at 2007-11-10 3:32:42 >
# 25 Re: PictureBox Transparent
I added the code to my module and it said the "Me" was an invalid identifier in a module. Ignoring that for the moment, in my player moving event in the module, it will not allow me to use the form argument with the enabledoublebuffering sub. If there is no argument, how can the program know which form to act upon? Thanks in advance

============Edit=====================
Do you mean that if one of my forms has the enabledoublebuffering sub and i call it from another form, it will work effectively on others?
TCanuck42 at 2007-11-10 3:33:47 >
# 26 Re: PictureBox Transparent
OK, take a look at this :
Public Sub EnableDoubleBuffering(ByVal CurrentForm As System.Windows.Forms.Form)
' Set the value of the double-buffering style bits to true.
Me.SetStyle(ControlStyles.DoubleBuffer _
Or ControlStyles.UserPaint _
Or ControlStyles.AllPaintingInWmPaint, _
True)
Me.UpdateStyles()
End Sub

Then, the call(s) :
EnableDoubleBuffering(Me)

Is that what you're after
HanneSThEGreaT at 2007-11-10 3:34:53 >
# 27 Re: PictureBox Transparent
Sorry for the confusion. I have played around with the EnableDoubleBuffering sub and I tested it for just one form and movement function to remove the complexity and still, this is a subtle blinking. Earlier you said if this was not the case, you suspected of another cause. Can you please explain? Many thanks
TCanuck42 at 2007-11-10 3:35:48 >
# 28 Re: PictureBox Transparent
Hello again! I totally forgot about this :blush:

It seems there will always be some blinking :( At one stage I thought the results I'm getting was because of my Display cards, but that wasn't the case.
Have you considered using DirectX for smoother animations
HanneSThEGreaT at 2007-11-10 3:36:53 >
# 29 Re: PictureBox Transparent
TCanuck42, I haven't forgotten about you, or this issue. I tried conatcting you through email or PM, but sadly both are disabled.

I did find this very interesting article about flicker free drawing :
http://www.dev-archive.com/Csharp/Csharp/cs_graphics/drawing/article.php/c6137/

Just one drawback, it's in C#. But, the author is making use of various APIs which can also be used through VB as well.

Just thought I'd let you know.. :)
HanneSThEGreaT at 2007-11-10 3:37:54 >
# 30 Re: PictureBox Transparent
Thanks for the continuous posts to help solve this issue. The link you posted proved very useful. I made a work around using the suggested double buffering techniques offered in that article but the blinking hasn't completely vanished. It has reduced so greatly that I have a hard time noticing it, so i feel for now, the problem has been corrected. Thanks again for all of your help! Next time, i definitely will use DirectX protocols for drawing and animation.
TCanuck42 at 2007-11-10 3:38:56 >
# 31 Re: PictureBox Transparent
That's very good news! I'm just glad you got it solved, and heck, I ended up learning some things as well! :)
Good luck with the rest of Adventure Game! :)
HanneSThEGreaT at 2007-11-10 3:39:54 >