Guest afrinspray Posted August 5, 2002 Posted August 5, 2002 Does anyone know how to get a screen capture in vb.net? I actually want to print an MDI form... Thanks, Mike Quote
Humble Seeker Posted June 18, 2003 Posted June 18, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.