Results 1 to 7 of 7

Thread: [2.0] Capture & Print Windows

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    [2.0] Capture & Print Windows

    This needs to be built as a COM object to extend the capabilities of an existing VC++ application.

    The hooks will be a menu selection and toolbar button that can make calls to the interface. The Windows Print Dialog has to be used to allow the usual printer options.

    I need to allow the user to select a form (one of probably three child forms each containing a map image), then print this image to a printer.

    I've found code that will screen capture a window to a .gif file. And I've found scraps of code that will print text documents to a printer. But nothing covering what I need to do.

    I'm new at C# and my background is VB. Any help would be much appreciated!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Capture & Print Windows

    Gigemboy has posted screen capture code to the VB.NET CodeBank forum. The fact that it's VB code should make it easier for you to understand and then you can convert it to C# if required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [2.0] Capture & Print Windows

    Thanks. I checked out his code, but the screen capture part I can do already, though the added functionally looks promising.

    The problem is with opening the image file and printing it.

    Are there .NET classes that will handle formating printer output, such as scaling, or adding headers/footers, etc.?

    And the COM issue is a concern. Can you plug a COM component into a VC++ application and pass it window handles? It seems as though this should be a resounding YES, but other sources make this sound next to impossible.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Capture & Print Windows

    All .NET printing is done using a PrintDocument object. You call its Print method and handle its PrintPage event. In the PrintPage event handler you use GDI+ to draw whatever you want printed, so you have complete control over what gets printed where. You can define the area dedicated to headers and footers and simply print the same thing in that area each time the PrintPage event is raised. As for images, you have all the power of the Graphics.DrawImage method at your disposal. If you need more then you have to operate on the Image object yourself before passing it to DrawImage.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [2.0] Capture & Print Windows

    Thank you for pointing me in the right direction!

    I'm going to leave this thread unresolved so I can come back with more refined questions and visit the COM implementation issue. Is building COM in .NET something I can easily do?

    Thanks.

  6. #6

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [2.0] Capture & Print Windows

    Ok. After a bit of research and help from "The Book of Visual Basic 2005: .NET Insight for Classic VB Developers (ISBN: 9781593270742) by Mathew McDonald, I can now print a bitmap stored in a file on my hard drive and even do a print preview:
    Code:
    Imports System.Drawing.Printing
    
    
    Public Class Form1
        Dim PrintPreviewDialog1 As PrintPreviewDialog
        'Dim PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
        Dim PrintPageSettings As New PageSettings
        Dim PrintDocument2 As New PrintDocument ' this takes place of placing from the toolbox
        Dim PrintDialog1 As New PrintDialog
    
        ' Private WithEvents docToPrint As New Printing.PrintDocument
        ' Sub for printing graphic
        Private Sub PrintGraphic(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
            ' Create the graphic using DrawImage
            ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text), ev.Graphics.VisibleClipBounds)
            ' specify that this is the last page to print
            ev.HasMorePages = False
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' print using an error handler to catch problems
            Try
    
                '     Dim result As DialogResult = PrintDialog1.ShowDialog()
                '    If (result = DialogResult.OK) Then  ' this code does what it is supposed to do.
                PrintDocument2.Print()  ' print graphic
                '      End If
            Catch ex As Exception   ' catch printing exception
                MessageBox.Show("Sorry--there is a printing problem", ex.ToString())
    
            End Try
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                PrintDialog1.AllowSomePages = True
                AddHandler PrintDocument2.PrintPage, AddressOf Me.PrintGraphic
                PrintDialog1.Document = PrintDocument2
            Catch ex As Exception   ' catch printing exception
                MessageBox.Show("Sorry--there is a printing problem", ex.ToString())
    
            End Try
        End Sub
    
        Private Sub btnPrintSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintSetup.Click
            'Try
            '    ' load page settings and display page setup dialog box
            '    PageSetupDialog1.PageSettings = PrintPageSettings
            '    PageSetupDialog1.ShowDialog()
    
            'Catch ex As Exception
            '    ' display error message
            '    MessageBox.Show(ex.Message)
            'End Try
    
            Dim dlgSettings As New PrintDialog()
            dlgSettings.Document = PrintDocument2
            dlgSettings.ShowDialog()
    
        End Sub
    
        Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
            Dim dlgPreview As New PrintPreviewDialog()
            dlgPreview.Document = PrintDocument2
            dlgPreview.Show()
    
        End Sub
    End Class
    I'm definitely on my way. Next up is how to scale the image to fit the paper. The code above prints the image but doesn't scale properly. I'll get squashed images.

    How do I:

    a) Properly scale x and y based on the original aspect ratio to fit to the page in the most appropriate format (landscape vs. portrait) or to force one or the other)
    b) Place the image on the page where I want it
    c) Include header/footer data

    I know entire books and web sites are probably dedicated to this. I've read up on GDI+ and even revisited Matrix math (shudder!) to try to do this. But Google is letting me down.

    Can anyone explain what I need to know, or point me to where I can learn it?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Capture & Print Windows

    The Graphics.DrawImage method has a large number of overloads. You should read the documentation for the method to see all the options available to you. Many of them allow you to specify a Rectangle or RectangleF structure within which to draw the Image. That structure controls the location and size of the drawn image. If you specify a Point or PointF instead then the Image will be draw at that location at its original size.

    The properties of the PrintPageEventArgs passed to the PrintPage event handler can give you information about the page being printed on. For positioning printed elements you'd be most interested in the MarginBounds property.

    Just like everything else, it's up to you to print any header and footer information on the page. A header is simply the same information printed at the top of every page, so you simply draw the same thing at the top of the page in every PrintPage event handler. The footer is the same but at the bottom of the page. If you want to print page numbers then it's up to you to count the number of times the PrintPage event handler has been raised. If you need a total number of pages then you will need to calculate that before you start printing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width