Steve Thomas
Steve Thomas

Reputation: 51

VB.NET Window Screen Capture (ALT+PRINTSCREEN)

I found that code somewhere and I find it quite useful but I would like to find a way to make it work so it capture only the given window target. Maybe with a processID or Window Name. Even if that window is not active.

I do not want to make that window active but want to get a screen capture like if I was doing Alt+PrintScreen on it.

Here is the code that works for full Screen Capture

    Private bmpScreenShot As Bitmap
    Private gfxScreenshot As Graphics

    bmpScreenShot = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb)

    gfxScreenshot = Graphics.FromImage(bmpScreenShot)
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)

    bmpScreenShot.Save(fileName, ImageFormat.Png)

I use the Visual Basic 2008 Express

Thank you in advance!

Upvotes: 5

Views: 35896

Answers (5)

Capture the active-form.

Private Sub tsbCamera_Click(sender As Object, e As EventArgs) Handles tsbCamera.Click
    Dim bm As New Bitmap(Width, Height)
    DrawToBitmap(bm, New Rectangle(0, 0, Width, Height))
    Dim name As String = InputBox("Name it:")
    bm.Save(Application.StartupPath & "\ScreenShot\" & name & ".png", System.Drawing.Imaging.ImageFormat.Png)
End Sub

Upvotes: 0

Nitro
Nitro

Reputation: 11

This will give you the Alt + Printscreen, showing only front most application.

SendKeys.Send("%{PRTSC}") 

Then continue the normal way:

Dim Screenshot As Image = Clipboard.GetImage()
Screenshot.Save("c:\ScreenShot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Upvotes: 1

Crash Matrix
Crash Matrix

Reputation: 11

The easiest way to do it, though it's a hack, is this:

SendKeys.Send("{PRTSC}")
Dim Screenshot As Image = Clipboard.GetImage()
Screenshot.Save("c:\ScreenShot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Upvotes: 1

user407749
user407749

Reputation:

This works in vb.net2.0. I just used it. Here is the source code.

    Dim SC As New ScreenShot.ScreenCapture

    'captures entire desktop straight to file
    SC.CaptureScreenToFile("c:\accops\test\desktop2.jpg", Imaging.ImageFormat.Jpeg)

Upvotes: 1

EgorBo
EgorBo

Reputation: 6142

Look at this Capture screenshot of active window? Instead of this.Handle (current window) you may insert a handle of any other window (using WinAPI functions like FindWindow)

Upvotes: 1

Related Questions