1

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81

1 Answers1

0
  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.

xpda
  • 15,585
  • 8
  • 51
  • 82
  • So I should set Timer1.Enable to False, then True after code has run between Enable and Disable? And even though I set image instead of background image, the program set the image for 30sec or so then the picturebox's image is blank. – Rudi van Vuuren Feb 21 '15 at 10:22
  • The tick is set to 1 and the timer starts after Form1.Paint, but if all fails, how can I make a screen recorder that will be streamed in a picture box? – Rudi van Vuuren Feb 21 '15 at 10:23
  • And you said there are two types of timers, what should be used here? – Rudi van Vuuren Feb 21 '15 at 10:28
  • There are three types, and any will work fine. The timer control might be easy to use. – xpda Feb 21 '15 at 16:38