none
Printing/print Preview the whole panel RRS feed

  • Question

  • hello,

    i am facing an issue in printing and print preview of panel containing various user controls(containing images  and textboxs).

    in fact my panel is scroll-able but when i press the print or print preview button , it shows me only a certain part of the panel and not the whole panel.(in other words ,if i place a user control at the bottom of the panel ,scrolled  up the panel and pressed the print/print preview button ,it doesn't show the user control at the bottom) 

    for example if i scrolled all the way down ,the print preview will show me a page with a grey line the middle which represent the bottom of my panel .

    panel's size :1093,1428

     if anyone can help me pls .thank you 

    Tuesday, September 11, 2018 12:10 AM

Answers

  • You can adapt this as required. See where the panel size is set in the formload event?

    A memory image of the panel is captured with DrawToBitmap and that image is then scaled and centered to the printdocument margins.

    All the example does is show the print preview in the form load event. To print click the print button on the print preview dialog.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Text = "Print Panel Example"
            Panel1.Size = New Size(1200, 1600)
            Panel1.BackgroundImage = Image.FromFile("c:\bitmaps\c2.png")
            Panel1.BackgroundImageLayout = ImageLayout.Zoom
    
            Dim preview As New PrintPreviewDialog
            Dim pd As New System.Drawing.Printing.PrintDocument
            'pd.DefaultPageSettings.Landscape = True
            AddHandler pd.PrintPage, AddressOf OnPrintPage
            preview.Document = pd
            preview.ShowDialog()
    
    
        End Sub
    
    
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    
            'create a memory bitmap and size to the panel 
            Using bmp As Bitmap = New Bitmap(Panel1.Width, Panel1.Height)
    
                'draw the panel on the memory bitmap
                Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
    
                'draw the panel image on the printer graphics sized and centered to margins
                Dim ratio As Single = CSng(bmp.Width / bmp.Height)
    
                If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
    
                    e.Graphics.DrawImage(bmp,
                                        e.MarginBounds.Left,
                                        CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - ((e.MarginBounds.Width / ratio) / 2)),
                                        e.MarginBounds.Width,
                                        CInt(e.MarginBounds.Width / ratio))
    
                Else
                    e.Graphics.DrawImage(bmp,
                                         CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (e.MarginBounds.Height * ratio / 2)),
                                         e.MarginBounds.Top,
                                         CInt(e.MarginBounds.Height * ratio),
                                         e.MarginBounds.Height)
                End If
            End Using
        End Sub
    End Class
    


    • Edited by tommytwotrain Tuesday, September 11, 2018 2:27 AM
    • Marked as answer by marounb98 Monday, April 29, 2019 11:30 AM
    Tuesday, September 11, 2018 2:24 AM

All replies

  • You can adapt this as required. See where the panel size is set in the formload event?

    A memory image of the panel is captured with DrawToBitmap and that image is then scaled and centered to the printdocument margins.

    All the example does is show the print preview in the form load event. To print click the print button on the print preview dialog.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Text = "Print Panel Example"
            Panel1.Size = New Size(1200, 1600)
            Panel1.BackgroundImage = Image.FromFile("c:\bitmaps\c2.png")
            Panel1.BackgroundImageLayout = ImageLayout.Zoom
    
            Dim preview As New PrintPreviewDialog
            Dim pd As New System.Drawing.Printing.PrintDocument
            'pd.DefaultPageSettings.Landscape = True
            AddHandler pd.PrintPage, AddressOf OnPrintPage
            preview.Document = pd
            preview.ShowDialog()
    
    
        End Sub
    
    
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    
            'create a memory bitmap and size to the panel 
            Using bmp As Bitmap = New Bitmap(Panel1.Width, Panel1.Height)
    
                'draw the panel on the memory bitmap
                Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
    
                'draw the panel image on the printer graphics sized and centered to margins
                Dim ratio As Single = CSng(bmp.Width / bmp.Height)
    
                If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
    
                    e.Graphics.DrawImage(bmp,
                                        e.MarginBounds.Left,
                                        CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - ((e.MarginBounds.Width / ratio) / 2)),
                                        e.MarginBounds.Width,
                                        CInt(e.MarginBounds.Width / ratio))
    
                Else
                    e.Graphics.DrawImage(bmp,
                                         CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (e.MarginBounds.Height * ratio / 2)),
                                         e.MarginBounds.Top,
                                         CInt(e.MarginBounds.Height * ratio),
                                         e.MarginBounds.Height)
                End If
            End Using
        End Sub
    End Class
    


    • Edited by tommytwotrain Tuesday, September 11, 2018 2:27 AM
    • Marked as answer by marounb98 Monday, April 29, 2019 11:30 AM
    Tuesday, September 11, 2018 2:24 AM
  • okay thanks for your help/

    Tuesday, September 11, 2018 8:51 PM
  • Hi,

    Do you resolve the issue? If you resolve the issue, could you please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 12, 2018 1:30 AM