Yeah, that's what I was saying with the Graphics Double Buffer. It's a bit of a pain how CreateGraphics doesn't refresh. Kind of defeats it's own purpose.
However, I hope through this that our friend here has learned the answer to his question!
You can override the Paint event of your form. You should NEVER call me.creategraphics, you should always handle the paint event, and do your drawing there. If you handle the paint event, then you can draw the image transparently there.
Private Sub OnPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
End Sub
That's a paint handler. Now e.Graphics is what you want to use to do your drawing.
I should add - if you change what you're drawing, Invalidate() is the call to force a paint event.
What's at issue here is that if you draw anything outside the paint event, it's not preserved.
Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim r As Rectangle = New Rectangle(10, 10, 20, 20)
Me.CreateGraphics().FillRectangle(Brushes.Red, r)
End Sub
Put that in a form. Now click on it. Now, drag another form over your form and back . Your red box is gone. The reason is, the paint event is fired to redraw the form, and it does NOT draw the box, you have to click to fire that code again. So, that is why you NEVER paint outside of hte paint event.
Here, let me simplify this: I want PictureBox1 (the panda picture with the pink background) to have the pink in it invisible or transparent or whatever you call it.
All I am really doing is just making a game where you use the controls to move around, i have pictures for the character facing different ways and i just want the background to be pink.
Dim TempBitmap As Bitmap TempBitmap = Bitmap.FromFile("C:\Panda.Bmp") TempBitmap.MakeTransparent(Color.FromArgb(255, 0, 255)) Me.CreateGraphics.DrawImage(TempBitmap, 0, 0)
If the code above actually works, how do i get TempBitmap to be PictureBox1 for the code:
If e.KeyCode = Keys.Down Then PictureBox1.Size = New System.Drawing.Size(14, 17) PictureBox1.Image = Darkeyes.My.Resources.panda_down PictureBox1.Top = PictureBox1.Top + 16 ElseIf e.KeyCode = Keys.Up Then PictureBox1.Size = New System.Drawing.Size(14, 18) PictureBox1.Image = Darkeyes.My.Resources.panda_up PictureBox1.Top = PictureBox1.Top - 16 ElseIf e.KeyCode = Keys.Left Then PictureBox1.Size = New System.Drawing.Size(24, 17) PictureBox1.Image = Darkeyes.My.Resources.panda_left PictureBox1.Left = PictureBox1.Left - 16 ElseIf e.KeyCode = Keys.Right Then PictureBox1.Size = New System.Drawing.Size(24, 17) PictureBox1.Image = Darkeyes.My.Resources.panda_right PictureBox1.Left = PictureBox1.Left + 16 End If
I really want to make this game but I am stuck behind so many road blocks because of my inexperience at VB .NET I almost want to say "forget the whole thing!".
But I would at least like to try to take a shot at it.
I ran into the same program you're having. The EXACT same problem.... Well.... I didn't have a pink panda.... But still....
The SHORT answer to your question is that you can't have transparency in a picturebox control without doing some pretty complex coding. If you want the contents of a picturebox to be transferred into an image object, you can just go:
Dim I as new Image I = Picturebox1.image
or something...
But, when I had your problem, I learned how to use the CreateGraphics.DrawImage method and drew things with that instead of with pictureboxes.
I KNOW that cgraus says it's not advisable and i will tell you now that its not the proper way you should do it. But come on, man, this is your first project. HAVE SOME FUN! Using this method will give you a fantastic foothold of knowledge to lift you up and get you learning about graphics coding.
If you need any more specific help, just email me and I will be happy to sort you out :-)
I can see where you're coming from, but I wouldn't go so far as to say that you should never call the me.creategraphics method. Sure, it's system intensive, but that's only in an environment where it will be re-used again and again, as in a timer's tick event or something.
Another option would be to create a graphics double buffer, draw his panda in that using the coding method from my other post, and re-draw the buffer into place. This would be less memory intensive, but also refresh itself and not flicker at all.
As far as I can see, it's useful for drawing on TOP of the form, for example to draw a rubber band. Even then, I don't use it, because my background is C++, I'm used to handling stuff like that in my paint handler. However, the absence of an XOR does pose problems which this can solve.
But at the end of the day, a windows app should do all drawing in a paint handler.
I think you should be able to create a Bitmap object, store the contents of the imagebox in the Bitmap object, then use Bitmap.Maketransparent to get rid of the pink colour. If you can't do this in the picture box, try using the same method, but using the form.creategraphics.drawimage method to draw the picture instead of using a picture box. Hope this helps!
Making a picturebox have a transparent background
ScottyO
Yeah, that's what I was saying with the Graphics Double Buffer. It's a bit of a pain how CreateGraphics doesn't refresh. Kind of defeats it's own purpose.
However, I hope through this that our friend here has learned the answer to his question!
Skye_
You can override the Paint event of your form. You should NEVER call me.creategraphics, you should always handle the paint event, and do your drawing there. If you handle the paint event, then you can draw the image transparently there.
Private Sub OnPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint End SubThat's a paint handler. Now e.Graphics is what you want to use to do your drawing.
I should add - if you change what you're drawing, Invalidate() is the call to force a paint event.
MarceloTallis
What's at issue here is that if you draw anything outside the paint event, it's not preserved.
Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click Dim r As Rectangle = New Rectangle(10, 10, 20, 20) Me.CreateGraphics().FillRectangle(Brushes.Red, r) End SubPut that in a form. Now click on it. Now, drag another form over your form and back . Your red box is gone. The reason is, the paint event is fired to redraw the form, and it does NOT draw the box, you have to click to fire that code again. So, that is why you NEVER paint outside of hte paint event.
Chester03
velt
PLEASE HELP ME!
hakel
Here's some code to show what I'm saying...
Dim TempBitmap As Bitmap
TempBitmap = Bitmap.FromFile("C:\Panda.Bmp")
TempBitmap.MakeTransparent(Color.FromArgb(255, 0, 255))
Me.CreateGraphics.DrawImage(TempBitmap, 0, 0)
chuck620
You can NOT write a game using pictureboxes. You need to follow the advice you've been given.
Martin Flores
You're probably right, a game is not a good choice for a first project, you should take some smaller steps first/.
Pirringer
Dim TempBitmap As Bitmap
TempBitmap = Bitmap.FromFile("C:\Panda.Bmp")
TempBitmap.MakeTransparent(Color.FromArgb(255, 0, 255))
Me.CreateGraphics.DrawImage(TempBitmap, 0, 0)
If the code above actually works, how do i get TempBitmap to be PictureBox1 for the code:
If e.KeyCode = Keys.Down Then
PictureBox1.Size = New System.Drawing.Size(14, 17)
PictureBox1.Image = Darkeyes.My.Resources.panda_down
PictureBox1.Top = PictureBox1.Top + 16
ElseIf e.KeyCode = Keys.Up Then
PictureBox1.Size = New System.Drawing.Size(14, 18)
PictureBox1.Image = Darkeyes.My.Resources.panda_up
PictureBox1.Top = PictureBox1.Top - 16
ElseIf e.KeyCode = Keys.Left Then
PictureBox1.Size = New System.Drawing.Size(24, 17)
PictureBox1.Image = Darkeyes.My.Resources.panda_left
PictureBox1.Left = PictureBox1.Left - 16
ElseIf e.KeyCode = Keys.Right Then
PictureBox1.Size = New System.Drawing.Size(24, 17)
PictureBox1.Image = Darkeyes.My.Resources.panda_right
PictureBox1.Left = PictureBox1.Left + 16
End If
I really want to make this game but I am stuck behind so many road blocks because of my inexperience at VB .NET I almost want to say "forget the whole thing!".
But I would at least like to try to take a shot at it.
binks120
Never say never, my friend!
I ran into the same program you're having. The EXACT same problem.... Well.... I didn't have a pink panda.... But still....
The SHORT answer to your question is that you can't have transparency in a picturebox control without doing some pretty complex coding. If you want the contents of a picturebox to be transferred into an image object, you can just go:
Dim I as new Image
I = Picturebox1.image
or something...
But, when I had your problem, I learned how to use the CreateGraphics.DrawImage method and drew things with that instead of with pictureboxes.
I KNOW that cgraus says it's not advisable and i will tell you now that its not the proper way you should do it. But come on, man, this is your first project. HAVE SOME FUN! Using this method will give you a fantastic foothold of knowledge to lift you up and get you learning about graphics coding.
If you need any more specific help, just email me and I will be happy to sort you out :-)
My email is alex.norton@unitab.com.au
It's your first project. Don't give up! This is programming! The ONLY way to learn is to experiment and make mistakes!
Dominic Riccetti
I can see where you're coming from, but I wouldn't go so far as to say that you should never call the me.creategraphics method. Sure, it's system intensive, but that's only in an environment where it will be re-used again and again, as in a timer's tick event or something.
Another option would be to create a graphics double buffer, draw his panda in that using the coding method from my other post, and re-draw the buffer into place. This would be less memory intensive, but also refresh itself and not flicker at all.
twiztar
As far as I can see, it's useful for drawing on TOP of the form, for example to draw a rubber band. Even then, I don't use it, because my background is C++, I'm used to handling stuff like that in my paint handler. However, the absence of an XOR does pose problems which this can solve.
But at the end of the day, a windows app should do all drawing in a paint handler.
NewC#
a_pess
Draw the image yourself in a paint handler, and use the imageattributes class to make 255/0/255 ( magenta ) transparent.