none
Print RRS feed

  • Question

  • I can't print my entire form, it always gets cut off I have looked everywhere for an answer, none have helped my form size is 1920x1080. please help.

    Donny

    Sunday, December 10, 2017 10:24 PM

Answers

  •  Without knowing how you are printing the form,  we could be guessing at an answer all day.

     However,  my example below will scale the image of the form down to fit in the printed document.  You can un-comment the 3 lines at the top of the PrintPage event if you want the image flipped sideways before it is scaled and printed.

    Imports System.ComponentModel
    Imports System.Drawing.Printing
    
    Public Class Form1
        Private WithEvents PrintDocument1 As New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog With {.Document = PrintDocument1}
        Private FormImage As Bitmap = Nothing
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Me.Width, Me.Height)
            Me.DrawToBitmap(FormImage, New Rectangle(0, 0, Me.Width, Me.Height))
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            'un-comment these 3 lines of code if you want the form image to be flipped sideways when it is wider than the page's printing area
            'If FormImage.Width > FormImage.Height AndAlso FormImage.Width > e.MarginBounds.Width Then
            'FormImage.RotateFlip(RotateFlipType.Rotate90FlipNone) 'or use (RotateFlipType.Rotate270FlipNone)
            'End If
    
            Dim CenterBounds As Rectangle
            CenterBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (FormImage.Width \ 2))
            CenterBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (FormImage.Height \ 2))
            CenterBounds.Width = FormImage.Width
            CenterBounds.Height = FormImage.Height
    
            Dim ratio As Double = FormImage.Width / FormImage.Height
    
            If CenterBounds.Width > e.MarginBounds.Width Then
                CenterBounds.Width = e.MarginBounds.Width
                CenterBounds.Height = CInt(CenterBounds.Width / ratio)
                CenterBounds.X = e.MarginBounds.Left
                CenterBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (CenterBounds.Height \ 2))
            End If
    
            If CenterBounds.Height > e.MarginBounds.Height Then
                CenterBounds.Height = e.MarginBounds.Height
                CenterBounds.Width = CInt(CenterBounds.Height * ratio)
                CenterBounds.Y = e.MarginBounds.Top
                CenterBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (CenterBounds.Width \ 2))
            End If
    
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(FormImage, CenterBounds)
        End Sub
    
        Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            PrintDocument1.Dispose()
            PrintPreviewDialog1.Dispose()
            If FormImage IsNot Nothing Then FormImage.Dispose()
        End Sub
    End Class
     

     PS - If your last question about the movable label at the link below has been answered,  then please remember to mark the post or posts that answered it as the answer(s).  Thanks.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/97e82264-c602-412d-89c6-3fe017c28ddd/label?forum=vbgeneral#794872b9-6811-45ce-b351-118fe77fee01


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 11, 2017 1:01 AM
    • Marked as answer by Scott7465625255 Tuesday, December 12, 2017 4:02 PM
    Sunday, December 10, 2017 11:36 PM
  • just the client area

    Donny

     Well,  i will assume that you do not have the FormBorderStyle set to None.  In that case,  if you change the Button1 click event code in my example to the below code,  it will create an image of just the client area of the form and print that.

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If FormImage IsNot Nothing Then FormImage.Dispose()
    
            Using FullFormImage As New Bitmap(Me.Width, Me.Height)
                Me.DrawToBitmap(FullFormImage, New Rectangle(0, 0, Me.Width, Me.Height))
                FormImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
                Dim ptc As Point = Me.PointToClient(Me.Location)
                Using g As Graphics = Graphics.FromImage(FormImage)
                    g.DrawImage(FullFormImage, ptc.X, ptc.Y)
                End Using
            End Using
    
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
     

     If you use your code,  then you can pretty much just use what i have shown by just changing the (FormImage) in my code above,  to (bitmap) which is the name of the Bitmap image you are printing.

     Again,  if your other question about the movable labels has been answered (link below),  then please remember to go back to your question and mark the post or posts that answered it as the answer(s).  If it is not answered,  then let us know what you have tried so we can help further.  Thanks.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/97e82264-c602-412d-89c6-3fe017c28ddd/label?forum=vbgeneral#97e82264-c602-412d-89c6-3fe017c28ddd


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 11, 2017 12:46 PM
    • Marked as answer by Scott7465625255 Tuesday, December 12, 2017 4:03 PM
    Monday, December 11, 2017 11:31 AM

