Solved

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

Posted on 2010-09-14
17
1,358 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 70

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
 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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