Saving and restoring PictureBox - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Saving and restoring PictureBox

CitizenOlekCitizenOlek Posts: 53Member
Hello,

I'm writing a Game in VB 2005. The Game Board is displayed in a PictureBox on the Main Form. Occasionally I will display a graphic over a portion of the Game Board, so I need to save the PictureBox (in Memory), so that it can be restored afterwards. I've read a lot of stuff and tried a lot of different code, but still can't get it functioning 100%. Could someone please give me a small sample code that might do the trick. I do not want to give you the code that I've come up with, it doesn't work anyway, because I'm hoping for a new perspective that might solve the problem. Restoring the PictureBox from a File on disk is not an option.

Just to clarify things, here's the logic:
- There is an image (my Game Board) in the PictureBox.
- Save the PictureBox in memory.
- Display a smaller graphic over some portion of the PictureBox.
- Wait for an Event like a Mouse Click.
- Restore The PictureBox from what was saved earlier.

That's it! Can someone help?
And Thank You Kindly.

Comments

  • CitizenOlekCitizenOlek Posts: 53Member
    Hello again,

    I got tired of waiting for a response so I would like to show some code I developed. I suspect that it was thought that I was too lazy to write the code myself - [b]NOT TRUE[/b]! I just want some help because I'm having problems understanding what is going on! Here is my code, based on a Form 1024 x 768 in size with a PictureBox. You'll need to supply 2 image files, one for the background and one for the DragBox. The code works but I just want someone to check it and make sure I'm not doing something wrong/bad.

    [code]Public Class Form1

    Dim VirtualBoard As Bitmap
    Dim ExtraBoard As Bitmap
    Dim TerrInfoBox As Bitmap
    Dim Gr As Graphics
    Dim Dragging As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load

    PictureBox1.Image = Image.FromFile("GameBoard.jpg")
    VirtualBoard = PictureBox1.Image
    ExtraBoard = PictureBox1.Image
    TerrInfoBox = Image.FromFile("TInfo.gif")
    Dragging = False

    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseDown

    Dim Gr As Graphics = PictureBox1.CreateGraphics
    Gr.DrawImage(TerrInfoBox, e.X, e.Y, TerrInfoBox.Width, _
    TerrInfoBox.Height)
    Gr.Dispose()
    Dragging = True

    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseMove

    If Dragging Then

    VirtualBoard = DirectCast(ExtraBoard.Clone, Bitmap)
    PictureBox1.Image = VirtualBoard
    Dim Gr As Graphics = PictureBox1.CreateGraphics
    Gr.DrawImage(TerrInfoBox, e.X, e.Y, TerrInfoBox.Width, _
    TerrInfoBox.Height)
    Gr.Dispose()

    End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseUp

    VirtualBoard = DirectCast(ExtraBoard.Clone, Bitmap)
    PictureBox1.Image = VirtualBoard
    Dragging = False

    End Sub

    End Class[/code]
  • CitizenOlekCitizenOlek Posts: 53Member
    I finally found a good explanation of the Graphics Object in a Book called "[b]Start-To-Finish Visual Basic 2005[/b]". (I highly recommend this book.)

    "There is always some surface behind the [italic]Graphics[/italic] object, whether it is a portion of the screen, a [italic]Bitmap[/italic] object, or the simulated surface of a printed page. Any drawing that is done to the [italic]Graphics[/italic] object immediately impacts the underlying surface."

    Also the book reminds us to always Dispose of our [italic]Graphics[/italic] objects immediately after use.
Sign In or Register to comment.