-
Mar 2nd, 2021, 04:41 PM
#1
Thread Starter
Member
Printing form on 8 1/2 x 11 paper
I found this code somewhere in my google search and it works very nicely.
Code:
Private Sub btnPrintForm_Click(sender As Object, e As EventArgs) Handles btnPrintForm.Click
'Print Form Fit to Page 8 1/2 X 11
Dim preview As New PrintPreviewDialog
Dim pd As New System.Drawing.Printing.PrintDocument
pd.DefaultPageSettings.Landscape = True
pd.DefaultPageSettings.PaperSize = New PaperSize("A", 850, 1100) '8 1/2 X 11 100's of an inch
AddHandler pd.PrintPage, AddressOf OnPrintPage
preview.Document = pd
preview.ShowDialog()
End Sub
Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'create a memory bitmap and size to the form
Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
'draw the form on the memory bitmap
Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height))
'draw the form image on the printer graphics sized and centered to margins
Dim ratio As Single = CSng(bmp.Width / bmp.Height)
If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
e.Graphics.DrawImage(bmp,
e.MarginBounds.Left,
CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - ((e.MarginBounds.Width / ratio) / 2)),
e.MarginBounds.Width,
CInt(e.MarginBounds.Width / ratio))
Else
e.Graphics.DrawImage(bmp,
CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (e.MarginBounds.Height * ratio / 2)),
e.MarginBounds.Top,
CInt(e.MarginBounds.Height * ratio),
e.MarginBounds.Height)
End If
End Sub
I think I understand it until I get to this code:
Code:
If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
e.Graphics.DrawImage(bmp,
e.MarginBounds.Left,
CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - ((e.MarginBounds.Width / ratio) / 2)),
e.MarginBounds.Width,
CInt(e.MarginBounds.Width / ratio))
Else
e.Graphics.DrawImage(bmp,
CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (e.MarginBounds.Height * ratio / 2)),
e.MarginBounds.Top,
CInt(e.MarginBounds.Height * ratio),
e.MarginBounds.Height)
End If
What I don't understand is this "e.MarginBounds". There is Width, Height, Top...of what?
Can someone tell me what "e.MarginBounds" is? Is it the paper size?
-
Mar 2nd, 2021, 04:48 PM
#2
Re: Printing form on 8 1/2 x 11 paper
-
Mar 2nd, 2021, 05:06 PM
#3
Re: Printing form on 8 1/2 x 11 paper
e.MarginBounds is a rectangle within the page bounds, located and sized as specified by the margins properties
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2021, 05:17 PM
#4
Thread Starter
Member
Re: Printing form on 8 1/2 x 11 paper
paul, what you are saying is e.MarginsBounds.Width and e.MarginsBounds.Height is the size of the paper I am printing on.
Then what is e.MarginsBounds.Top? I mean how was it calculated? There is nothing obvious to that.
-
Mar 2nd, 2021, 05:25 PM
#5
Re: Printing form on 8 1/2 x 11 paper
No assuming you have e.marginbounds.top and bottom of 100px each and left and right both 100px each, then the rectangle described by e.maginbounds is your paper size minus those borders
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2021, 05:30 PM
#6
Re: Printing form on 8 1/2 x 11 paper
Review the documentation that OptionBase1 linked to. The property value is a Rectangle. The Rectangle has two constructors, both of which specify the initial size and position.
Looking at the documentation for PrintDocument (here), there appears to be a DefaultPageSettings property (here).
The property value for DefaultPageSettings is a PageSettings object (here).
Look at that documentation, there appears to be a Margins property (here) with this comment:
A Margins that represents the margins, in hundredths of an inch, for the page. The default is 1-inch margins on all sides.
All in all it took me longer to write out this post than it did to visit the link that OptionBase1 provided and to traverse my way to the appropriate documentation.
-
Mar 2nd, 2021, 06:32 PM
#7
Re: Printing form on 8 1/2 x 11 paper
The inner dashed rectangle in this image shows e.marginbounds...
Attachment 180391
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2021, 06:36 PM
#8
Re: Printing form on 8 1/2 x 11 paper
Attachments don't always work great in this forum. Any mods who see this, can you fix my image upload?
http://www.scproject.biz/marginbounds.png
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2021, 07:09 PM
#9
Re: Printing form on 8 1/2 x 11 paper
I haven't figured out why the attachments work half the time either. I believe Steve is aware of it.
-
Mar 3rd, 2021, 01:00 PM
#10
Thread Starter
Member
Re: Printing form on 8 1/2 x 11 paper
Thanks to your help I have seen that things like like MarginBounds.Top or Left or Right etc is nothing more than a default of 100,
and that it refers to a rectangle(I had already concluded) that starts out as the paper size and is scaled to make the BMP scaled to fit the paper size.
Also I found by looking at some of the Microsoft documents that paper size is the default printer size.
Microsoft documentation for someone looking for answers the first time is very difficult, once you learn it then it is a good reference.
I wish there was a book that would take the Microsoft Documentation and explain it. The books you can buy are just too basic.
To this day I can't figure out how to find the Microsoft documents that refer to what I need. If you responders didn't show me with your post I would not have found the documents I was looking for, even then the explanations in the Documents are lacking.
Yes I can google them and maybe it would be what I was looking for.
In earlier Visual Basic program you could follow the program and know where it came from. Now it is very difficult.
Thanks OK for all your help.
-
Mar 3rd, 2021, 02:55 PM
#11
Thread Starter
Member
Re: Printing form on 8 1/2 x 11 paper
After using msgbox I find that e.MarginBounds refers to the bmp rectangle that fits on the paper size not the actual form size. I guess that's what paul was trying to tell me.
Where did that get changed from the original form size (Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height)
I found this at Microsoft:
Parameters
bitmap
Bitmap
The bitmap to be drawn to.
targetBounds
Rectangle
The bounds within which the control is rendered.
That does not tell me what it got sized too, obvious must be less than the paper.
-
Mar 3rd, 2021, 03:11 PM
#12
Re: Printing form on 8 1/2 x 11 paper
e.marginbounds is a rectangle within the physical printed page. It isn’t the paper size. The size of your form is irrelevant. If you want to print your form on 8 1/2 x 11 paper, you might have to scale your form image down, or if your form image is smaller than a printed page, you might want to scale it up or just centre the unscaled image.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 3rd, 2021, 04:47 PM
#13
Thread Starter
Member
Re: Printing form on 8 1/2 x 11 paper
Thank you paul, I am getting closer to understanding it.
Early in the code the physical printed page was set to 1100 x 850 (8 1/2 x 11 landscape) and the form was larger than that
so the If statement scaled it to fit.
I wish I had copied who I got the code from so I could thank them. It works very nicely I am just trying to understand it.
-
Mar 4th, 2021, 10:39 AM
#14
Thread Starter
Member
Re: Printing form on 8 1/2 x 11 paper
Because the Form is set in size and will always be printed on Landscape 11 x 8 1/2 and maybe someone else can use the code.
I have shortened the code to this:
Code:
Private Sub btnPrintForm_Click(sender As Object, e As EventArgs) Handles btnPrintForm.Click
Dim preview As New PrintPreviewDialog
Dim pd As New System.Drawing.Printing.PrintDocument
pd.DefaultPageSettings.Landscape = True
pd.DefaultPageSettings.PaperSize = New PaperSize("A", 850, 1100) 'MAKE SURE IT IS 8 1/2 X 11 100's of an inch
AddHandler pd.PrintPage, AddressOf OnPrintPage
preview.Document = pd
preview.ShowDialog() 'SHOWSS HOW IT LOOKS BEFORE PRINTING FROM HERE YOU CAN PRINT OR CANCEL
End Sub
Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height)) 'DRAW THE FORM ON A BMP
e.Graphics.DrawImage(bmp, 100, 100, 900, 650) 'SINCE I KNOW THE PAPER SIZE OF 1100 X 850
End Using
End Sub
Any comments would be appreciated.
-
Mar 4th, 2021, 05:23 PM
#15
Re: Printing form on 8 1/2 x 11 paper
To this day I can't figure out how to find the Microsoft documents that refer to what I need.
This might help... here's how I search for things: go to google, then use [language] [class] [method] so in this case "vb.net printdocument marginbounds" this usually brings up a pretty good set of results, and usually the documentation is close to the top if not the top result.
-tg
-
Mar 4th, 2021, 05:29 PM
#16
Re: Printing form on 8 1/2 x 11 paper
You can also set the WindowState of the PrintPreviewDialog, before showing it...
Code:
preview.WindowState = FormWindowState.Maximized
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|