VB.NET Printscreen... Print winform option...

Hi.. I need som help..

I want to take print screen or something like on a winform..

I have this funktion but with this function I have I have to use som extra software...

Please look at my code..

Dim Printform1 As New Microsoft.VisualBasic.PowerPacks.Printing.PrintForm(myForm)

        'Printform1.PrinterSettings.PrinterName = "Adobe PDF"
        Dim newMargins As System.Drawing.Printing.Margins
        newMargins = New System.Drawing.Printing.Margins(10, 10, 70, 10)
        Printform1.PrinterSettings.DefaultPageSettings.Margins = newMargins

        Printform1.PrinterSettings.PrinterName = "PDFCreator"

        'Printform1.PrinterSettings.PrinterName = "Adobe PDF"
        Printform1.PrinterSettings.DefaultPageSettings.Landscape = False


So do someone have some tips or help to fix this to another solution...

Who is Participating?
klakkasConnect With a Mentor Commented:

In order to get a PDF document, you will have to print to you PDF printer, instead of the default system printer.

This is easily done by adding
m_PrintDocument.PrinterSettings.PrinterName = "PDF Creator"
in the btnPrint_click, right above

Mohit VijayCommented:
If you are using VS2008, you will find print option in toolbox
kavvisAuthor Commented:

That´s true.. hmm...
I never use it like that.. I almost write the code..
hmm... Can I get some exemple.. I want the current viewd form to be save as PDF.. or someting...
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Mohit VijayCommented:
kavvisAuthor Commented:
hmm.. I have googled that to... but can´t get the function I really need.

I want to print the whole form...  not just a textbox or something like that.. the winform   just like it is.. can´t understand that this should be so hard or I´m verry verry stupid.. :S
Thank you anyway!
Éric MoreauSenior .Net ConsultantCommented:
Mohit VijayCommented:
I think kavvis is already using power pack.
kavvisAuthor Commented:

I have tested this but When I try to get it into PDF then I have to use a nother software...  to do the "pritning" to PDF

But take it from the beginning..

give me some tips how I should do If I need.

1. Need to have a pdf file on my winform
2, very good if I get some options.. save as bmp..  save as pdf..  
3. is power pack the best way?
This is the code I use to print the form exactly as it appears on the user, without the borders.

As you can see, it also contains a function called GetFormImage, which returns a bmp of the form.
Imports System.Drawing.Printing

Public Class Form1

    Private Declare Auto Function BitBlt Lib "gdi32.dll" (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
    Private Const SRCCOPY As Integer = &HCC0020

    ' Variables used to print.
    Private m_PrintBitmap As Bitmap
    Private WithEvents m_PrintDocument As PrintDocument

    Private Function GetFormImage() As Bitmap
        ' Get this form's Graphics object.
        Dim me_gr As Graphics = Me.CreateGraphics

        ' Make a Bitmap to hold the image.
        Dim bm As New Bitmap(Me.ClientSize.Width, _
            Me.ClientSize.Height, me_gr)
        Dim bm_gr As Graphics = Graphics.FromImage(bm)
        Dim bm_hdc As IntPtr = bm_gr.GetHdc

        ' Get the form's hDC. We must do this after 
        ' creating the new Bitmap, which uses me_gr.
        Dim me_hdc As IntPtr = me_gr.GetHdc

        ' BitBlt the form's image onto the Bitmap.
        BitBlt(bm_hdc, 0, 0, Me.ClientSize.Width, _
            Me.ClientSize.Height, _
            me_hdc, 0, 0, SRCCOPY)

        Return bm

    End Function

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintMe.Click
        ' Copy the form's image into a bitmap.
        m_PrintBitmap = GetFormImage()

        ' Make a PrintDocument and print.
        m_PrintDocument = New PrintDocument
        m_PrintDocument.DefaultPageSettings.Landscape = True
    End Sub

    ' Print the form image.
    Private Sub m_PrintDocument_PrintPage(ByVal sender As _
        Object, ByVal e As  _
        System.Drawing.Printing.PrintPageEventArgs) Handles _
        ' Draw the image centered.
        Dim x As Integer = e.MarginBounds.X + _
            (e.MarginBounds.Width - m_PrintBitmap.Width) \ 2
        Dim y As Integer = e.MarginBounds.Y + _
            (e.MarginBounds.Height - m_PrintBitmap.Height) \ 2

        e.Graphics.DrawImage(m_PrintBitmap, x, y)

        ' There's only one page.
        e.HasMorePages = False
    End Sub

End Class

Open in new window

kavvisAuthor Commented:

Thank you for that.. but I´m afarid that this is to hard code for me.. can´t understand..

I´m pretty new of this and can only  VB.NET..

thank you anyway!
The code I gave you is VB.NET

Just copy this code into your form, add a Print button which is handeled by Private Sub btnPrint_Click and you are done. No need to change anything.

Note: This will print to the default system printer.
kavvisAuthor Commented:
that code worked verry verry well! now I just have to convert somhow so it can save as PDF file instead of bmp!

kavvisAuthor Commented:
But I don't  want to use another software.. Can't I use some other way..
I beleive that in order to create a PDF document, you must use a 3rd party tool, with a PDF printer being the most commonly used.
kavvisAuthor Commented:
Then I do like that!  Thank you very mutch for you help!
No problem, glad I could help.
kavvisAuthor Commented:
Thank you verry mutch....  if someone have any other idé how to make a PDF without a 3-parts software then I listen!

Thank you all..
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.