CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2007
    Location
    Winnipeg, Manitoba, Canada
    Posts
    47

    Capture portion of PicBox

    Hello,

    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.
    Visual Basic 2005 Express Edition
    Microsoft Net version 2

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Capture portion of PicBox

    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 ¿

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Capture portion of PicBox

    This is what I'm talking about :
    Code:
    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
    Last edited by HanneSThEGreaT; December 19th, 2008 at 03:21 AM.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Capture portion of PicBox

    Quote Originally Posted by CitizenOlek
    <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.

  5. #5
    Join Date
    May 2007
    Location
    Winnipeg, Manitoba, Canada
    Posts
    47

    Re: Capture portion of PicBox

    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.
    Visual Basic 2005 Express Edition
    Microsoft Net version 2

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Capture portion of PicBox

    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_gr...le.php/c14007/

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Capture portion of PicBox

    I was working on something like that, and someone gave me this last night.
    Code:
    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)
    
            g.Clear(Color.Black)
            draggingGraphics.Clear(Color.Black)
    
            '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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    May 2007
    Location
    Winnipeg, Manitoba, Canada
    Posts
    47

    Re: Capture portion of PicBox

    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?
    Visual Basic 2005 Express Edition
    Microsoft Net version 2

  9. #9
    Join Date
    May 2007
    Location
    Winnipeg, Manitoba, Canada
    Posts
    47

    Re: Capture portion of PicBox

    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!
    Visual Basic 2005 Express Edition
    Microsoft Net version 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured