Sunday, August 23, 2009

To see Print Preview & Print a WindowsForm, Code in VB.Net


PrintDoc1.PrintPage += PDoc_PrintPage;
Private PrintDoc1 As PrintDocument = New PrintDocument
Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog

Private Sub btn_PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_PreviewButton.Click 'When PrintPreview Button Clicks

PrintPreviewDialog1.Document = PrintDoc1
'To set or Get the Position of a Graphic Object
PrintDoc1.OriginAtMargins = True
'PrintDoc1.PrintPage += PDoc_PrintPage;
PrintPreviewDialog1.ShowDialog()
End Sub


Private Sub PDoc_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)

Dim bmp As Bitmap = New Bitmap(Me.Width, Me.Height)

'this.DrawToBitmap(bmp, this.ClientRectangle);
Me.DrawToBitmap(bmp, Me.ClientRectangle)

'Takes the Snap of the Exact WindowForm size as Bitmap image
Me.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, 0, 0)
End Sub

In C# Code

No comments:

Post a Comment