Suppose one has an open VB.NET form on the screen. How can one have that form printed?
Hi here is the Solution:
' 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)
'saves image to c drive just, u can comment it also
<DllImport("gdi32.DLL", EntryPoint:="BitBlt", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
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.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
img = CaptureForm1()
pd = New PrintDocument
'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
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.
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?
it says that printdocument and PrintPageEventArgs are not defind
You will have to add them to your project.
Look in the toolbox when in design view, you will find PrintDocument under the printing tree.
Add it manually;
Imports System.Drawing.Printing .... Dim pd As New PrintDocument
Edited 4 Years Ago by Begginnerdev
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...
Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.
This is the result of previous question answered :
code for the listbox - datagridview interaction
At the top of the code ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.
Private Sub btn_addline_Click(ByVal ...