locked
VB.NET - Print the panel area using printdocument RRS feed

  • Question

  • Hello All,

    I am trying to print panel/form area into paper using printdocument, below is my code.

        Private Sub PrintDocumentIR_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocumentIR.PrintPage

            Using bm As New Bitmap(Me.pnlReceiptInvoice.Width, Me.pnlReceiptInvoice.Height)
                Me.pnlReceiptInvoice.DrawToBitmap(bm, New Rectangle(0, 0, Me.pnlReceiptInvoice.Width, Me.pnlReceiptInvoice.Height))

                Dim x As Double = Me.pnlReceiptInvoice.Location.X - Me.pnlReceiptInvoice.Location.X
                x = x * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width
                x = x + e.MarginBounds.X

                Dim y As Double = Me.pnlReceiptInvoice.Location.Y - Me.pnlReceiptInvoice.Location.Y
                y = y * e.MarginBounds.Height / Me.pnlReceiptInvoice.Height
                y = y + e.MarginBounds.Y

                Dim width As Double = Me.pnlReceiptInvoice.Width
                width = width * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width

                Dim height As Double = Me.pnlReceiptInvoice.Height
                height = height * e.MarginBounds.Height / Me.pnlReceiptInvoice.Height

                e.Graphics.DrawImage(bm, New Rectangle(x, y, width, height))
            End Using

        End Sub

    But, as per below picture, the result for print area is too small, may I know what should I do to change my above code to resize the print area as per below picture, I am using paper A5..

    • Moved by lake Xiao Monday, August 1, 2016 5:17 AM
    Friday, July 29, 2016 3:40 PM

Answers

  • Hi Indra[Thagor],

    According to your description, you want to resize the print area as the above picture. And I tried to reproduce the code which you provided. I suggest you to change the “width” and “height” values to resize the print area, and use x and y to control the print area’s location. Like this:

    Dim x As Double = Me.pnlReceiptInvoice.Location.X - Me.pnlReceiptInvoice.Location.X
    
    x = x * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width
    
    x = x + e.MarginBounds.X - 40
    
    
    Dim y As Double = Me.pnlReceiptInvoice.Location.Y - Me.pnlReceiptInvoice.Location.Y
    
    y = y * e.MarginBounds.Height / Me.pnlReceiptInvoice.Height
    
    y = y + e.MarginBounds.Y - 40
    
    
    Dim width As Double = Me.pnlReceiptInvoice.Width
    
    width = width * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width + 100
    
    
    Dim height As Double = Me.pnlReceiptInvoice.Height
    
    height = height * e.MarginBounds.Height / Me.pnlReceiptInvoice.Height + 100
    

    I hope it will be helpful to you.

    Best Regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 1, 2016 6:38 AM

All replies

  • Hi Indra,

    Since your question is more related to the VB.NET development. I moved your thread to our VB forum for better support.

    Thanks for your understanding.

    Best Regards,

    Lake Xiao

    Monday, August 1, 2016 3:20 AM
  • Hi Indra[Thagor],

    According to your description, you want to resize the print area as the above picture. And I tried to reproduce the code which you provided. I suggest you to change the “width” and “height” values to resize the print area, and use x and y to control the print area’s location. Like this:

    Dim x As Double = Me.pnlReceiptInvoice.Location.X - Me.pnlReceiptInvoice.Location.X
    
    x = x * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width
    
    x = x + e.MarginBounds.X - 40
    
    
    Dim y As Double = Me.pnlReceiptInvoice.Location.Y - Me.pnlReceiptInvoice.Location.Y
    
    y = y * e.MarginBounds.Height / Me.pnlReceiptInvoice.Height
    
    y = y + e.MarginBounds.Y - 40
    
    
    Dim width As Double = Me.pnlReceiptInvoice.Width
    
    width = width * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width + 100
    
    
    Dim height As Double = Me.pnlReceiptInvoice.Height
    
    height = height * e.MarginBounds.Height / Me.pnlReceiptInvoice.Height + 100
    

    I hope it will be helpful to you.

    Best Regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 1, 2016 6:38 AM
  • But, as per below picture, the result for print area is too small, may I know what should I do to change my above code to resize the print area as per below picture, I am using paper A5.

    Step that code through line-by-line in the debugger, looking at the value of each variable as it is calculated and used.  You might like to declare some additional variables just to be able to look at those other values as you debug.  That should reveal the adjustments you need to make to the calculation to get the size you want.

    Monday, August 1, 2016 6:43 AM
  • As Acamar says you need to debug by steping through the execution and look at the values your code it generating.

    In particular I would check the first line:

    Dim x As Double = Me.pnlReceiptInvoice.Location.X - Me.pnlReceiptInvoice.Location.X

    x
    = x * e.MarginBounds.Width / Me.pnlReceiptInvoice.Width

    After you get yours working look at this one I did that I am quite proud of. There are lots of ways to do it.

    'fit the bitmap to the page margins and center
                Dim l, t, w, h As Integer
                Dim ratio As Single = CSng(bmp.Width / bmp.Height)
    
                If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
                    w = e.MarginBounds.Width
                    h = CInt(w / ratio)
                    t = CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (h / 2))
                    l = e.MarginBounds.Left
                Else
                    h = e.MarginBounds.Height
                    w = CInt(h * ratio)
                    l = CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (w / 2))
                    t = e.MarginBounds.Top
                End If
    
                'now draw the form image on the printer graphics
                e.Graphics.DrawImage(bmp, l, t, w, h)

    Monday, August 1, 2016 1:07 PM