none
Print Form RRS feed

Answers

  • Now I got my contents are small and area of paper is large size so I want to scale them up as in picture, Do you have any idea how I can scale them up?

     As Tom already said,  scaling an image up will cause it to become blurry looking.  However,  you can try it with the example below if you want.  It will scale the image down if it is larger than the margin bounds of the page,  or scale it up if the image is smaller than the margin bounds of the page.

     Also,  take note that i have changed the code that sets the Paper size and other properties of the PrintDocument.  I did not notice the other day that my code was not actually changing the Paper size,  so that is fixed in this example.

    Imports System.Drawing.Printing
    
    Public Class Form1
        Private WithEvents PrintDoc As New PrintDocument
        Private FormImage As Bitmap = Nothing
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'draw the whole panel
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(FormImage, New Rectangle(0, 0, FormImage.Width, FormImage.Height))
            Try
                With PrintDoc
                    .PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer if you do not want the system's default printer used
                    .PrinterSettings.DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDoc.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.Folio).FirstOrDefault 'sets the paper size to A4 or Folio
                    .DefaultPageSettings.Margins = New Margins(0, 0, 0, 0) 'set the margins of the page to all 0 if you want no margins
                    .Print()
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'only draw the controls
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Panel1.Width, Panel1.Height)
            For Each c As Control In Panel1.Controls
                c.DrawToBitmap(FormImage, c.Bounds)
            Next
            Try
                With PrintDoc
                    .PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer if you do not want the system's default printer used
                    .PrinterSettings.DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDoc.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.Folio).FirstOrDefault 'sets the paper size to A4 or Folio
                    .DefaultPageSettings.Margins = New Margins(0, 0, 0, 0) 'set the margins of the page to all 0 if you want no margins
                    .Print()
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage
            Dim ImgBounds As Rectangle = New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, FormImage.Width, FormImage.Height)
            Dim ratio As Double = ImgBounds.Width / ImgBounds.Height
    
            While ImgBounds.Width > e.MarginBounds.Width OrElse ImgBounds.Height > e.MarginBounds.Height 'scale the image down if bigger than margin bounds
                ImgBounds.Height -= 1
                ImgBounds.Width = CInt(ImgBounds.Height * ratio)
                ImgBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (ImgBounds.Width \ 2))
                ImgBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (ImgBounds.Height \ 2))
            End While
    
            While ImgBounds.Width < e.MarginBounds.Width AndAlso ImgBounds.Height < e.MarginBounds.Height 'scale the image up if it is smaller than margin bounds
                ImgBounds.Height += 1
                ImgBounds.Width = CInt(ImgBounds.Height * ratio)
                ImgBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (ImgBounds.Width \ 2))
                ImgBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (ImgBounds.Height \ 2))
            End While
    
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(FormImage, ImgBounds)
        End Sub
    End Class
    
     

     I think it would be better to do one of the following... 

     1.  Resize your panels and controls so that they are as close as possible to the page size you are printing on.

     2. Pick the page size you are going to always use,  and just draw the information on the page as Text instead of drawing an image of the form.  You can even draw shapes too if needed.  You can make the text and shapes look much clearer this way.  It might take a little more work but,  it would always look cleaner,  use less printer ink,  and maybe look more professional.


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

    • Marked as answer by DearBaba Wednesday, January 24, 2018 3:46 PM
    Sunday, January 21, 2018 6:51 PM
  • IronRazerz

    AutoScroll property is already True in the form1 and so as panel1 also

    but still error shows


     Here is a link to an example of using the method i showed,  printing the whole panel as an image,  and the method to just draw the controls on a bitmap and print that.  Download it and see if it helps you understand what you may or may not be doing correctly.

     My Example - 'Print Scrollable Client Area Of Form'

     To open it in Visual Studio,  open the "Print Scrollable Client Area Of Form.vbproj" file.


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

    • Edited by IronRazerz Saturday, January 20, 2018 4:32 PM
    • Marked as answer by DearBaba Saturday, January 20, 2018 4:41 PM
    Saturday, January 20, 2018 4:27 PM

