George O
George O

Reputation: 187

How to take screenshots in vb.net, WPF?

So, as the title said how i can take screenshots in vb.net, WPF, the normal code for vb.net for screenshot

 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)

doesn't work because i can't define "Graphics" in WPF, i can't define drawing bitmap too, like i normally do in vb.net

so, what i can do ?

Upvotes: 0

Views: 1110

Answers (2)

rheitzman
rheitzman

Reputation: 2297

I gave up on capturing the screen "automatically" (e.g. from unhandled exceptions) - too inconsistent.

I used to use screen capture for users to use to record the form they were having problems with (code in the exception handler below.) Now I launch the SnippingTool - good for the users to know how to use that tool any way.

Friend Sub PrintScrn(ByVal ffrm As System.Windows.Forms.Form)
    Try
        If Not Environment.Is64BitProcess Then
            Process.Start("C:\Windows\sysnative\SnippingTool.exe")
        Else
            Process.Start("C:\Windows\system32\SnippingTool.exe")
        End If
    Catch ex As Exception
        If vbOK = MsgBox("Replace clipboard contents with an image of the form?", MsgBoxStyle.DefaultButton2 + MsgBoxStyle.OkCancel) Then
            Dim wid As Integer = ffrm.Width
            Dim hgt As Integer = ffrm.Height
            Dim bm As New Bitmap(wid, hgt)
            ' Draw the form onto a bitmap.
            ffrm.DrawToBitmap(bm, New Rectangle(0, 0, wid, hgt))
            My.Computer.Clipboard.SetImage(bm)
            MsgBox("Form image now on clipboard. Use Ctrl-V to paste into Word, Notes, Paint, etc.")
        End If
    End Try
End Sub

Context: Vb.Net WinForm

Upvotes: 0

SLaks
SLaks

Reputation: 887433

You need to add a reference and Imports directive to System.Drawing.

Upvotes: 1

Related Questions