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?
Printable View
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?
what do you have 2 pictureboxes over eachother?
Yup, thats how I have it now.
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.
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?
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?
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?
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
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
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.
.NET user controls don't have those properties
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.
Thanks a lot!! Look forward to see what you come up with...
Here is my try at it
I created a usercontrolPut it on a form with a picturebox and called it fishVB Code:
Public Class UserControl1 Dim Draging As Boolean Dim Xpos As Single Dim Ypos As Single Dim myBitMap As Bitmap Public Sub New() InitializeComponent() myBitMap = New Bitmap("test.gif") myBitMap.MakeTransparent(Color.White) DoubleBuffered = True Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle SetStyle(ControlStyles.SupportsTransparentBackColor, True) Me.BackColor = Color.Transparent End Sub Private Sub UserControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If Draging = False Then Draging = True Xpos = e.X Ypos = e.Y End If End Sub Private Sub UserControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If Draging = True Then Me.Left += (e.X - Xpos) Me.Top += (e.Y - Ypos) End If End Sub Private Sub UserControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp Draging = False End Sub Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint e.Graphics.DrawImage(myBitMap, 0, 0) End Sub End ClassYou 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.VB Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load PictureBox1.Image = Image.FromFile("Backgnd.gif") Fish.Parent = PictureBox1 End Sub End Class
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 imagesQuote:
Originally Posted by moeur
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 lineAlso, change the Usercontrol Paint event to the followingVB Code:
SetStyle(ControlStyles.AllPaintingInWmPaint, True)Try it with and without the Me.Parent.Refresh() line and see which one you like better.VB Code:
If Draging = True Then Me.Left += (e.X - Xpos) Me.Top += (e.Y - Ypos) Me.Refresh() Me.Parent.Refresh() End If
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.Edit: Just noticed that if you have two sprites and they overlap then back sprite does not show through transparent background of front sprite???VB Code:
'put it into a container myPicturebox.Parent = bkgndPictureBox 'set its background color to be transparent myPictureBox.BackColor = Color.Transparent