none
Apply a background to game map using a picture box

    Question

  • Afternoon, I decided to take a break from the last thread. 

    I want to create a terrain for this map which is a "plain land" so what I want to do is apply a picturebox which acts as a background to the game map for example snow, the four pictureboxes you see on the screen I still want them to be visible but the rest of the game grid I just want it to be hidden and make it a terrain.

    I would like to do this with three terrains in particular such as one for snow, grass and sand. 

    Is there a simple way in doing this? I am open to all solutions. 


    WRA

    Wednesday, April 12, 2017 3:58 PM

Answers

  • This is Tommys answer which I believe is concurrent to answering another question on my thread

    There are several ways. Here is one with a special picturebox class that allows a transparent color background. In this example I used black. But something like magenta is better then less likely to occur in the original image. ie all black is transparent.

    Public Class Form7
        Private WithEvents Picturebox1 As New TransparentPictureBox With {.Parent = Me,
            .Location = New Point(100, 100), .Size = New Size(100, 100)}
        Private tankBmp As Bitmap = New Bitmap("c:\bitmaps\tiles\tank 3.png")
        Private bgBmp As Bitmap = New Bitmap("C: \bitmaps\landscape\textures\landscape pattern lime green grass.jpg")
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Picturebox1.BackColor = Color.Transparent
            Picturebox1.ClientSize = New Size(100, 100)
            Picturebox1.BackgroundImageLayout = ImageLayout.Zoom
    
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            'attribs setting for transparent using range of color 0 - 25 for shades of black
            Dim attribs As New System.Drawing.Imaging.ImageAttributes
            attribs.SetColorKey(Color.FromArgb(0, 0, 0), Color.FromArgb(25, 25, 25))
    
            'draw pic with transparent color
            Dim dst As New Rectangle(0, 0, Picturebox1.Width, Picturebox1.Height)
            e.Graphics.DrawImage(tankBmp, dst, 0, 0, tankBmp.Width, tankBmp.Height, GraphicsUnit.Pixel, attribs)
    
        End Sub
    
        Private Sub Form7_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            'draw the background image
            e.Graphics.DrawImage(bgBmp, ClientRectangle)
    
        End Sub
    
        Private Class TransparentPictureBox
            'use to draw transparent animated gifs
            Inherits PictureBox
    
            Private Shared ReadOnly DefaultBackground As Color = Color.Transparent
    
            Public Sub New()
                Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
                BackColor = DefaultBackground
            End Sub
        End Class
    End Class

    PS The special class allows the picturebox to be transparent. So if you use that picturebox class you can use a transparent image and that's all you need. ie an image like the tank I show the black would be transparent.  However

    However, the example also produces a transparent image from a color. In the example the black part of the original tank image is made transparent and shows through to the picturebox background. Since the picturebox background is transparent in our special class we see through the picuturebox to the form.

    So you can use any image and set any transparent color range for the image. As I say the transparent color will come through any part of the image so if you use a color like black then all black lines etc become transparent. So you need a color that is not in your image.


    WRA

    • Marked as answer by Waliur Rahman Sunday, April 16, 2017 5:29 PM
    Saturday, April 15, 2017 5:36 PM