All replies

  • Hi DearBaba,

    Welcome to MSDN forum.

    According to your description, your problem is related to Print Form control.  

    But this forum is discussing and ask questions about the Test tools in Visual Studio including web performance, load testing and of course the hot new Coded UI testing.

    I will move this thread to VB forum for a professional answer.

    Thanks for your understanding.

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Judy ZhuY Monday, January 15, 2018 5:54 AM
    Monday, January 15, 2018 5:54 AM
  • Hi DearBaba,

    You said that PrintForm's scrollable is not working, here is the info about Print a Scrollable Form, you can take a look firstly. If you still have some issue about this, please provide your code here, it is helpful for us to analyze your issue.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-scrollable-form

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 15, 2018 8:32 AM
    Moderator
  • PrinForm's scrollable otion is not working when I make form height over 730 px

    kindly help me!

      You have not said what programming language you are using.  We need to know what programming language you are using to be sure your question is in the correct forum.  Are you using Vb.Net,  or C#,  or another?

     It seems as you asked for help with this in THIS OLD THREAD here on the Vb.Net forum yesterday so,  if you are using Vb.Net,  did you try the answers that Tom and Me have given to that person in that link?  Did you also see what Tom and Me said in our replies to you in that thread?


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

    Monday, January 15, 2018 1:55 PM
  • Thax for replies all of you guys

    I am using vb.net in Visual Studio Enterprise 2015 

    but in my this command of scroll able form printing is not working fully means my forms are usually 1200 or 1100 px but scrollable does working.

    as in picture bellow

    i am using this simple command:

     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
     End Sub 

    for printing after page setup ok

    image1:

    https://drive.google.com/open?id=1LfWttGO53Sz6gb1JnEVnLstAOtEFCCCI

    image2:

     https://drive.google.com/open?id=1UtdUNnvZ35Rbfgx5Dc8kKx6nb6kgtzat

    sory my account has no abaility  yet to add picture in this.

    • Edited by DearBaba Thursday, January 18, 2018 4:29 PM
    Thursday, January 18, 2018 4:10 PM
  • Thax for replies all of you guys

    I am using vb.net in Visual Studio Enterprise 2015 

    but in my this command of scroll able form printing is not working fully means my forms are usually 1200 or 1100 px but scrollable does working.

    as in picture bellow

    i am using this simple command:

     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
     End Sub 

    for printing after page setup ok

    image1:

    https://drive.google.com/open?id=1LfWttGO53Sz6gb1JnEVnLstAOtEFCCCI

    image2:

     https://drive.google.com/open?id=1UtdUNnvZ35Rbfgx5Dc8kKx6nb6kgtzat

    sory my account has no abaility  yet to add picture in this.

    As I recall that is one of the problems with power packs printform I would not use it.

    See if these help. One prints a panel with scroll bar or all controls on a panel, one a form. You should be able to make what you want.

    The other choice is start over and draw what you want to print on both the form and the printer with the same routine.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7590d712-4ac3-47c9-926c-d98802060b7f/print-a-panel?forum=vbgeneral


    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea7e92c-7a61-4c65-bacb-2ef66e006717/print-a-form-but-in-lansscape?forum=vbgeneral



    • Edited by tommytwotrain Thursday, January 18, 2018 7:09 PM
    • Proposed as answer by IronRazerz Thursday, January 18, 2018 9:53 PM
    • Marked as answer by DearBaba Friday, January 19, 2018 6:13 PM
    • Unmarked as answer by DearBaba Friday, January 19, 2018 6:13 PM
    Thursday, January 18, 2018 5:45 PM
  •  Perhaps this will help,  it is basically a mix of the two answers that i posted in the two links we already gave you.  It requires you to set the Form's AutoScroll property to True and then place a Panel in the form.  Set the Panel's height to 1100 and it's width as desired.  Place all your controls from the Form into the Panel.

     Then try the code below.  It will scale the image if needed so that it fits in one printed page.  I had two labels and 1 button in my panel.  My Panel's Size is 772x1100 and my Form's Size is 805x486.  I placed one label at the Top and one label at the Bottom of the Panel.  The Button is in the middle of the Panel.

     Example Code...

    Imports System.Drawing.Printing
    
    Public Class Form1
        Private WithEvents PrintDoc As New PrintDocument
        Private FormImage As Bitmap = Nothing
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(FormImage, New Rectangle(0, 0, FormImage.Width, FormImage.Height))
            With PrintDoc
                .DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDoc.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.A4).FirstOrDefault 'sets the paper size to A4
                .DefaultPageSettings.Margins = New Margins(20, 20, 20, 20) 'set the left, right, top, bottom margins for the printed
                '.PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer if you do not want the system's default printer used
                .Print()
            End With
        End Sub
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage
            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
    End Class
    
     

     Here is my example form scrolled to the top at run-time.

     

     Here is my example form scrolled to the bottom...

     

     And here is the form scaled and printed to an A4 page...


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

    Thursday, January 18, 2018 9:29 PM
  • Thx for this use full help dear 

    but scrollable is not working yet 

    i used these code as you give above.

    please give a mailing id to mail you a part of my project, which contains two forms.

    first is form1 which i want to print on Follio paper size with scroll able

    and 2nd one is a receipt form which i want to print on half of the A4 paper with center bound.

    Friday, January 19, 2018 5:30 PM
  • Thx dear

    it is working for scrollable 100% first time like below 

    again shows error of bound targets

    and also center bound with page size like folio , A4 or my customized size like half of A4  


    • Edited by DearBaba Friday, January 19, 2018 6:33 PM
    Friday, January 19, 2018 6:13 PM
  • Thx dear

    it is working for scrollable 100% first time like below 

    again shows error of bound targets

    and also center bound with page size like folio , A4 or my customized size like half of A4  


    Baba,

    If you put your mouse pointer over the word rect in the Dim rect line the debug intellisense? will show the values for rect. If not look at the ctrl.left etc values. Follow the values backwards up the code and try to figure out what is causing the problem. One of the values will be zero or infinity or something and its just a matter of working that out.

    Friday, January 19, 2018 6:43 PM
  • please give a mailing id to mail you a part of my project, which contains two forms.

    first is form1 which i want to print on Follio paper size with scroll able

    and 2nd one is a receipt form which i want to print on half of the A4 paper with center bound.

     Sorry,  i do not give out my email address on public forums.  If you want to share it,  you can upload a zip file of your project folder to the Public folder of your OneDrive account and then share a link to it here on the forum.

     As you can see from the images i posted,  it works fine,  so I am not sure what you might be doing or not doing that is causing it to not work.  I would need to see your project in order to tell.


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

    • Edited by IronRazerz Friday, January 19, 2018 8:13 PM
    Friday, January 19, 2018 7:33 PM
  • Its OK

    first is form1 which i want to print on Follio paper size with scroll able

    and 2nd one is a receipt form which i want to print on half of the A4 paper with center bound.

    and resutt of printing is out of page when from left side a big space is empty. Marines are left = 0 right =0

     


    • Edited by DearBaba Saturday, January 20, 2018 4:04 PM
    Saturday, January 20, 2018 8:45 AM
  • kindly you also check my reply to IronRazerz

    Saturday, January 20, 2018 8:48 AM
  • Saturday, January 20, 2018 2:53 PM
  • How does ctrl.top get to -84? Is it the scroll offset for the panel or something? Keep going back up the code.

    PS where does ymax come from? If it is zero or something then your bmp is not large enough?

    Saturday, January 20, 2018 3:12 PM
  • kindly check my project
    Saturday, January 20, 2018 3:16 PM
  • kindly check my project

    You check it. I am not a slave. I am a volunteer helper. I will help only.

    See my ps. If you cant post the code here then you are not getting help from me as I will not download and run a project from unknown sources.


    Saturday, January 20, 2018 3:27 PM
  • kindly check my project

    You check it. I am not a slave. I am a volunteer helper. I will help only.

    See my ps. If you cant post the code here then you are not getting help from me as I will not download and run a project from unknown sources.


    PS If you need to make a new one form example that reproduces the problem in less than say 100 lines of code, then post it here.

    :)

    Also, make it clear whom you are responding to as required.

    Finally don't take it the wrong way. I am helping you, helping you learn how it works.


    Saturday, January 20, 2018 3:31 PM
  •  That is because you have the Panel's AutoScroll property set to True and you are scrolling the panel,  not the Form.

     What you need to do is set the size of the panel so that all the controls fit in it.  Do not resize it smaller than that and do not set it's AutoScroll property to True either.  Then you can set the Form's AutoScroll property to True.  Then i believe you will find that my example,  and Tom's example will work.

     I believe this is why you said my example did not work for you,  you did not follow the instructions to set the Panel height to 1100 and set the Form's AutoScroll property to True.


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

    • Edited by IronRazerz Saturday, January 20, 2018 3:54 PM
    Saturday, January 20, 2018 3:54 PM
  • Tommy,

    Thx dear i am really thankful to you for helping me.

    but i have no about print form without power tool

    i use your code which is working for some times and make error 



    • Edited by DearBaba Saturday, January 20, 2018 4:19 PM
    Saturday, January 20, 2018 4:01 PM
  • Thx dear i am really thankful to you for helping me.

    but i have no about print form without power tool

    i use your code which is working for some times and make error 


    Whom are you replying to?

    Who's code are you using, Razerz, tommytwotrain? Post the code here if its not Razerz example.

    We know you don't know. You will know when we are done if you follow the instructions.

    :)

    Saturday, January 20, 2018 4:08 PM
  • IronRazerz

    AutoScroll property is already True in the form1 and so as panel1 also

    but still error shows


    • Edited by DearBaba Saturday, January 20, 2018 4:18 PM
    Saturday, January 20, 2018 4:13 PM
  • Dear Tommy

    sorry to doesn't mentioning the name in reply

    but im try both codes separately in two different projects.

    i just want to get my target also learn.

    i re-edit the replies with name



    • Edited by DearBaba Saturday, January 20, 2018 4:22 PM
    Saturday, January 20, 2018 4:17 PM
  • IronRazerz

    Did you get my project?

    Saturday, January 20, 2018 4:25 PM
  • IronRazerz

    AutoScroll property is already True in the form1 and so as panel1 also

    but still error shows


     Here is a link to an example of using the method i showed,  printing the whole panel as an image,  and the method to just draw the controls on a bitmap and print that.  Download it and see if it helps you understand what you may or may not be doing correctly.

     My Example - 'Print Scrollable Client Area Of Form'

     To open it in Visual Studio,  open the "Print Scrollable Client Area Of Form.vbproj" file.


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

    • Edited by IronRazerz Saturday, January 20, 2018 4:32 PM
    • Marked as answer by DearBaba Saturday, January 20, 2018 4:41 PM
    Saturday, January 20, 2018 4:27 PM
  • IronRazerz

    AutoScroll property is already True in the form1 and so as panel1 also

    but still error shows


     Here is a link to an example of using the method i showed,  printing the whole panel as an image,  and the method to just draw the controls on a bitmap and print that.  Download it and see if it helps you understand what you may or may not be doing correctly.

     My Example - 'Print Scrollable Client Area Of Form'


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

    Wow ...

    Thx Dear that is great for scrollable form printing 

    Thx alot

    My 1st problem is resolved.

    but second problem of print a small receipt on half of A4 size with adjusted printer tray is still not resolved.

    kindly help on this issue

    Saturday, January 20, 2018 4:41 PM
  • ...but second problem of print a small receipt on half of A4 size with adjusted printer tray is still not resolved.

     You have not said if it is suppose to be printed on the same page or a separate page.  You also have not said if it should be printed at the top, middle, or bottom of the page either.

     However,  it does not take much to adjust the CenterBounds rectangle to center the receipt at the top, middle, or bottom of the page.  As an example,  i print a panel that is on the ReceiptForm,  at the top center of an A4 page.

     You can also draw just the controls if you want.  Just use the Button2 click event code i showed in the example you downloaded.

    Imports System.Drawing.Printing
    
    Public Class ReceiptForm
        Private WithEvents PrintDoc As New PrintDocument
        Private FormImage As Bitmap = Nothing
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(FormImage, New Rectangle(0, 0, FormImage.Width, FormImage.Height))
            With PrintDoc
                .DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDoc.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.A4).FirstOrDefault 'sets the paper size to A4
                .DefaultPageSettings.Margins = New Margins(20, 20, 20, 20) 'set the left, right, top, bottom margins for the printed page
                .PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer if you do not want the system's default printer used
                Try
                    .Print()
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            End With
        End Sub
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage
            Dim TopCenterBounds As Rectangle
            TopCenterBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (FormImage.Width \ 2))
            TopCenterBounds.Y = e.MarginBounds.Top
            TopCenterBounds.Width = FormImage.Width
            TopCenterBounds.Height = FormImage.Height
    
            Dim ratio As Double = FormImage.Width / FormImage.Height
    
            If TopCenterBounds.Width > e.MarginBounds.Width Then
                TopCenterBounds.Width = e.MarginBounds.Width
                TopCenterBounds.Height = CInt(TopCenterBounds.Width / ratio)
                TopCenterBounds.X = e.MarginBounds.Left
            End If
    
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(FormImage, TopCenterBounds)
        End Sub
    End Class


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

    Saturday, January 20, 2018 5:15 PM
  • but second problem of print a small receipt on half of A4 size with adjusted printer tray is still not resolved.

    kindly help on this issue

    Baba,

    You may need to set the correct paper size and if that is a special tray etc as you show in your picture of the printer with the half a4 in a special tray. See where that is set in Razerz example posted above?

    That's why if you understand how the code works, how it draws, how it centers the image, how it sets up the correct printer settings, etc then you can modify what you have. One day Razerz may not be here when you need help.

    :)


    PS I suppose you could set the margins larger for the half a4 size and still use the a4 size settings.
    Saturday, January 20, 2018 5:26 PM
  •  I agree with Tom,  you need to understand the code and how it works in order to modify the code to work the way you want it to.  Please read the msdn documents for the PrintDocument Class.  Follow the links that are there to find more information on the Properties,  Methods,  and Events that are shown for the PrintDocument class.

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

    Saturday, January 20, 2018 5:42 PM
  • but second problem of print a small receipt on half of A4 size with adjusted printer tray is still not resolved.

    kindly help on this issue

    Baba,

    You may need to set the correct paper size and if that is a special tray etc as you show in your picture of the printer with the half a4 in a special tray. See where that is set in Razerz example posted above?

    That's why if you understand how the code works, how it draws, how it centers the image, how it sets up the correct printer settings, etc then you can modify what you have. One day Razerz may not be here when you need help.

    :)

    Tommy Dear,

    Yes you are right but i just add these codes after modifies and set according to my requirements. mean now i have download these example codes then study that and will modify them according to my requirement easily, before this I have no idea that form could be printed without power tools.

    And also it is very important to me to understand these commands how they are working. specially page bounds.

    I just try them & then tell you

    thx again 

    Saturday, January 20, 2018 5:45 PM
  • Tommy Dear,

    Yes you are right but i just add these codes after modifies and set according to my requirements. mean now i have download these example codes then study that and will modify them according to my requirement easily, before this I have no idea that form could be printed without power tools.

    And also it is very important to me to understand these commands how they are working. specially page bounds.

    I just try them & then tell you

    thx again 

    Baba,

    Yes. It takes time. Everyone goes through it.

    It is very hard to communicate on a forum in writing, especially when we speak different languages.

    One way to learn the code is to watch it run one line at a time using the debugging tools built into Visual Studio. You can SINGLE STEP execution one line at a time and look at where it goes, look at the values, use the F1 help to read the docs on each code word etc. That is what is great about Visual Studios debugging tools.

    To center the image you have to understand the math. You have to understand it enough to do it by hand. Then understand and write the code to give the same result you get by hand.

    But first you have to understand that the paper size is, what units it is, where it comes from, and same for the margins.

    So that is the other thing we try to show you by telling you check this or look at that.

    And sometimes we are telling you things you may already know.

    Saturday, January 20, 2018 6:01 PM
  • Baba,

    Here is a more basic example for starters if you need it.

    See how I draw the default margins which are 1 inch wide or (100 hundredths of an inch) with a dashed line? Then I offset the left and right margin by 200 hundredths of an inch and draw the solid rect.

    Public Class Form6
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Print Margins Example"
    
            'add the print document and print preview controls to the form
            'you can also do this by dragging an dropping the controls from the toolbox
            'but this is just as easy and then others dont have to do it to run the example
            Dim pd As New System.Drawing.Printing.PrintDocument
    
            'add the sub routine to call when ready to print
            AddHandler pd.PrintPage, AddressOf OnPrintPage
    
            'add and print preview 
            Dim preview As New PrintPreviewDialog
            preview.Document = pd    'tell the preview what is being printed
    
            'showing the priview dialog will fire the printPage event
            'and call the print event
            'to print on paper (or selected printer) click the print button
            'on the print dialog
            preview.ShowDialog()
    
        End Sub
    
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    
            Using p As New Pen(Color.Black, 1)
                p.DashStyle = Drawing2D.DashStyle.Dash
    
                'draw the margins
                e.Graphics.DrawRectangle(p, e.MarginBounds)
    
                'draw a rect inside the margins
                p.Width = 5
                p.DashStyle = Drawing2D.DashStyle.Solid
    
                Dim rect As New Rectangle(e.MarginBounds.X + 200, e.MarginBounds.Y, e.MarginBounds.Width - 400, e.MarginBounds.Height)
                e.Graphics.DrawRectangle(p, rect)
            End Using
    
        End Sub
    End Class


    Saturday, January 20, 2018 8:18 PM
  •     Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage
            Dim TopCenterBounds As Rectangle
            TopCenterBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (FormImage.Width \ 2))
            TopCenterBounds.Y = e.MarginBounds.Top
            TopCenterBounds.Width = FormImage.Width
            TopCenterBounds.Height = FormImage.Height
    
            Dim ratio As Double = FormImage.Width / FormImage.Height
    
            If TopCenterBounds.Width > e.MarginBounds.Width Then
                TopCenterBounds.Width = e.MarginBounds.Width
                TopCenterBounds.Height = CInt(TopCenterBounds.Width / ratio)
                TopCenterBounds.X = e.MarginBounds.Left
            End If
    
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(FormImage, TopCenterBounds)
        End Sub
    End Class

    Razerz,

    I studied these codes and example codes last night 

    These are ok for receipt for because its bounded Vertically = Top and Horizontally = Center , which is needed half of A4 type paper.

    But again I got 1st form of scroll able Panel is working ok Bounded at center but printing result is not ok because paper sizes Folio / A4 are large sizes papers and contents is small, so I got this:

    Is this possible to scale up this yellow colored panel to this black border without resizing it?





    • Edited by DearBaba Sunday, January 21, 2018 11:08 AM
    Sunday, January 21, 2018 10:51 AM
  • Tommy,

    Yes got it, this is an other very simple codes to bound, thx for this.

    Now I got my contents are small and area of paper is large size so I want to scale them up as in picture, Do you have any idea how I can scale them up?


    Sunday, January 21, 2018 11:14 AM
  • Tommy,

    Yes got it, this is an other very simple codes to bound, thx for this.

    Now I got my contents are small and area of paper is large size so I want to scale them up as in picture, Do you have any idea how I can scale them up?


    Yes. If you really want to do that. If you scale up you will lose resolution and your graphic image will lose quality and get blocky. You should make the original panel size the size ratio you want the image to be when printed as Razerz did. What you show in your image of what you want really seems to be changing the margins. You need to do that for the original panel image and then the printer settings I believe but I don't have the code you are using. I am sure Razerz can show it if that is what you mean as it is not clear to me exactly what you want from Razerz example.

    Meanwhile, here is an example of how to set the margins to new values. This shows how to center a half size a4 image (with 1 inch top and bottom margin) on a full size a4 printout using only the margins. If you want additional margins on the left and right you have to add that. This will print to the edge of the half size a4 paper on the left and right side, and print a 1 inch margin top and bottom (assuming you add the code to print the image you capture from the panel).

    So, this is just an example of how to set the margins to new values. Not how to print the image.

    PS I say a4 but this works for whatever paper size is set for pd.

    Public Class Form6
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Print Margins Example 2"
    
            'add the print document and print preview controls to the form
            'you can also do this by dragging an dropping the controls from the toolbox
            'but this is just as easy and then others dont have to do it to run the example
            Dim pd As New System.Drawing.Printing.PrintDocument
    
            'add the sub routine to call when ready to print
            AddHandler pd.PrintPage, AddressOf OnPrintPage
    
            'calc margin width to center half size a4 on normal a4
            Dim w As Integer = pd.DefaultPageSettings.PaperSize.Width
            Dim w2 As Integer = CInt(w / 2)
            Dim mrg As Integer = CInt((w - w2) / 2) 'hundredths of inch
            'set the new margin for left and right
            pd.DefaultPageSettings.Margins = New Printing.Margins(mrg, mrg, 100, 100)
    
            'add and print preview 
            Dim preview As New PrintPreviewDialog
            preview.Document = pd    'tell the preview what is being printed
    
            'showing the priview dialog will fire the printPage event
            'and call the print event
            'to print on paper (or selected printer) click the print button
            'on the print dialog
            preview.ShowDialog()
    
        End Sub
    
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    
            Using p As New Pen(Color.Black, 1)
                p.DashStyle = Drawing2D.DashStyle.Dash
    
                'draw the margins
                e.Graphics.DrawRectangle(p, e.MarginBounds)
    
            End Using
        End Sub
    End Class



    Sunday, January 21, 2018 12:43 PM
  • Tommy,

    Yes got it, this is an other very simple codes to bound, thx for this.

    Now I got my contents are small and area of paper is large size so I want to scale them up as in picture, Do you have any idea how I can scale them up?




    Baba,

    Why don't you show us two images of the two final printouts you want to achieve? One full size, one half size? Show what is on each image ie text here, picture there, and how the two relate if they do. How the images fit on the paper size full and half size with margins etc. Similar to the last image you posted for full size you want to scale. However add the details that makeup the panel image.

    If you know exactly how big the images will be you know you can move a half size image to the left margin instead of centering it between the left and right margins. Comprende? (that is why an image example helps). I am just guessing at what you want. You should show us exactly what you want now that you have a better understanding of how to do it.

    PS I know that it is a lot of work to describe to us what you want, exactly. In fact many times when I do it for a question I am asking, I figure out the whole thing and I don't need to ask the question. It is the logical thinking and organizing that helps solve the problem. Once you can explain it exactly you almost have it solved.

    Sunday, January 21, 2018 6:41 PM
  • Now I got my contents are small and area of paper is large size so I want to scale them up as in picture, Do you have any idea how I can scale them up?

     As Tom already said,  scaling an image up will cause it to become blurry looking.  However,  you can try it with the example below if you want.  It will scale the image down if it is larger than the margin bounds of the page,  or scale it up if the image is smaller than the margin bounds of the page.

     Also,  take note that i have changed the code that sets the Paper size and other properties of the PrintDocument.  I did not notice the other day that my code was not actually changing the Paper size,  so that is fixed in this example.

    Imports System.Drawing.Printing
    
    Public Class Form1
        Private WithEvents PrintDoc As New PrintDocument
        Private FormImage As Bitmap = Nothing
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'draw the whole panel
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(FormImage, New Rectangle(0, 0, FormImage.Width, FormImage.Height))
            Try
                With PrintDoc
                    .PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer if you do not want the system's default printer used
                    .PrinterSettings.DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDoc.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.Folio).FirstOrDefault 'sets the paper size to A4 or Folio
                    .DefaultPageSettings.Margins = New Margins(0, 0, 0, 0) 'set the margins of the page to all 0 if you want no margins
                    .Print()
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'only draw the controls
            If FormImage IsNot Nothing Then FormImage.Dispose()
            FormImage = New Bitmap(Panel1.Width, Panel1.Height)
            For Each c As Control In Panel1.Controls
                c.DrawToBitmap(FormImage, c.Bounds)
            Next
            Try
                With PrintDoc
                    .PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer if you do not want the system's default printer used
                    .PrinterSettings.DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDoc.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.Folio).FirstOrDefault 'sets the paper size to A4 or Folio
                    .DefaultPageSettings.Margins = New Margins(0, 0, 0, 0) 'set the margins of the page to all 0 if you want no margins
                    .Print()
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage
            Dim ImgBounds As Rectangle = New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, FormImage.Width, FormImage.Height)
            Dim ratio As Double = ImgBounds.Width / ImgBounds.Height
    
            While ImgBounds.Width > e.MarginBounds.Width OrElse ImgBounds.Height > e.MarginBounds.Height 'scale the image down if bigger than margin bounds
                ImgBounds.Height -= 1
                ImgBounds.Width = CInt(ImgBounds.Height * ratio)
                ImgBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (ImgBounds.Width \ 2))
                ImgBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (ImgBounds.Height \ 2))
            End While
    
            While ImgBounds.Width < e.MarginBounds.Width AndAlso ImgBounds.Height < e.MarginBounds.Height 'scale the image up if it is smaller than margin bounds
                ImgBounds.Height += 1
                ImgBounds.Width = CInt(ImgBounds.Height * ratio)
                ImgBounds.X = e.MarginBounds.Left + ((e.MarginBounds.Width \ 2) - (ImgBounds.Width \ 2))
                ImgBounds.Y = e.MarginBounds.Top + ((e.MarginBounds.Height \ 2) - (ImgBounds.Height \ 2))
            End While
    
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(FormImage, ImgBounds)
        End Sub
    End Class
    
     

     I think it would be better to do one of the following... 

     1.  Resize your panels and controls so that they are as close as possible to the page size you are printing on.

     2. Pick the page size you are going to always use,  and just draw the information on the page as Text instead of drawing an image of the form.  You can even draw shapes too if needed.  You can make the text and shapes look much clearer this way.  It might take a little more work but,  it would always look cleaner,  use less printer ink,  and maybe look more professional.


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

    • Marked as answer by DearBaba Wednesday, January 24, 2018 3:46 PM
    Sunday, January 21, 2018 6:51 PM
  • Razer,

    Thx again for helping me

    Thx to tommy again

    Wednesday, January 24, 2018 3:46 PM