All replies

  •  Without knowing how you are printing the form,  we could be guessing at an answer all day.

     However,  my example below will scale the image of the form down to fit in the printed document.  You can un-comment the 3 lines at the top of the PrintPage event if you want the image flipped sideways before it is scaled and printed.

    Imports System.ComponentModel
    Imports System.Drawing.Printing
    
    Public Class Form1
        Private WithEvents PrintDocument1 As New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog With {.Document = PrintDocument1}
        Private FormImage As Bitmap = Nothing
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Me.Width, Me.Height)
            Me.DrawToBitmap(FormImage, New Rectangle(0, 0, Me.Width, Me.Height))
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            'un-comment these 3 lines of code if you want the form image to be flipped sideways when it is wider than the page's printing area
            'If FormImage.Width > FormImage.Height AndAlso FormImage.Width > e.MarginBounds.Width Then
            'FormImage.RotateFlip(RotateFlipType.Rotate90FlipNone) 'or use (RotateFlipType.Rotate270FlipNone)
            'End If
    
            Dim CenterBounds As Rectangle
            CenterBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (FormImage.Width \ 2))
            CenterBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (FormImage.Height \ 2))
            CenterBounds.Width = FormImage.Width
            CenterBounds.Height = FormImage.Height
    
            Dim ratio As Double = FormImage.Width / FormImage.Height
    
            If CenterBounds.Width > e.MarginBounds.Width Then
                CenterBounds.Width = e.MarginBounds.Width
                CenterBounds.Height = CInt(CenterBounds.Width / ratio)
                CenterBounds.X = e.MarginBounds.Left
                CenterBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (CenterBounds.Height \ 2))
            End If
    
            If CenterBounds.Height > e.MarginBounds.Height Then
                CenterBounds.Height = e.MarginBounds.Height
                CenterBounds.Width = CInt(CenterBounds.Height * ratio)
                CenterBounds.Y = e.MarginBounds.Top
                CenterBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (CenterBounds.Width \ 2))
            End If
    
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(FormImage, CenterBounds)
        End Sub
    
        Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            PrintDocument1.Dispose()
            PrintPreviewDialog1.Dispose()
            If FormImage IsNot Nothing Then FormImage.Dispose()
        End Sub
    End Class
     

     PS - If your last question about the movable label at the link below has been answered,  then please remember to mark the post or posts that answered it as the answer(s).  Thanks.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/97e82264-c602-412d-89c6-3fe017c28ddd/label?forum=vbgeneral#794872b9-6811-45ce-b351-118fe77fee01


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 11, 2017 1:01 AM
    • Marked as answer by Scott7465625255 Tuesday, December 12, 2017 4:02 PM
    Sunday, December 10, 2017 11:36 PM
  • I have looked everywhere for an answer, none have helped my form size is 1920x1080. please help.

    What have you tried?  Without listing what you have already done you are likely only going to get the same suggestions, with the same results.

    Sunday, December 10, 2017 11:47 PM
  • this is what I have tried
    Private bitmap As Bitmap
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim grp As Graphics = Me.CreateGraphics()
            Dim formSize As Size = Me.ClientSize
            bitmap = New Bitmap(formSize.Width, formSize.Height, grp)
            grp = Graphics.FromImage(bitmap)
    PrintDocument1.DefaultPageSettings.Landscape = True
            PrintDocument1.Print()
    end sub
    Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        Dim b As New Bitmap(Me.Width, Me.Height)
        Me.DrawToBitmap(b, Me.ClientRectangle)
    
           e.Graphics.DrawImage(bitmap, New Point(0, 0))
        End Sub

    and
     
     PrintForm1.Form = Me  
         Me.PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
                   Me.PrintForm1.Print()



    Donny

    Monday, December 11, 2017 1:04 AM
  •  If your form is 1920x1080 then it is most likely going to be cut off no matter what size page you print it to.  You will most likely need to scale the image down to fit the page.  This is what my example does,  scales the image down to fit inside the page margins when it is printing the image.  It also centers it in the page.  Give it a try in a new form project with 1 button added to the form.

     Looking at your code,  i notice you are creating a Bitmap that is only the size of the Client area of the form (Me.ClientSize).  That is only good if your form has the FormBorderStyle set to None.  If it is not,  then that is where you are cutting off the right side and the bottom side of your form image.

     Are you trying to print an image of just the Client area of the form,  without the caption area and borders,  or do you want to print the whole form? 

     Is the FormBorderStyle set to None?


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 11, 2017 2:17 AM
    Monday, December 11, 2017 2:16 AM
  • just the client area

    Donny

    Monday, December 11, 2017 4:30 AM
  • just the client area

    Donny

     Well,  i will assume that you do not have the FormBorderStyle set to None.  In that case,  if you change the Button1 click event code in my example to the below code,  it will create an image of just the client area of the form and print that.

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If FormImage IsNot Nothing Then FormImage.Dispose()
    
            Using FullFormImage As New Bitmap(Me.Width, Me.Height)
                Me.DrawToBitmap(FullFormImage, New Rectangle(0, 0, Me.Width, Me.Height))
                FormImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
                Dim ptc As Point = Me.PointToClient(Me.Location)
                Using g As Graphics = Graphics.FromImage(FormImage)
                    g.DrawImage(FullFormImage, ptc.X, ptc.Y)
                End Using
            End Using
    
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
     

     If you use your code,  then you can pretty much just use what i have shown by just changing the (FormImage) in my code above,  to (bitmap) which is the name of the Bitmap image you are printing.

     Again,  if your other question about the movable labels has been answered (link below),  then please remember to go back to your question and mark the post or posts that answered it as the answer(s).  If it is not answered,  then let us know what you have tried so we can help further.  Thanks.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/97e82264-c602-412d-89c6-3fe017c28ddd/label?forum=vbgeneral#97e82264-c602-412d-89c6-3fe017c28ddd


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 11, 2017 12:46 PM
    • Marked as answer by Scott7465625255 Tuesday, December 12, 2017 4:03 PM
    Monday, December 11, 2017 11:31 AM