Results 1 to 18 of 18

Thread: [RESOLVED] image layering with transparent gifs question

  1. #1

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Resolved [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+?

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    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.
    Attached Files Attached Files
    I don't live here any more.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    GDI+ is your friend, don't be afraid of it
    I don't live here any more.

  4. #4

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    Thanks. It works great!

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    Your welcome.
    I don't live here any more.

  6. #6

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    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?

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    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:
    1. 'put this in the mouseUp event of your picturebox...
    2. Horiz = e.x \ (width/8)
    3. Vert = e.y \ (height/8)
    4.  
    5. 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
    Attached Images Attached Images  
    Last edited by wossname; Jul 15th, 2005 at 06:00 AM.
    I don't live here any more.

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    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:
    1. Class Pawn
    2. Inherits ChessPiece
    3. ...
    4. End Class
    5.  
    6. Class Knight
    7. Inherits ChessPiece
    8. ...
    9. End Class
    10.  
    11. Class Bishop
    12. Inherits ChessPiece
    13. ...
    14. End Class
    15.  
    16.  
    17. ...
    18. ...
    19. ...

    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.
    I don't live here any more.

  9. #9

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    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.

  10. #10

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    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.

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    You could do this in at least 2 ways...

    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)
    I don't live here any more.

  12. #12

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    I'd prefer to include them in the exe, but once I change them to embedded resources, how do I reference them in the code?

  13. #13
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    Look in MSDN for the Bitmap Constructor that lets you load from a resource.

    (ie,. Search for "Bitmap Constructor" and then look down the list of overloads until you see a link to the one that handles resources)

    I don't live here any more.

  14. #14

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    In the original example code, I have a question about a subroutine. The part I'm referring to is equivalent to this:

    VB Code:
    1. Private Sub Field_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Field.Paint
    2.         e.Graphics.DrawImage(_Board, 0, 0)
    3.         e.Graphics.DrawImage(_Blue, 534, 281)
    4.         e.Graphics.DrawImage(_Red, 483, 27)
    5.     End Sub

    In this example, "Field" is a picture box and "_Board", "_Blue", and "_Red" are all seperate images.

    Is there a way that I can have such events not happen on load and instead occur when a button is clicked or similar event?

    I'm still coping with the image controls for my program and I haven't even started working with mouseUp events.

    Thank you so much for your helpful code.
    Last edited by bluej774; Jul 18th, 2005 at 04:13 AM.

  15. #15

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    Bumpity bumpity bump bump...
    [Sorry.]

  16. #16
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: image layering with transparent gifs question

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.     Dim temp As Graphics = Field.CreateGraphics
    4.     temp.DrawImage(_Board, 0, 0)
    5.     temp.DrawImage(_Blue, 534, 281)
    6.     temp.DrawImage(_Red, 483, 27)
    7.  
    8. End Sub

    You just move the cod into a button and use a reference to that picturebox's graphics object (because no PaintEventArgs object is available).
    I don't live here any more.

  17. #17

    Thread Starter
    Lively Member bluej774's Avatar
    Join Date
    Jul 2005
    Posts
    70

    Re: image layering with transparent gifs question

    That isn't working for me.

    Edit: Nevermind. It only doesn't work when I have it occur onLoad.
    Just out of curiousity, why not?
    Last edited by bluej774; Jul 24th, 2005 at 02:39 AM.

  18. #18
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.
    I don't live here any more.

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