Jump to content
Xtreme .Net Talk

Capture the Screen in VB.NET


Recommended Posts

Guest afrinspray
Posted

Does anyone know how to get a screen capture in vb.net? I actually want to print an MDI form...

 

Thanks,

Mike

  • 10 months later...
Posted

Have a play round with this code

 

Option Explicit On

Option Strict On

'**********************************************************************************

' PrintForm Class

'

' Purpose: Print active window

'

' Dependencies:

'

' Author: Øyvind Rustan - Compositae, 27.05.2002

' http://www.compositae.no

' Revision:

'

'**********************************************************************************

 

Imports System.Windows

Imports System.Drawing

 

Public Class PrintForm

'------------------------------------------------------------------------------

' New

' Constructor

'------------------------------------------------------------------------------

Public Sub New()

End Sub

 

'------------------------------------------------------------------------------

' Print

' Print active window (form)

'------------------------------------------------------------------------------

Public Sub Print()

Try

'-- Keys Alt+PrintScreen copies active window to clipboard

Forms.SendKeys.SendWait("%{PRTSC}")

'-- Create print document and printing handler

Dim pd As New Printing.PrintDocument()

AddHandler pd.PrintPage, AddressOf pd_PrintPage

'-- Setup document

pd.DocumentName = "PrintScreen"

With pd.DefaultPageSettings

.Landscape = False

'-- Reduce margins to accomodate larger pictures

.Margins.Left -= .Margins.Left \ 3

.Margins.Top -= .Margins.Top \ 3

.Margins.Right -= .Margins.Right \ 3

.Margins.Bottom -= .Margins.Bottom \ 3

End With

'-- Print document

pd.Print()

Catch ex As Exception

Throw ex

End Try

End Sub

 

'------------------------------------------------------------------------------

' pd_PrintPage

' PrintPage handler

'------------------------------------------------------------------------------

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)

Try

'-- Get PrintScreen bitmap from clipboard

Dim iData As IDataObject = Clipboard.GetDataObject()

If iData.GetDataPresent(DataFormats.Bitmap) Then

Dim bm As Bitmap = CType(iData.GetData(DataFormats.Bitmap), Bitmap)

'-- Set size of destination rectangle (printer)

Dim rDest As New RectangleF(ev.MarginBounds.Left, ev.MarginBounds.Top, 0, 0)

If bm.Width < ev.MarginBounds.Width Then

'-- Set to bitmap size if bitmap is smaller than paper

rDest.Width = bm.Width

rDest.Height = bm.Height

Else

'-- Set to paper size if bitmap is larger than paper

'-- Scale height to retain proportions

rDest.Width = ev.MarginBounds.Width

rDest.Height = CType(rDest.Width * (bm.Height / bm.Width), Single)

End If

'-- Draw bitmap

ev.Graphics.DrawImage(bm, rDest, bm.GetBounds(GraphicsUnit.Pixel), GraphicsUnit.Pixel)

'-- Draw timestamp

Dim printFont As Font = New Font("Arial", 10, FontStyle.Regular)

ev.Graphics.DrawString(Now.ToString, printFont, Brushes.Black, rDest.Left, rDest.Top + rDest.Height + printFont.GetHeight(ev.Graphics))

Else

'-- No bitmap, cancel printing

ev.Cancel = True

End If

Catch ex As Exception

'-- Cancel printing

ev.Cancel = True

Throw ex

Finally

'-- Print only on page

ev.HasMorePages = False

End Try

End Sub

End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...