Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-14
17
Medium Priority
?
1,395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 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