Converting Bitmap to RGB in VB - vb.net

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

Related

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

upload a screenshot without save it in vb.net

I would like to take a screenshot without saving it to the disk.
All this in vb.net
Since I would like to stream the screen I run a screenshot and load it every 0.5 seconds, so it does not have to be that heavy.
Below is the code I currently use:
Dim timestamp As String = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalMilliseconds)
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(Path.GetTempPath & "\stream_video\screenshot.jpg", Imaging.ImageFormat.Jpeg)
My.Computer.Network.UploadFile(Path.GetTempPath & "\stream_video\screenshot.jpg", server_total & "upload.php")
IO.File.Delete(Path.GetTempPath & "\stream_video\screenshot.jpg")
GC.Collect() 'Garbage Collection
how could I make it as fluid and optimized as possible?
[Sorry for my bad english].

Set picturebox image in thread

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.

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.

VB.NET Screen Capture

Hello all i am using the below code to capture the screenshot of the black panel of my window form in visual basic 2005.
My problem is that I want the red border image to come in between and to be full.
My 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(618, 191, 850, 455, bounds.Size, CopyPixelOperation.SourceCopy)
screenshot.Save("d:\\dcap.bmp", Imaging.ImageFormat.Bmp) `
Your code works well just set the bounds to zero
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("d:\\dcap.jpg", Imaging.ImageFormat.Bmp)