Need help with PrintDocument

superservo15

Member
Joined
Apr 25, 2006
Messages
11
Location
Canada
Programming Experience
10+
Hi everyone...
I am stuck and need some guidance on using print document. We are building a windows app in vb.net and VS 2008. My requirments initially were to print the in focus form and scale based on printer settings.

Then a change came back and now they want the entire app window, so if they have multiple forms open side by side to view data, they want to print it all.

Everything is contained in a MainMenu form container, so this is great. I implemented a base form that handles the key capture and the printing. However, they way I have done things I do not see the menu or full window (with title) which is now something they want. It seems to only start taking the bitmap image on the in focus form but I need ot get it to start at the top corner of the application (mainMenu) form.

Here is a snippet of the code I am currently using:
Code:
#Region "Print Screen Functionality"
    Private Function PrintDocument() As Boolean
        If objPrintDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            objPrintDocument.PrinterSettings = objPrintDialog.PrinterSettings

            Me.Refresh()
            mPrintBitmap = GetFormImage()
            objPrintDocument.OriginAtMargins = False
            objPrintDocument.Print()
        End If

        Return True
    End Function

    Private Function GetFormImage() As Bitmap
        ' Get this form's Graphics object.
        Dim frm As Form
        Dim width As Integer = Me.ClientSize.Width ' default
        Dim height As Integer = Me.ClientSize.Height

        For Each frm In My.Application.OpenForms ' default
            If frm.Name = "frmMainMenu" Then
                'Get size for screenshot
                width = frm.Size.Width
                height = frm.Size.Height
                frm.Refresh()
            End If
        Next
        Dim me_gr As Graphics = Me.CreateGraphics

        ' Make a Bitmap to hold the image.
        Dim bm As New Bitmap(width, height, me_gr)

        Dim bm_gr As Graphics = me_gr.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, _
            width, _
            height, _
            me_hdc, 0, 0, SRCCOPY)
        me_gr.ReleaseHdc(me_hdc)
        bm_gr.ReleaseHdc(bm_hdc)

        ' Return the result.
        Return bm
    End Function


    Private Sub objPrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles objPrintDocument.PrintPage
       Dim newWidth As Single = mPrintBitmap.Width * 100 / mPrintBitmap.HorizontalResolution
       Dim newHeight As Single = mPrintBitmap.Height * 100 / mPrintBitmap.VerticalResolution
        ' Convert to same units (100 ppi) as e.MarginBounds.Height
        Dim widthFactor As Single = newWidth / e.MarginBounds.Width
        Dim heightFactor As Single = newHeight / e.MarginBounds.Height

        If widthFactor > 1 Or heightFactor > 1 Then
            ' if the image is wider or taller than the printable area then adjust...
            If widthFactor > heightFactor Then
                newWidth = newWidth / widthFactor
                newHeight = newHeight / widthFactor
            Else
                newWidth = newWidth / heightFactor
                newHeight = newHeight / heightFactor
            End If
        End If

        e.Graphics.DrawImage(mPrintBitmap, 0, 0, CInt(Math.Truncate(newWidth)), CInt(Math.Truncate(newHeight)))

        ' There's only one page.
        e.HasMorePages = False

    End Sub
#End Region
Any help is much appreciated <3
 

superservo15

Member
Joined
Apr 25, 2006
Messages
11
Location
Canada
Programming Experience
10+
Blah nevermind, I think I just needed a fresh set of eyes... on myself...

Moved the line

Dim me_gr As Graphics = Me.CreateGraphics

into my loop and then changed it to

Dim me_gr As Graphics = frm.CreateGraphics

Now it works sweet... I have to add an offset to include the title bar, but at least I get it all now.
I tried to delete this thread but wasn't sure how. Sorry for anyone who might have spent the time reading it haha! Thx
 
Top Bottom