Set picturebox image in thread - vb.net

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

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.

Related

Vb.Net Application Screen Capture and Save it as a pdf

I have this small application which capture the screen and save it as a pdf. I am running this application on my PC which has 1080P primary display and 1080P TV as an extended display. When i put the application on the extended TV display (full screen mode) it doesn't capture entire screen. It capture only 1/4 of the screen. But i want to capture the entire screen. How do i fix this. Please help
Here is the code;
Private Sub SaveForm_shift1()
Dim yesterday As String = DateTime.Now.ToString("yyyy-MM-dd")
Dim filePath As String = "C:\Autodesk" + yesterday + ".jpg"
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
Try
gfxScreenshot.Dispose()
' Save the screenshot
bmpScreenshot.Save(filePath)
saveToPdf_shift1(filePath)
deleteImg(filePath)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Use this function to get a Bitmap with all the screens
Public Function GetScreenShot() As Bitmap
Dim bmp As Bitmap = New Bitmap(Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width),
Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height))
Dim gfx As Graphics = Graphics.FromImage(bmp)
gfx.CopyFromScreen(New Point(0, 0), New Point(0, 0), bmp.Size)
Return bmp
End Function
If you have strange staggered screen positions, the empty areas will be black in the resulting bitmap.

Disappearing Picturebox images [duplicate]

This question already has answers here:
Drawing Graphics Disappear in VB.net
(4 answers)
Closed 5 years ago.
I was successful in loading an image from a file into a picturebox in visual basic. Or so i thought. I had a msgbox in the code and the picture showed up in the picturebox. take the msgbox out and no picture. Any Ideas? Thanks
Private Sub DrawImageinSquarePanel(Panelname As PictureBox, ImageFile As String)
g = Panelname.CreateGraphics() 'creates new graphics element in panel
Dim newImage As Image = Image.FromFile(ImageFile) ' Create image.
Dim SquareDim As Integer 'Size of longest dimension in source image
If newImage.Width > newImage.Height Then
SquareDim = newImage.Width
Else
SquareDim = newImage.Height
End If
MsgBox(Panelname.Width & " " & Panelname.Height) 'the magic msgbox!!
' scale factor
Dim imageAttr As New ImageAttributes
imageAttr.SetGamma(Panelname.Width / SquareDim)
Dim ScaleFactor As Single = Panelname.Width / SquareDim
' Create rectangle for source and destination image.
Dim srcRect As New Rectangle(0, 0, newImage.Width, newImage.Height)
Dim destRect As New Rectangle((Panelname.Width - newImage.Width * ScaleFactor) / 2, (Panelname.Height - newImage.Height * ScaleFactor) / 2, newImage.Width * ScaleFactor, newImage.Height * ScaleFactor)
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw image to screen.
g.DrawImage(newImage, destRect, srcRect, units)
End Sub
Your title says "Picturebox" but there's no PictureBox there at all. If you want to display an Image in a PictureBox then assign the Image object to the Image property of that PictureBox.
Don't use GDI+ to draw it on a Panel and, if you ever do use GDI+ to draw, NEVER call CreateGraphics. Always draw on a control in its Paint event handler. The reason that your drawing disappears is that all drawing is erased on every Paint event. By doing your drawing in the Paint event handler, you ensure that it gets reinstated each time.
If you want to modify the image before displaying it then what you should do is create a new Bitmap object, use GDI+ to draw the modified image onto that and then assign that to the Image property of the PictureBox, e.g.
Using originalImage = Image.FromFile(filePath)
Dim newImage As New Bitmap(originalImage.Width, originalImage.Height)
Using g = Graphics.FromImage(newImage)
g.DrawImage(originalImage, Point.Empty)
End Using
'Dispose the existing image if there is one.
PictureBox1.Image?.Dispose()
PictureBox1.Image = newImage
End Using

How to save real PictureBox

I need to save picturebox with real image as displayed.
Help me?
Ex:
Dim myEncoder As Encoder
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
Dim bmp As Bitmap = CType(imgJpgPng.Image, Bitmap)
Dim bmpt As New Bitmap(640, 640)
Using g As Graphics = Graphics.FromImage(bmpt)
g.DrawImage(bmp, 0, 0, bmpt.Width, bmpt.Height)
End Using
myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)
myEncoder = Encoder.Quality
myEncoderParameters = New EncoderParameters(1)
myEncoderParameter = New EncoderParameter(myEncoder, CType(75L, Int32))
myEncoderParameters.Param(0) = myEncoderParameter
bmpt.Save("d:\ImgTemp\0000.JPG", myImageCodecInfo, myEncoderParameters)
From what I'm getting is that you want to save the picture box image, maybe to a directory or something. So, lets get right into this(It's very simple. By the way, I noticed you have Dim'd the bmpt at 640, 640, maybe that's a problem.
This is how I would save a picture box image
PictureBox1.Image.Save("C:\Users\Owner\Desktop\Webcam Application poctures\Picture.jpg")
Its very self explanatory, Picturebox1.Image.Save(saving the image) and in the quotations, there's the directory, and at the end there' a jpg, you can make it a .png, or any other file format.
If you want to have it being displayed as the original picture in the picture box, you will need to goto the properties of the picture box and make it bigger(size) or use the stretch feature.
Edit: I'm sorry I didn't read your question right, here's an update!
Read this website's information and look into it, this will help you greatly!
http://www.vb-helper.com/howto_resize_picture_save.html

Save multiple screenshots

I'm using this code to take a screenshot with my VB app:
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.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
screenshot.Save("c:\Screenshot.png", Imaging.ImageFormat.Png)
End Sub
How would it be possible to save multiple screenshots in the same map? (eg. screenshot1.png, screenshot2.png, ....)
Thanks again.
PS: also if there are 2 monitors, is there a way to take a 'fullscreen' screenshot?
Posting An Answer Because of the turn around time
Even though your original code you have posted wont compile on my system, With my comment as well as Pro Grammers comment, this should do the trick.
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
Dim TimeStamp = DateTime.Now.ToLongTimeString & "_" & DateTime.Now.ToShortDateString()
Label1.Text = TimeStamp
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)
screenshot.Save("c:\Screenshot_" & TimeStamp & ".png", Imaging.ImageFormat.Png)
Have a play around with the time stamp and see what works for you.

Converting Bitmap to RGB in VB

Please see the attatched code below. I have managed to take a screenshot of my computing using VB and I have stored it as a bitmap. I want to convert this bitmap (quickly!) to an array of rgb values.
Any simple code that can do this?
Cheers
Martin
' Capture screen 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.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
' Display the captured image
Display.Image = screenshot
An example is in the Bitmap.LockBits documentation:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx#Y856