• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1407
  • Last Modified:

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

        Printform1.Print()



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

0
kavvis
Asked:
kavvis
  • 8
  • 5
  • 3
  • +1
1 Solution
 
Mohit VijayCommented:
If you are using VS2008, you will find print option in toolbox
Capture.JPG
0
 
kavvisAuthor Commented:
VjSoft:

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...
0
 
Mohit VijayCommented:
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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!
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Mohit VijayCommented:
I think kavvis is already using power pack.
0
 
kavvisAuthor Commented:
emoreau:

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?
0
 
klakkasCommented:
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)
        me_gr.ReleaseHdc(me_hdc)
        bm_gr.ReleaseHdc(bm_hdc)

        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
        m_PrintDocument.Print()
    End Sub



    ' Print the form image.
    Private Sub m_PrintDocument_PrintPage(ByVal sender As _
        Object, ByVal e As  _
        System.Drawing.Printing.PrintPageEventArgs) Handles _
        m_PrintDocument.PrintPage
        ' 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

0
 
kavvisAuthor Commented:
klakkas:

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!
0
 
klakkasCommented:
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.
0
 
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!



0
 
klakkasCommented:

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
m_PrintDocument.Print()


0
 
kavvisAuthor Commented:
But I don't  want to use another software.. Can't I use some other way..
0
 
klakkasCommented:
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.
0
 
kavvisAuthor Commented:
OK!
Then I do like that!  Thank you very mutch for you help!
0
 
klakkasCommented:
No problem, glad I could help.
0
 
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..
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 8
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now