Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a Form which has Labels, TextBoxes with text, PictureBox with a picture, Buttons, DateTimePicker and a image as Form Background. I want to print all these things i.e. the form except buttons and Form Border.
I have used PrintDocunt Control, PrintDialog Control and PrintForm Control.
The PrintDocument Control prints a blank document and PrintForm Control prints a cropped form with buttons and form borders. As a result buttons and form borders are appeared in document and picturebox got cropped, which I don't want.

Note: *I am using Visual Studio Community 2017 with Access Database 2016.
*VB as programming language.
*To use PrintForm control I have installed Visual Studio PowerPacks 12.0.
*The application doesn't have any error and/or exceptions. So I didn't post the full code.
*The only problem is that Printing a blank document and a cropped document.

What I have tried:

'This Code Prints A blank Document
VB
Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
        PrintDialog1.Document = PrintDocument1
        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        PrintDialog1.AllowSomePages = True

        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintDocument1.Print()
        End If
    End Sub

'This Code Prints A cropped document with buttons and Form Borders
VB
Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintForm1.Print()
        End If
    End Sub
Posted
Updated 29-Aug-18 4:19am

Yes, it will - because you haven't set a handler for the PrintDocument.PrintPage Event (System.Drawing.Printing)[^] which is where the actual work of printing the document content takes place.

The link includes a basic example.
 
Share this answer
 
Comments
Debsbond008 14-Aug-18 12:30pm    
Thanks OriginalGriff for reply. The provided link is using a text file that is already saved in computer and prints the text file. What I actually want here is to print all the form components as they are except buttons and form borders.
For example if my form has 4 Labels at left, 4 TextBoxes in middle, 1 PictureBox at right and 4 Buttons at the bottom of the Form then all the Labels, TextBoxes, PictureBox will be printed except 4 Buttons.
**I do not want to save the form as a file and use it to print further.
*But I understand that i have to use PrintPageEvent handler to do so.
I got the a solution from OriginalGriff's Idea of using a Handler to PrintDocument.PrintPage Event and Google.
Step1: Get the image of the whole form.
Step2. Crop the generated image and get image of client area/ form without border.
Step3. Print The Image.
Code For Print Button and PrintDocument.PrintPage Event
VB
Private Sub Print_btn_Click(sender As Object, e As EventArgs) Handles Print_btn.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.OriginAtMargins = False
        AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim print_bmp As Bitmap = GetClientareaImage(Me)
        e.Graphics.DrawImage(print_bmp, 0, 0)
    End Sub


Code For getting image of client area.
**I am using modules to declare functions and Subs that are required globally for this project..
VB
Module Image_processor

    Public Function GetControlImage(ByVal ctrl As Control) As Bitmap
        Dim ctrl_bmp As New Bitmap(ctrl.Width, ctrl.Height)
        ctrl.DrawToBitmap(ctrl_bmp, New Rectangle(0, 0, ctrl.Width, ctrl.Height))
        Return ctrl_bmp
    End Function

    Public Function GetClientareaImage(ByVal frm As Form) As Bitmap
        Using Cropped_frm As Bitmap = GetControlImage(frm)
            Dim Border As Point = frm.PointToScreen(New Point(0, 0))
            Dim client_x As Integer = Border.X - frm.Left
            Dim client_y As Integer = Border.Y - frm.Top
            Dim client_wd As Integer = frm.ClientSize.Width
            Dim client_ht As Integer = frm.ClientSize.Height

            Dim client_bmp As New Bitmap(client_wd, client_ht)
            Using client_graph As Graphics = Graphics.FromImage(client_bmp)
                client_graph.DrawImage(Cropped_frm, 0, 0, New Rectangle(client_x, client_y, client_wd, client_ht), GraphicsUnit.Pixel)
            End Using
            Return client_bmp
        End Using

    End Function
End Module


Thaks a lot CodeProject, OriginalGriff and Coders
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900