Solved

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

Posted on 2010-09-14
17
1,334 Views
Last Modified: 2012-05-10
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
Comment
Question by:kavvis
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33671175
If you are using VS2008, you will find print option in toolbox
Capture.JPG
0
 

Author Comment

by:kavvis
ID: 33671214
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
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33671306
0
 

Author Comment

by:kavvis
ID: 33671324
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33671349
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33671370
I think kavvis is already using power pack.
0
 

Author Comment

by:kavvis
ID: 33671395
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
 
LVL 7

Expert Comment

by:klakkas
ID: 33671491
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kavvis
ID: 33671578
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
 
LVL 7

Expert Comment

by:klakkas
ID: 33671615
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
 

Author Comment

by:kavvis
ID: 33671709
that code worked verry verry well! now I just have to convert somhow so it can save as PDF file instead of bmp!



0
 
LVL 7

Accepted Solution

by:
klakkas earned 500 total points
ID: 33671867

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
 

Author Comment

by:kavvis
ID: 33672076
But I don't  want to use another software.. Can't I use some other way..
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33672531
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
 

Author Comment

by:kavvis
ID: 33672572
OK!
Then I do like that!  Thank you very mutch for you help!
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33672624
No problem, glad I could help.
0
 

Author Closing Comment

by:kavvis
ID: 33714248
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now