Inconsistent page orientation issue

  • Thread starter Thread starter MK
  • Start date Start date
M

MK

Hi,

I'm using the code below to print an image (trying to understand the
print methods). The issue is that when I first start the application
and print it prints in portrait orientation when it should print
landscape, but subsequent printing attempts print in landscape as
expected. I shut down the application, then start it and the same
thing occurs.

It may be a setting in the 2nd parameter of the method (e) but I have
no idea. Any ideas on what can be causing the issue?

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

Dim PrintAreaHeight As Int32
Dim PrintAreaWidth As Int32
Dim marginLeft As Int32
Dim marginTop As Int32

With PrintDocument1.DefaultPageSettings

' Set the bounds of the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

' Set the margin values for the upper left corner
marginLeft = .Margins.Left
marginTop = .Margins.Top

End With

' Determine the orientation of the image
If mobjImage.Height >= mobjImage.Width Then
' Taller than wide
PrintDocument1.DefaultPageSettings.Landscape = False

e.PageSettings.Landscape = False
Else
' Wider than tall
PrintDocument1.DefaultPageSettings.Landscape = True

e.PageSettings.Landscape = True

Console.Write(vbCrLf & "PrintDocument1_PrintPage : Orientation
landscape")
End If

'Swap the printing area height and width for landscape.
'Not a good way to do this
If PrintDocument1.DefaultPageSettings.Landscape Then
Dim iTemp As Int32
iTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = iTemp
End If

' Define printing area
Dim objRectPrintArea As New RectangleF(marginLeft, _
marginTop, _
PrintAreaWidth, _
PrintAreaHeight)

e.Graphics.DrawImage(mobjImage, objRectPrintArea)
e.HasMorePages = False

End Sub



Regards,

Michael
 
Hi,

I'm using the code below to print an image (trying to understand the
print methods). The issue is that when I first start the application
and print it prints in portrait orientation when it should print
landscape, but subsequent printing attempts print in landscape as
expected. I shut down the application, then start it and the same
thing occurs.

It may be a setting in the 2nd parameter of the method (e) but I have
no idea. Any ideas on what can be causing the issue?

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

Dim PrintAreaHeight As Int32
Dim PrintAreaWidth As Int32
Dim marginLeft As Int32
Dim marginTop As Int32

With PrintDocument1.DefaultPageSettings

' Set the bounds of the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

' Set the margin values for the upper left corner
marginLeft = .Margins.Left
marginTop = .Margins.Top

End With

' Determine the orientation of the image
If mobjImage.Height >= mobjImage.Width Then
' Taller than wide
PrintDocument1.DefaultPageSettings.Landscape = False

e.PageSettings.Landscape = False
Else
' Wider than tall
PrintDocument1.DefaultPageSettings.Landscape = True

e.PageSettings.Landscape = True

Console.Write(vbCrLf & "PrintDocument1_PrintPage : Orientation
landscape")
End If

'Swap the printing area height and width for landscape.
'Not a good way to do this
If PrintDocument1.DefaultPageSettings.Landscape Then
Dim iTemp As Int32
iTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = iTemp
End If

' Define printing area
Dim objRectPrintArea As New RectangleF(marginLeft, _
marginTop, _
PrintAreaWidth, _
PrintAreaHeight)

e.Graphics.DrawImage(mobjImage, objRectPrintArea)
e.HasMorePages = False

End Sub

Regards,

Michael

Helpful bunch around here. I worked it out anyway, but would have
liked further comments on the code.
 
Back
Top