943,553 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 63470
  • VB.NET RSS
Jul 28th, 2004
-1

Printing of a Form in VB.NET?

Expand Post »
Suppose one has an open VB.NET form on the screen. How can one have that form printed?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Omnibus is offline Offline
1 posts
since Jul 2004
May 29th, 2005
0

Re: Printing of a Form in VB.NET?

Hi here is the Solution:

Imports System.Runtime.InteropServices
.....
.....
' Global Variables
Dim img As Bitmap
Dim WithEvents pd As PrintDocument

'Returns the Form as a bitmap
Function CaptureForm1() As Bitmap

Dim g1 As Graphics = Me.CreateGraphics()
Dim MyImage = New Bitmap(Me.ClientRectangle.Width, (Me.ClientRectangle.Height ), g1)
Dim g2 As Graphics = Graphics.FromImage(MyImage)
Dim dc1 As IntPtr = g1.GetHdc()
Dim dc2 As IntPtr = g2.GetHdc()
BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, (Me.ClientRectangle.Height ), dc1, 0, 0, 13369376)
g1.ReleaseHdc(dc1)
g2.ReleaseHdc(dc2)
'saves image to c drive just, u can comment it also
MyImage.Save("c:\abc.bmp")
Return MyImage
End Function

<DllImport("gdi32.DLL", EntryPoint:="BitBlt", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function BitBlt(ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Boolean

' Leave function empty - DLLImport attribute forwards calls to MoveFile to
' MoveFileW in KERNEL32.DLL.
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

img = CaptureForm1()
pd = New PrintDocument
pd.Print()

End Sub
'this method will be called each time when pd.printpage event occurs
Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles pd.PrintPage

Dim x As Integer = e.MarginBounds.X
Dim y As Integer = e.MarginBounds.Y
e.Graphics.DrawImage(img, x, y)
e.HasMorePages = False

End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sabdulhaq is offline Offline
1 posts
since May 2005
Oct 18th, 2005
0

Re: Printing of a Form in VB.NET?

What if the form you have is a vertically scrollable form.

How would you print the hidden area of the form (the area you have to get to by scrolling down)..

The above method only prints the front first screen of the form.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sm43 is offline Offline
2 posts
since Aug 2003
Mar 26th, 2008
0

Re: Printing of a Form in VB.NET?

Exactly it needs to print the data which is binded into the datagrid instead of creating it an image and printing it.

Have you tried Form1.Printform?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jaytheguru is offline Offline
18 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: How to fetch a datagrid content or data to a print page function?
Next Thread in VB.NET Forum Timeline: Text in a listbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC