User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 426,478 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,198 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 27087 | Replies: 3
Reply
Join Date: Jul 2004
Posts: 1
Reputation: Omnibus is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Omnibus Omnibus is offline Offline
Newbie Poster

Printing of a Form in VB.NET?

  #1  
Jul 28th, 2004
Suppose one has an open VB.NET form on the screen. How can one have that form printed?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 1
Reputation: sabdulhaq is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sabdulhaq sabdulhaq is offline Offline
Newbie Poster

Re: Printing of a Form in VB.NET?

  #2  
May 29th, 2005
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
Reply With Quote  
Join Date: Aug 2003
Posts: 2
Reputation: sm43 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sm43 sm43 is offline Offline
Newbie Poster

Re: Printing of a Form in VB.NET?

  #3  
Oct 18th, 2005
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.
Reply With Quote  
Join Date: Aug 2007
Posts: 16
Reputation: jaytheguru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jaytheguru jaytheguru is offline Offline
Newbie Poster

Re: Printing of a Form in VB.NET?

  #4  
Mar 26th, 2008
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 4:15 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC