Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Dynamically set page size when printing from VB.NET

  • 18-02-2010 5:53pm
    #1
    Registered Users, Registered Users 2 Posts: 7,541 ✭✭✭


    Hey all,

    I'm trying to get this working but so far without luck. I'm developing an ap in VB.net that part of which takes an image file (.jpg), annotates it with some text and prints the output. Depending on the size of the image, it should print to either A4 or A3. Everything works apart from the page sizing. A print job that I expect to print on A3 prints on A4 and isn't scaled to fit so only a portion of the image is printed.

    Anyone got any ideas?

    Code follows:

    Main code:
    Private WithEvents PD As PrintDocument
         PD = New PrintDocument
                        PD.PrinterSettings.PrinterName = "Printer name"
                        Dim bm As New Bitmap(path to original image file)
                        bm2 = New Bitmap(bm.Width, bm.Height) 'create new bitmap for edited image
                        bm2.SetResolution(bm.HorizontalResolution, bm.VerticalResolution)
                        Dim pageSize As String = DeterminePageSize(bm2.Width, bm2.Height)
                        Dim pageOrientation As String = DetermineOrientation(bm2.Width, bm2.Height, pageSize)
                        If pageOrientation = "Landscape" Then
                            PD.DefaultPageSettings.Landscape = True
                        Else
                            PD.DefaultPageSettings.Landscape = False
                        End If
                        If pageSize = "A3" Then
                            PD.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("A3", 1169, 1654)
                        Else
                            PD.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("A4", 830, 1170)
                        End If
    
                        Using gr As Graphics = Graphics.FromImage(bm2)
                            gr.DrawImage(bm, 0, 0, bm.Width, bm.Height) 'copy image from original
          'more code here that adds some text to the new bitmap etc
                            
                        End Using
                        PD.DefaultPageSettings.Margins = New Printing.Margins(40, 40, 40, 40)
                        PD.OriginAtMargins = True
                        PD.Print() ' print the image
                        'clear up
                        'bm2.Save("new filename", System.Drawing.Imaging.ImageFormat.Jpeg)  --used for debugging only
                        bm.Dispose()
                        bm2.Dispose()
                        PD.Dispose()
    

    Functions to determine orientation and size based on original image size:
    Private Function DeterminePageSize(ByVal x As Integer, ByVal y As Integer) As String
            If (x <= 210 And y <= 297) Or (x <= 297 And y <= 210) Then
                Return "A4"
            Else
                Return "A3"
            End If
        End Function
     
     Private Function DetermineOrientation(ByVal x As Integer, ByVal y As Integer, ByVal PageSize As String) As String
            If PageSize = "A4" Then
                If (x > 210 And x > y) Then
                    Return "Landscape"
                Else
                    Return "Portrait"
                End If
            Else
                If (x > 297 And x > y) Then
                    Return "Landscape"
                Else
                    Return "Portrait"
                End If
            End If
        End Function
    

    This sub should scale image to fit in the printable area of the page
    Private Sub FitPictureToMargins(ByVal gr As Graphics, ByVal picture_bounds As RectangleF, ByVal margin_bounds As RectangleF)
            ' Remove any existing transformation.
            gr.ResetTransform()
            ' Translate to center picture_bounds at the origin.
            gr.TranslateTransform( _
                -(picture_bounds.Left + picture_bounds.Width / 2), _
                -(picture_bounds.Top + picture_bounds.Height / 2))
            ' Scale to make picture_bounds fit margin_bounds.
            ' Compare the aspect ratios.
            Dim margin_aspect As Single = margin_bounds.Height / margin_bounds.Width
            Dim picture_aspect As Single = picture_bounds.Height / picture_bounds.Width
            Dim scale As Single
            If picture_aspect > margin_aspect Then
                ' picture_bounds is relatively tall and thin.
                ' Make it as tall as possible.
                scale = margin_bounds.Height / picture_bounds.Height
            Else
                ' picture_bounds is relatively short and wide.
                ' Make it as wide as possible.
                scale = margin_bounds.Width / picture_bounds.Width
            End If
            ' Scale.
            gr.ScaleTransform(scale, scale, MatrixOrder.Append)
            ' Translate to move the origin to the center of margin_bounds.
            gr.TranslateTransform( _
                margin_bounds.Left + margin_bounds.Width / 2, _
                margin_bounds.Top + margin_bounds.Height / 2, _
                MatrixOrder.Append)
        End Sub
    


Comments

  • Registered Users, Registered Users 2 Posts: 7,541 ✭✭✭irlrobins


    Turns out my code was working fine and the printer was the issue. It wasn't correctly configured to handle A3 so kept trying to revert to printing on A4. Tested on a correctly configured printer and all is well.


Advertisement