Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

Print and Scale the client portion of a form

I have a form with multiple grids on it and I need to capture the form data and control and have the image scale to fit a regular 8X10 piece of paper.
I have tried using the PrintForm control that comes with the Powerpack, but there seems to be no option to scale.
I also added the pagesetupdialog, thinking that I could set the margins real small and then the whole sheet would show, but that doesn't work.
The only thing that will get the whole screen on one sheet of paper is resizing the form to make it smaller, which is something I don't want users to have to do.
What is the best way to capture a form, display it as landscape to fit on one piece of paper?
Please provide an example,
Thank you
Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Click
 
        If Me.TabControl1.SelectedIndex = 2 Then 'print form
            Dim psd As New PageSetupDialog
    
            Dim ppd As New PrintPreviewDialog
            Dim Printer As New Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer
 
***'Was going to try and see if I could get the VB6.ScaleMode to work, but no luck
            'Printer.ScaleMode = Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.ScaleModeConstants.vbInches
            'Printer.ScaleLeft = -0.5
            'Printer.ScaleTop = -0.5
            'Printer.CurrentX = 0
            'Printer.CurrentY = 0
 
 
            Dim pd As New Printing.PrintDocument
            
            psd.Document = pd
            psd.PageSettings.Landscape = True
            psd.AllowMargins = True
            psd.PageSettings.Margins.Top = 0.2
          
            If psd.ShowDialog = Windows.Forms.DialogResult.OK Then
 
                Me.PrintForm1.PrinterSettings = psd.PrinterSettings
 
                If PrintForm1.PrinterSettings.IsValid Then
                    Me.PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
 
                    Me.PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
 
                End If
            End If
            '
        End If
 
end sub

Open in new window

Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Try this approach...

1.) Printscreen the form.
2.) Get the data from the clipboard then store it in an image variable.
3.) After storing it to an image file, you now have the option, of resizing the image according to the size you want.

 SendKeys.SendWait("%{PRTSC}")
 
Dim myImage as image
 
   If Not (Clipboard.GetDataObject Is Nothing) Then
      Dim data As IDataObject = Clipboard.GetDataObject
 
      If data.GetDataPresent(DataFormats.Bitmap) Then
         myImage = CType(data.GetData(DataFormats.Bitmap, True), Image)
      End If
 
   End If

Open in new window

If you need further help, just post...
Avatar of Sheritlw

ASKER

Hi,

My application is a mdi app and I don't want to print the mdi parent portion, it has panels etc on it.
I just want to print the client portion of the active mdi child.
Thanks
Do you have screenshots of the form?
I can take a screen shot and upload it, if that will help
Please do so...
Thanks so much for your help.
I've attached a screen shot that notes what I would like printed to a print preview screen.
Thanks again
ScreenShotBuild.jpg
ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well I think I finally got it working.
I believe I was having problems because of the splitcontrol and tabcontrol.  I had to make adjustments to make sure everything fit on the screen.  I also do a print preview of the screen.
I've attached the code.
Thank you very much for your help!
    Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (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 Long
    Dim memoryImage As Bitmap
 
 
Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Click
        If Me.TabControl1.SelectedIndex = 2 Then 'print form
            Dim ppd As New PrintPreviewDialog
 
            CaptureTab()
            PrintDocument1.DefaultPageSettings.Landscape = True
            ppd.Document = PrintDocument1
            ppd.ShowDialog()
            
        End If
        
    End Sub
 
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.DrawImage(memoryImage, 50, 50)
    End Sub
 
  Private Sub CaptureTab()
        Dim mygraphics As Graphics = Me.CreateGraphics()
        Dim s As Size = Me.Size
        memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        Dim dc1 As IntPtr = mygraphics.GetHdc
        Dim dc2 As IntPtr = memoryGraphics.GetHdc
        With Me.TabControl1.TabPages(2).ClientRectangle
            BitBlt(dc2, 0, 0, .Width, .Height, dc1, Me.TabControl1.TabPages(2).Location.X, Me.TabControl1.TabPages(2).Location.Y + 25, 13369376)
        End With
 
        mygraphics.ReleaseHdc(dc1)
        memoryGraphics.ReleaseHdc(dc2)
 
    End Sub

Open in new window