All replies

  • You know you can also set the form background 


    Success
    Cor

    Wednesday, April 12, 2017 4:53 PM
  • I didn't know about that but what If I wanted to make it a case. For example with my project it is determined by the windows form called game menu. I will create an if statement where it will say selected item is snow for instance then in the map it will import the background of snow and send it to the back (send to back feature idk how to do that part) 

    Also, would your example possibly be able to carry out 3 pictures and import 1 depending on the selected item?


    WRA

    Wednesday, April 12, 2017 5:00 PM
  • Therefore you have more options. 

    You simply can change the images, or you can change parts of the image. 

    If you do this quick in a timer you even get an animated image view.


    Success
    Cor

    Wednesday, April 12, 2017 5:36 PM
  • Do you mean create a loop to hold all three backgrounds and depending on which is selected from the combobox that is the one which will display?

    Btw I don't want it to be an animation noooo.


    WRA

    Wednesday, April 12, 2017 5:56 PM
  • Yes,

    However use then an imagelist as it is native to the forms

    https://msdn.microsoft.com/en-us/library/system.windows.forms.imagelist(v=vs.110).aspx


    Success
    Cor

    Wednesday, April 12, 2017 6:01 PM
  • Morning cor, i have through my form_paint feature set the background, would I have to change that so the background chosen would workk. Ill check your link given it should help i hope

    WRA

    Thursday, April 13, 2017 9:13 AM
  • Afternoon Cor I think the following might help you to help me answer the question of what I should do.

     Private Sub Plain_Turf_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.Gray)
                .FillRectangle(Brushes.LightGray, GridRect)
    
                Using p1 As New Pen(Color.Black, 1)
                    For x = GridRect.X To GridRect.X + GridRect.Width Step GridSize.Width
                        .DrawLine(p1, x, GridRect.Y, x, GridRect.Y + GridRect.Height)
                    Next
                    For y = GridRect.Y To GridRect.Y + GridRect.Height Step GridSize.Height
                        .DrawLine(p1, GridRect.X, y, GridRect.X + GridRect.Width, y)
                    Next
                End Using
            End With
        End Sub
    end class

    This designs the form background so I presume something from here has to be adjusted. 


    WRA

    Saturday, April 15, 2017 11:29 AM
  • This is Tommys answer which I believe is concurrent to answering another question on my thread

    There are several ways. Here is one with a special picturebox class that allows a transparent color background. In this example I used black. But something like magenta is better then less likely to occur in the original image. ie all black is transparent.

    Public Class Form7
        Private WithEvents Picturebox1 As New TransparentPictureBox With {.Parent = Me,
            .Location = New Point(100, 100), .Size = New Size(100, 100)}
        Private tankBmp As Bitmap = New Bitmap("c:\bitmaps\tiles\tank 3.png")
        Private bgBmp As Bitmap = New Bitmap("C: \bitmaps\landscape\textures\landscape pattern lime green grass.jpg")
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Picturebox1.BackColor = Color.Transparent
            Picturebox1.ClientSize = New Size(100, 100)
            Picturebox1.BackgroundImageLayout = ImageLayout.Zoom
    
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            'attribs setting for transparent using range of color 0 - 25 for shades of black
            Dim attribs As New System.Drawing.Imaging.ImageAttributes
            attribs.SetColorKey(Color.FromArgb(0, 0, 0), Color.FromArgb(25, 25, 25))
    
            'draw pic with transparent color
            Dim dst As New Rectangle(0, 0, Picturebox1.Width, Picturebox1.Height)
            e.Graphics.DrawImage(tankBmp, dst, 0, 0, tankBmp.Width, tankBmp.Height, GraphicsUnit.Pixel, attribs)
    
        End Sub
    
        Private Sub Form7_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            'draw the background image
            e.Graphics.DrawImage(bgBmp, ClientRectangle)
    
        End Sub
    
        Private Class TransparentPictureBox
            'use to draw transparent animated gifs
            Inherits PictureBox
    
            Private Shared ReadOnly DefaultBackground As Color = Color.Transparent
    
            Public Sub New()
                Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
                BackColor = DefaultBackground
            End Sub
        End Class
    End Class

    PS The special class allows the picturebox to be transparent. So if you use that picturebox class you can use a transparent image and that's all you need. ie an image like the tank I show the black would be transparent.  However

    However, the example also produces a transparent image from a color. In the example the black part of the original tank image is made transparent and shows through to the picturebox background. Since the picturebox background is transparent in our special class we see through the picuturebox to the form.

    So you can use any image and set any transparent color range for the image. As I say the transparent color will come through any part of the image so if you use a color like black then all black lines etc become transparent. So you need a color that is not in your image.


    WRA

    • Marked as answer by Waliur Rahman Sunday, April 16, 2017 5:29 PM
    Saturday, April 15, 2017 5:36 PM
  • The question I have for you Tommy is, how would I use the background to set a distance by grid reference? I will be making a map called Bridge wars so I want to set the grass terrain for instance from grid reference 0 across to 10 across then inbetween 10 across to 15 across will be streams of water and a bridge which the tanks will have to cross then 15 across to 25 across will be more grass. 

    In a nutshell how will I set how far on my windows form the picture be? 

    The second part to it I believe is how can I prevent the sprites from crossing the water? 


    WRA


    Saturday, April 15, 2017 5:39 PM