admin管理员组

文章数量:1202793

I am working in a project where I have to print a marksheet. I am using the following code. this code is working fine in my system but when I try to print on the client system, it's not printing properly, the edges are cutting. I am using a 16" screen(laptop) and the client is using 20" monitor.

Dim printDoc As New PrintDocument()
printDoc.DefaultPageSettings.PaperSize = New PaperSize("A4", 827, 1169)
printDoc.DefaultPageSettings.Landscape = True
AddHandler printDoc.PrintPage, AddressOf PrintPageHandler
' Set print settings
'printDoc.PrinterSettings.PrintQuality = PrintQuality.High

printDoc.Print()
Me.FormBorderStyle = FormBorderStyle.Sizable

Printbtn.Visible = True
Prevbtn.Visible = True
Nextbtn.Visible = True
Firstbtn.Visible = True
Lastbtn.Visible = True
Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)
    Dim formBitmap As New Bitmap(Me.Width, Me.Height, Imaging.PixelFormat.Format64bppArgb)
    Me.DrawToBitmap(formBitmap, New Rectangle(0, 0, Me.Width, Me.Height))

    ' Draw the bitmap onto the print page
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    e.Graphics.DrawImage(formBitmap, 0, 0)

    ' Clean up
    formBitmap.Dispose()

    e.HasMorePages = False
End Sub

I am working in a project where I have to print a marksheet. I am using the following code. this code is working fine in my system but when I try to print on the client system, it's not printing properly, the edges are cutting. I am using a 16" screen(laptop) and the client is using 20" monitor.

Dim printDoc As New PrintDocument()
printDoc.DefaultPageSettings.PaperSize = New PaperSize("A4", 827, 1169)
printDoc.DefaultPageSettings.Landscape = True
AddHandler printDoc.PrintPage, AddressOf PrintPageHandler
' Set print settings
'printDoc.PrinterSettings.PrintQuality = PrintQuality.High

printDoc.Print()
Me.FormBorderStyle = FormBorderStyle.Sizable

Printbtn.Visible = True
Prevbtn.Visible = True
Nextbtn.Visible = True
Firstbtn.Visible = True
Lastbtn.Visible = True
Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)
    Dim formBitmap As New Bitmap(Me.Width, Me.Height, Imaging.PixelFormat.Format64bppArgb)
    Me.DrawToBitmap(formBitmap, New Rectangle(0, 0, Me.Width, Me.Height))

    ' Draw the bitmap onto the print page
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    e.Graphics.DrawImage(formBitmap, 0, 0)

    ' Clean up
    formBitmap.Dispose()

    e.HasMorePages = False
End Sub
Share Improve this question asked Jan 21 at 5:34 Sujay GhoshSujay Ghosh 12 bronze badges 1
  • e.Graphics.DrawImage(formBitmap, e.MarginBounds, 0, 0, formBitmap.Width, formBitmap.Height, GraphicsUnit.Pixel) – dr.null Commented Jan 21 at 12:55
Add a comment  | 

1 Answer 1

Reset to default 0

When drawing the bitmap, scale it to fit the print area.

    Dim printArea As RectangleF = e.MarginBounds

    Dim scaleX As Single = printArea.Width / formBitmap.Width
    Dim scaleY As Single = printArea.Height / formBitmap.Height
    Dim scale As Single = Math.Min(scaleX, scaleY) ' Scale proportionally

    Dim newWidth As Integer = CInt(formBitmap.Width * scale)
    Dim newHeight As Integer = CInt(formBitmap.Height * scale)

    Dim xPos As Integer = printArea.Left + (printArea.Width - newWidth) / 2
    Dim yPos As Integer = printArea.Top + (printArea.Height - newHeight) / 2

本文标签: visual studioCan39t print document in vbnetStack Overflow