[RESOLVED] image layering with transparent gifs question
I'm using VB.NET 2003 and I want to use gif files with transparency on a form. I'm trying to layer it on top of another picture box. If I use a new image box, the background of the gif is transparent, but it shows the grey or beige background of the picture box. I want it to just layer on top of the image under it. I've tried the transparent color in web colors and it doesn't work. I've tried changing it to that form's transparency color, but the I just see the stuff under the form. Any suggestions? Please help. Do I have to use GDI+?
PIctureboxes them selves are not transparent. Transparent controls a re a major point of contention in .Net.
Just use 1 picturebox and use GDI+ code to draw the images in the right order onto the picturebox. Here's a demo (see attachment). The background is passed to the picturebox during the load event so this means you only need to draw the foreground (transparent) gif manually. You can draw as many transparent gifs like this as you want they'll just pile up on top of each other.
Please look at the two images in the Bin folder before you run this demo, then you'll understand whats going on.
The effect works, but I run into another problem. I plan to have a different reaction based upon which layer or item in the picture box the user clicks on. For example, we have a background checkers board and each checkers piece. Different reaction depending on the piece that's clicked. Would I have to make long, drawn out code about the coordinates of the cursor, or is there an easier way? Or should I just scrap it in VB and start learning DirectX and DirectDraw?
On a checkerboard lets say you are using a 8x8 array to represent the board.
If you want to know which square was clicked on just use a little math...
VB Code:
'put this in the mouseUp event of your picturebox...
Horiz = e.x \ (width/8)
Vert = e.y \ (height/8)
Messagebox.Show("You clicked on: Square(" & Horiz & ", " & Vert & ")")
If your picturebox is 300 x 300 pixels and you clicked on (120, 271) then your chosen square would be (3, 7), which is the correct array index for that square.
this is a very rough illustration (not to scale), 3,7 is the chess square of the square that the red dot (click) falls in
Last edited by wossname; Jul 15th, 2005 at 06:00 AM.
It would be 100000000 times harder in DX. There is just so much setting up to be done. To do all that jsut for a little chess game is a bit crazy. VB is easily capable of this.
It would be best to design a ChessPiece class that handles all of the common operations like drawing the piece on the board at its internal coordinates, then for each of your specific types create a derived class...
VB Code:
Class Pawn
Inherits ChessPiece
...
End Class
Class Knight
Inherits ChessPiece
...
End Class
Class Bishop
Inherits ChessPiece
...
End Class
...
...
...
These derived clases can handle all the things specific to each type of piece, like pawns can only move forward and attack diagonally...Knights can only move in a dogleg and so on.
Thank you so much. That makes a lot of sense. Actually, I was just using checkers as an example. I'm trying to develop a computer version of this old viking game. Thank you, though. You answered it perfectly.
Is there a way to take more than one file, in my case gif files, and compile them into a single file that my program can later pick apart? The reason I'm asking is because I would prefer it if my users didn't have the ability to mess with my images before runtime. Plus, it'd be nice because I may end up with 6 images and if I could just reference one file, that'd be better. Or, is there a way to compile the images in with my program without using a picture box? Because I'm using GDI+ instead. Please help.
1. Serialize all your images to 1 binary file. (Means you have to include an extra file wherever your EXE goes)
2. Add your images to your project as "Embedded Resources" (will make your EXE bigger)
Re: [RESOLVED] image layering with transparent gifs question
Onload only happens once at the start of your program, usually before your form shows.
By default, graphics are not persistent, so anything moving in front of your form will erase your previous drawing. If you want persistent drawing either do everything in your paint event or draw to a memory bitmap (only when required) and use the paint event to copy that image to the screen.