# VBForums CodeBank > Codebank - Game Programming >  GDI+ - Scrolling Background

## dday9

So you may have seen my XNA - Scrolling Background, well here is the GDI+ version.



```
Option Strict On
Option Explicit On
Public Class Form1
    Private bgImage1 As Bitmap
    Private bgImage2 As Bitmap
    Private xPosition As Integer
    Private scrolling As Boolean
    Private scrollingThread As Threading.Thread

    Private Sub ResetGlobals()
        xPosition = 0
        scrolling = False
        scrollingThread = New Threading.Thread(AddressOf ScrollLoop)
        Me.SplitBackground()
    End Sub

    Private Sub SplitBackground()
        'The background1 will be the left half of the image and background2 will be the right half
        bgImage1 = My.Resources.gamebackground
        bgImage2 = My.Resources.gamebackground
    End Sub

    Private Sub ScrollLoop()
        'Update -> Draw -> Loop

        Dim s As Stopwatch = New Stopwatch
        s.Start()

        'Update
        Me.UpdateBackground()

        'Draw
        Me.DrawBackground()

        'Loop
        While s.Elapsed <= TimeSpan.FromMilliseconds(16.6) '60 FPS
            Application.DoEvents()
        End While

        'Stop the watch
        s.Stop()

        'Display FPS
        Console.WriteLine("Frames Per Second: " & (1000 / s.ElapsedMilliseconds))

        'Continue if applicable
        If scrolling Then
            scrollingThread = New Threading.Thread(AddressOf ScrollLoop)
            scrollingThread.Start()
        End If
    End Sub

    Private Sub UpdateBackground()
        'Move the x position to the left by 1 until we've scrolled to the width of the background
        xPosition = If(xPosition > -Me.ClientSize.Width, xPosition - 1, 0)
    End Sub

    Private Sub DrawBackground()
        'Refresh the form if the handle is still available
        If scrolling Then
            Me.BeginInvoke(Sub() RefreshForm())
        End If
    End Sub

    Private Sub RefreshForm()
        'Refresh
        Me.Refresh()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Be sure to stop scrolling if applicable
        scrolling = False
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'If the space key is hit then start scrolling
        'If the escape key is hit the stop scrolling
        If e.KeyCode = Keys.Space AndAlso Not scrolling Then
            scrolling = True
            scrollingThread.Start()
        ElseIf e.KeyCode = Keys.Escape AndAlso scrolling Then
            scrolling = False
        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawImage(bgImage1, New Rectangle(xPosition, 0, Me.ClientSize.Width, Me.ClientSize.Height))
        e.Graphics.DrawImage(bgImage2, New Rectangle(xPosition + Me.ClientSize.Width, 0, Me.ClientSize.Width, Me.ClientSize.Height))
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Reset the globals
        Me.ResetGlobals()

        'Set the form's properties
        With Me
            .DoubleBuffered = True
            .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
            .KeyPreview = True
            .ClientSize = New Size(bgImage1.Size.Width \ 2, bgImage1.Size.Height)
            .Text = "GDI+ Scrolling Background Example"
        End With

    End Sub

    
End Class
```

This is the image that I use which I've named gamebackground and is located in My.Resources:

----------


## NANOAddicts

Hi 

Where can I download My.Resources?

Cheers

----------


## dday9

You do not download My.Resources, instead you upload any resources(documents, images, etc.) into My.Resources and then you get the resource when needed. Take a look at this link on How To: Add or Remove Resources.

----------


## NANOAddicts

A my bad.

I should've taken the time to ask where I can get a copy of the background you are using. My apologies

Thanks

----------


## kfcSmitty

His gamebackground is the picture in the first post. If you right click it and save as, you should then be able to add it to My.Resources in your project.

----------

