-1

i'm trying set picturebox image in thread but i cant set it

  • take screenshot in specific window
  • set picturebox image to screenshot

This codes can get screenshot in specific window but cant set picturebox image to it. Whats wrong?

Public Function PrintWindow(hwnd As IntPtr) As Bitmap
    Dim rc As RECT
    GetWindowRect(hwnd, rc)
    Dim bmp As New Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)
    Dim gfxBmp As Graphics = Graphics.FromImage(bmp)
    Dim hdcBitmap As IntPtr = gfxBmp.GetHdc()
    PrintWindow(hwnd, hdcBitmap, 0)
    gfxBmp.ReleaseHdc(hdcBitmap)
    gfxBmp.Dispose()
    Return bmp
End Function

Dim OverviewRefresherThread As New Thread(AddressOf RefreshOverviewThread)

Public Sub RefreshOverviewThread()
    Do
        MainWindow.PictureBox.Image = PrintWindow(WindowHandle("TEST"))
    Loop
End Sub

thanks...

  • 2
    Possible duplicate of [Crossthread operation not valid... - VB.NET](http://stackoverflow.com/questions/2240702/crossthread-operation-not-valid-vb-net) – Nico Schertler Dec 07 '16 at 21:03

1 Answers1

-1

Am I right? You are tying to get a screenshot from Whatever and transfer it in Picturebox? If so then take a look at this code.

Dim bounds As 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.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot

This code takes a screenshot of the desktop and transfer it in picbox.

You can see tutorial here.

http://vb6.wonderhowto.com/how-to/capture-desktop-screen-with-vb-net-0158485/

If im not mistaken you already done with the screenshot you just need to transfer it and thats your problem.

Shadow Fiend
  • 351
  • 6
  • 18
  • The issue is probably that the OP is trying to update a UI component from a non-UI thread. – Enigmativity Dec 07 '16 at 23:47
  • I dont know why and Im trying to analyze my answer and it looks like its not justifiable to down vote. At some point this code can help because it does do what the user wants. ahhh forget about its already down. – Shadow Fiend Dec 07 '16 at 23:50