Printing a form

After following the code found here  to print a Windows Form I now have this

Dim memoryImage As Bitmap

Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage =
New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(
Me.Location.X, Me.Location.Y, 0, 0, s)
End Sub

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

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub

This works fine if the form you are printing fits on the screen and is visible.
What I would really like to do is print a form whose height is larger than the screen height and is also hidden.

Can anyone help me with this please. I don't seem to be able to get my head around it. I'm really, really, really missing the VB6 PrintForm



Answer this question

Printing a form

  • shaen170013

    OK, it worked, but, it printed out everything but the contents of my RichTextBox!

    The frame of the RichTextBox is there, but, the text inside is blank!


    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Dim prn As New Printing.PrintDocument
    '---- Select the PDF Printer
    prn.PrinterSettings.PrinterName =
    "pdf995"
    '---- Handle the page events
    AddHandler prn.PrintPage, AddressOf Me.PrintFormHandler
    '---- Do the print (Printing handled by the print page handler)
    prn.Print()
    '---- Remove the page handler
    RemoveHandler prn.PrintPage, AddressOf Me.PrintFormHandler
    End
    Sub

    Private Sub PrintFormHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
    '---- Create a new bitmap of a certain size
    Dim b As New Bitmap(Me.Width, Me.Width)
    '---- draw 'me' into that bitmap
    Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
    '---- Draw the resultant bitmap (image) into the printer canvas
    args.Graphics.DrawImage(b, New PointF(20, 20))
    End Sub


  • Phil026

    I really need to print a form that is higher than the screen height and I need to print all the labels and panels too. Not bothered about title bar/ borders etc.

    With the the amount of views this post has had it seems there are quite a few people interested in this too. I can't believe this has become so difficult.

    Does anyone have any ideas about this Any MS tech people reading this


  • Synergy

    Does the list of installed printers in Listbox1 when you click the "ButtonPrinters" button contain the entry "EPSON Stylus Photo 870" exactly as you have it when you hard coded it If you select a printer that is connected but, nor turned on you will get errors like you describe. I have two printers currently connected to my system here and I can list both of them in the Listbox even if neither one is turned on. But, if I click the print button, I either get an error or the document is put into that printer's document que.

    You can change the Listbox1.SelectedItem.ToString to just: Listbox1.SelectedItem.

    As I said previously, the code above works here with two printers attached. Other than that, I'm not sure what the problem may be.

    james

    aka:Trucker


  • Ed Hall

    I tried to run the code, but, I got an error on the "PrintDocument1.Print()" statement.

    I tried changing it to "PrintDocument1_PrintPage()", but, it is still missing the argument.

    Dim memoryImage As Bitmap

    Private Sub CaptureScreen()
    Dim myGraphics As Graphics = Me.CreateGraphics()
    Dim s As Size = Me.Size
    memoryImage =
    New Bitmap(s.Width, s.Height, myGraphics)
    Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
    memoryGraphics.CopyFromScreen(
    Me.Location.X, Me.Location.Y, 0, 0, s)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    CaptureScreen()
    PrintDocument1.Print() ' <------------------------------ Error on this line ! ! ! ! ! !
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.Graphics.DrawImage(memoryImage, 0, 0)
    End Sub


  • ETS

    Without seeing the code you used, there is no way to help you. Post what you have and maybe we can help you out.

    james

    aka:Trucker


  • ueo

    Thanks for helping

    It works great but then I changed the height of the form to 1036 (which is the max I can go at this resolution (1280*1024)) and set AutoScrollMinSize.Height to 1100.

    This makes scroll bars appear and obviously makes the bottom of the form disappear off the bottom of the screen. This is the height I would need to use for my app.

    Now when I run the app. it prints the part of the form that is on screen (even behind the taskbar) but anything beyond that is missing.

    Also depending on screen resolution, more or less of the form is printed.

    Any further help would be very much appreciated.


  • JonJon

    OK, it lists the installed printers. I click on one of them. Then, I Press [ButtonPrint]. Then it displays this message 5 times:

    "A first chance exception of type 'System.NullReferenceException' occurred in PrintForm.exe"

    I think that the problem may be with this line:

    prn.PrinterSettings.PrinterName = ListBox1.SelectedItem.ToString

    because when I substitute this line:

    prn.PrinterSettings.PrinterName = "EPSON Stylus Photo 870"

    IT WORKS ! ! !

    Imports System.Drawing.Printing

    Public Class Form1

    Private Sub ButtonPrinters_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrinters.Click

    'lists printer(s) installed on current system

    Dim pkInstalledPrinters As String

    If PrinterSettings.InstalledPrinters.Count = 0 Then

    MessageBox.Show("No Printers installed!!")

    Exit Sub

    Else

    End If

    For i As Integer = 0 To PrinterSettings.InstalledPrinters.Count - 1

    pkInstalledPrinters = PrinterSettings.InstalledPrinters.Item(i)

    ListBox1.Items.Add(pkInstalledPrinters)

    Next

    End Sub

    'Then, using the code in the previous example, here is how you select and print the form:

    Private Sub ButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrint.Click

    Dim prn As New Printing.PrintDocument

    '---- Select the PDF Printer

    prn.PrinterSettings.PrinterName = ListBox1.SelectedItem.ToString '<--------------

    '---- Handle the page events

    AddHandler prn.PrintPage, AddressOf Me.PrintFormHandler

    '---- Do the print (Printing handled by the print page handler)

    prn.Print()

    '---- Remove the page handler

    RemoveHandler prn.PrintPage, AddressOf Me.PrintFormHandler

    End Sub

    Private Sub PrintFormHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)

    '---- Create a new bitmap of a certain size

    Dim b As New Bitmap(Me.Width, Me.Width)

    '---- draw 'me' into that bitmap

    Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))

    '---- Draw the resultant bitmap (image) into the printer canvas

    args.Graphics.DrawImage(b, New PointF(20, 20))

    'After Listing the installed printers in Listbox1

    'you can Select one ( click on it)

    'and then from the second Button

    'I did the selection of the printer in this line:

    'prn.PrinterSettings.PrinterName = ListBox2.SelectedItem.ToString

    'This works here and allows you to select the printer you want to use

    'from the List of installed printers.

    End Sub

    End Class


  • cyles

    I tried running the above code, but, I got:

    "Settings to access printer 'pdf995' are not valid."

    How do I have the user pick one from the list of installed printers


  • dragonsbb16

    Hey man the code is the same as the one in the first post in this current topic. so i thougt no need to copy and paste it again :P

    sorry if you didn't understand it...


  • Martin B

    "Listbox1.SelectedItem" solved the problem! ! !

    It works great, now!

    Thanks a lot!


  • Lues

    furjaw wrote:

    I tried running the above code, but, I got:

    "Settings to access printer 'pdf995' are not valid."

    How do I have the user pick one from the list of installed printers

    To get the list of installed printers you can do the following:

    (be sure to add Imports System.Drawing.Printing at the top of your class)

    Private Sub ButtonPrinters_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrinters.Click

    'lists printer(s) installed on current system

    Dim pkInstalledPrinters As String

    If PrinterSettings.InstalledPrinters.Count = 0 Then

    MessageBox.Show("No Printers installed!!")

    Exit Sub

    Else

    End If

    For i As Integer = 0 To PrinterSettings.InstalledPrinters.Count - 1

    pkInstalledPrinters = PrinterSettings.InstalledPrinters.Item(i)

    ListBox1.Items.Add(pkInstalledPrinters)

    Next

    End Sub

    Then, using the code in the previous example, here is how you select and print the form:

    Private Sub ButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrint.Click

    Dim prn As New Printing.PrintDocument

    '---- Select the PDF Printer

    prn.PrinterSettings.PrinterName = ListBox2.SelectedItem.ToString

    '---- Handle the page events

    AddHandler prn.PrintPage, AddressOf Me.PrintFormHandler

    '---- Do the print (Printing handled by the print page handler)

    prn.Print()

    '---- Remove the page handler

    RemoveHandler prn.PrintPage, AddressOf Me.PrintFormHandler

    End Sub

    Private Sub PrintFormHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)

    '---- Create a new bitmap of a certain size

    Dim b As New Bitmap(Me.Width, Me.Width)

    '---- draw 'me' into that bitmap

    Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))

    '---- Draw the resultant bitmap (image) into the printer canvas

    args.Graphics.DrawImage(b, New PointF(20, 20))

    End Sub

    After Listing the installed printers in Listbox1 you can Select one ( click on it) and then from the second Button I did the selection of the printer in this line:

    prn.PrinterSettings.PrinterName = ListBox2.SelectedItem.ToString

    This works here and allows you to select the printer you want to use from the List of installed printers.

    james

    aka:Trucker


  • TomasZednik

    Hey m8, you are getting that error because you must have not added a PRINTDOCUMENT control into your form.

    Try adding that control, and re-run the application; i am sure it will work.


  • Shay Friedman

    The code you have posted is capturing the screen, obviously. In fact, it's a decent way to print out a screen capture.

    However, what you need to do is take a bitmap of the form and DrawImage of that bitmap. Browsing around the help gives something like:


    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Dim prn As New Printing.PrintDocument
    '---- Select the PDF Printer
    prn.PrinterSettings.PrinterName =
    "pdf995"
    '---- Handle the page events
    AddHandler prn.PrintPage, AddressOf Me.PrintFormHandler
    '---- Do the print (Printing handled by the print page handler)
    prn.Print()
    '---- Remove the page handler
    RemoveHandler prn.PrintPage, AddressOf Me.PrintFormHandler
    End Sub

    Private Sub PrintFormHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
    '---- Create a new bitmap of a certain size
    Dim b As New Bitmap(Me.Width, Me.Width)
    '---- draw 'me' into that bitmap
    Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
    '---- Draw the resultant bitmap (image) into the printer canvas
    args.Graphics.DrawImage(b, New PointF(20, 20))
    End Sub

    I'm sure that'll help.



  • Mangakl

    D:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\DoTestingHere\DoTestingHere\Form1.vb(80): 'CopyFromScreen' is not a member of 'System.Drawing.Graphics'.


    Thats what i get man ;(


  • Printing a form