Rudi van Vuuren
Rudi van Vuuren

Reputation: 21

Picturebox's image is nothing even though Clipboard.ContainsImage = True?

I am making a program that constantly sends the key "{PRTSC}" and then sets PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage.

At first it works fine but after a min or two the picturebox goes blank and no error is given.

My code is:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If Not My.Computer.Clipboard.ContainsImage Then
        SendKeys.Send("{PRTSC}")
    Else
        PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage
        My.Computer.Clipboard.Clear()
    End If
End Sub

I have tried:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    'SendKeys.Send("{PRTSC}")
    'If My.Computer.Clipboard.ContainsImage Then PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage

    Dim bounds As New Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height) ', System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    PictureBox1.BackgroundImage = screenshot
    graph.Dispose()
    'screenshot.Save("d:\\dcap.jpg", Imaging.ImageFormat.Bmp)

End Sub

But attempting to dispose the screenshot yields an instant error. I don't know why.

Upvotes: 1

Views: 712

Answers (1)

xpda
xpda

Reputation: 15813

  1. {PRTSC} grabs the active window, when it has focus, and the screen otherwise.

  2. It's a good idea to disable the timer at the beginning of the tick event, and start it at the end. This prevents re-entry, and, depending on the type of timer (there is the timer control, system.timers.timer, and system.threading.timer, each of which is a little different), you may be required to restart the timer each tick event.

  3. It's normal to assign an image to a picturebox image instead of the backgroundimage. If something in the application is assigning a bitmap to picturebox1.image or blanking picturebox1.image, it will overwrite the screen shot in picturebox1.backgroundimage.

Upvotes: 0

Related Questions