I'm writing a game in VB 2005. The Game Board is a Bitmap displayed in a PictureBox within the Main Form. Occassionally I want to display a Message or a Graphic onto the Game Board, but I need a way to save and restore what was originally there. I've come across this:
<code>
Dim RectCopy As New Rectangle(X, Y, sizeX, sizeY)
<end code>
But I believe this only works to capture the Windows Form background, whereas I need to capture the PictureBox background.
HanneSThEGreaT
April 8th, 2008, 06:09 AM
Can't you just save the background to a separate image, in other words, an in memory bitmap, then once done, revert back to that ¿
Another option would be to clone the current image ( that would make an in memory bmp ), then, once you're done, change back to it ¿
HanneSThEGreaT
April 8th, 2008, 06:27 AM
This is what I'm talking about :
Public Class Form1
Dim bmpTemp As New Bitmap(172, 190) 'same size as picbox bmp
Dim tGrfx As Graphics = Graphics.FromImage(bmpTemp) 'graphics object
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bmpTemp = PictureBox1.Image 'store image
PictureBox1.CreateGraphics().DrawLine(Pens.Azure, 20, 20, PictureBox1.Width - 20, 20) 'do something, in this case, draw a line
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PictureBox1.Image = bmpTemp 'reset picturebox pic
End Sub
End Class
Here, I'm just temporarily saving the image. After I did some processing, I changed it back.
I'm including the project with.
I hope it helps, it should ;)
HanneSThEGreaT
April 8th, 2008, 06:29 AM
<code>
Dim RectCopy As New Rectangle(X, Y, sizeX, sizeY)
<end code>
But I believe this only works to capture the Windows Form background, whereas I need to capture the PictureBox background.
Not really. that is just creating a rectangle object. This would perhaps have been something to do with a selection rectangle. But, using this way, you'd still need to copy the part of the pciture into that rectangle, then, manipulate the rectangle independantly - similar to what I've done in Post # 2, eventhough I haven't used a rectangle ther, because you need the entire picture.
CitizenOlek
April 8th, 2008, 11:43 AM
I am already familiar with saving and retrieving the entire Bitmap.
I thought though, that it would be more efficient and faster to merely save and retrieve the portion required. Do you think this is possible?
I am dealing with large amount of data, the PicBox is 1016 x 717.
HanneSThEGreaT
April 8th, 2008, 11:48 AM
Oh, sorry about that, I guess I need glasses.
I tell you what, have a look at this article ( it enables you to cut out a certain area of a picture, then displays it in another box ) - you could use the same principles, as discussed in there :
http://www.codeguru.com/vb/gen/vb_graphics/gdi/article.php/c14007/
dglienna
April 8th, 2008, 11:50 AM
I was working on something like that, and someone gave me this last night.
Public Class Form1
Dim canvas As Bitmap
Dim draggingCanvas As Bitmap
Dim g As Graphics
Dim draggingGraphics As Graphics
Dim isDragging As Boolean = False
Dim dragLoc As Point
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'set basic properties
Me.Size = New Size(800, 600)
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
canvas = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
draggingCanvas = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
g = Graphics.FromImage(canvas)
draggingGraphics = Graphics.FromImage(draggingCanvas)
'Me.BackColor = Color.Black
Me.DoubleBuffered = True
MessageBox.Show("Click and drag to create rectangles.")
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
'if left button is clicked, set the dragging var to true
isDragging = True
dragLoc = e.Location
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If isDragging Then
draggingGraphics.DrawImageUnscaled(canvas, 0, 0)
draggingGraphics.DrawRectangle(Pens.White, IIf(e.X > dragLoc.X, dragLoc.X, e.X), IIf(e.Y > dragLoc.Y, dragLoc.Y, e.Y), Math.Abs(dragLoc.X - e.X) - 1, Math.Abs(dragLoc.Y - e.Y) - 1)
End If
Me.Refresh()
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
isDragging = False
draggingGraphics.DrawRectangle(Pens.DarkBlue, IIf(e.X > dragLoc.X, dragLoc.X, e.X), IIf(e.Y > dragLoc.Y, dragLoc.Y, e.Y), Math.Abs(dragLoc.X - e.X), Math.Abs(dragLoc.Y - e.Y))
draggingGraphics.FillRectangle(Brushes.Blue, IIf(e.X > dragLoc.X, dragLoc.X + 1, e.X + 1), IIf(e.Y > dragLoc.Y, dragLoc.Y + 1, e.Y + 1), Math.Abs(dragLoc.X - e.X) - 1, Math.Abs(dragLoc.Y - e.Y) - 1)
g.DrawImageUnscaled(draggingCanvas, 0, 0)
Me.Refresh()
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImageUnscaled(canvas, 0, 0)
e.Graphics.DrawImageUnscaled(draggingCanvas, 0, 0)
End Sub
End Class
You always draw the box on the backbuffer, and then copy it to the front
CitizenOlek
April 8th, 2008, 12:29 PM
I just tried the code posted by dglienna and it works!
I used a background picture and commented out the lines:
'g.Clear(Color.Black)
'draggingGraphics.Clear(Color.Black)
and that worked too.
I'm still learning and will need to study the code to understand it.
This will definitely do what I want.
I noticed it is very slow! You can see multiply images of the dragged Box, which disappear when you stop. Is this just a limitation of VB?
CitizenOlek
April 8th, 2008, 11:53 PM
The above code simply uses the built in Message Box.
The background is automatically restored under that.
That doesn't help in trying to capture and restore that background of a PictureBox beneath a Graphic!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.