2 Pics Transparant with each other

I was wondering how to make two pictures transparant with each other.

In other words when one picture is slightly over the other how do you make the top one transparant so you can see the other one as well, Instead of seeing it being blocked out.

I am working on a game and when you move the character around I don't want every object to appear as a box. I am using .gifs because of their transparancy. I have made the .gifs transparant and the picture boxes background color transparant, but everything is still a box

Thanks,

White Hawk



Answer this question

2 Pics Transparant with each other

  • Marco Bergonzini

    You are not going to make a PictureBox truly transparent.

    You should be using the Paint method to draw your characters, rather than having several picturebox's moving around.

    It may seem more complicated, but once you have the basics set up, it really isn't.


  • say_2000

    Yes, paint the graphics on the form using it's Paint method.

    The paint method gives you a Graphics object to work with, and since you didn't create it, you should not dispose it.

    Take a look at Bob Powells site for further info: http://www.bobpowell.net/faqmain.htm

    The following is a very basic example to point you in the right direction (Assumes VB2005).

    Public Class Form1
    
      Private BackGround As Bitmap
    
      Private MyRandom As New Random
    
      Private MySprite As Bitmap
      Private MySpriteLocation As Point
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateBackBrush()
        MySprite = SystemIcons.Exclamation.ToBitmap
        MySpriteLocation = New Point(-MySprite.Width, MyRandom.Next(Me.ClientSize.Height - MySprite.Height))
        MyBase.DoubleBuffered = True
        Me.ResizeRedraw = True
      End Sub
    
      Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        CreateBackBrush()
      End Sub
    
      Private Sub Form_Paint(ByVal Sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        DrawSurface(e.Graphics)
      End Sub
    
      Private Sub CreateBackBrush()
        If BackGround IsNot Nothing Then BackGround.Dispose()
        BackGround = New Bitmap(Me.Width, Me.Height, Imaging.PixelFormat.Format32bppPArgb)
        Dim g As Graphics = Graphics.FromImage(BackGround)
        Dim fillBrush As New System.Drawing.Drawing2D.LinearGradientBrush(Me.ClientRectangle, Color.AliceBlue, Color.Blue, Drawing2D.LinearGradientMode.ForwardDiagonal)
        g.FillRectangle(fillBrush, 0, 0, BackGround.Width, BackGround.Height)
        fillBrush.Dispose()
        g.Dispose()
      End Sub
    
      Private Sub DrawSurface(ByVal g As Graphics)
        If BackGround Is Nothing Then Return
        If MySprite Is Nothing Then Return
        g.DrawImage(BackGround, Point.Empty)
        MySpriteLocation.Offset(1, 0)
        If MySpriteLocation.X > Me.Width Then
          MySpriteLocation = New Point(-MySprite.Width, MyRandom.Next(Me.ClientSize.Height - MySprite.Height))
        End If
        g.DrawImage(MySprite, MySpriteLocation)
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Invalidate()
      End Sub
    
    End Class
    

    Although I stated previously that you were not going to make a PictureBox transparent, obviously it is possible, but you will encounter other problems such as them painting in the wrong order when overlapped (that is assuming you've used the WS_EX_TRANSPARENT method).


  • Zuchman

    I figured out an easy alternative to this problem, by grabbing both pictures and combining them into one picture. However, there has to be an easier method than this.

    Does anyone know The previous method gets more complicated if used in my game I am programming.

    Thanks,

    White Hawk


  • Alan Robbins

    So I would paint the graphics on the form, moving and animating them as needed I also would need to dispose the graphic before I paint the next frame, correct So the paint method would run smoother then using pictureboxes

    As for the picturebox problem I made them transparant with some extra code.

    Thankyou,

    White Hawk


  • Pomo

    Thank you I appreciate it,

    White Hawk


  • 2 Pics Transparant with each other