Convert Inches to Pixels in PrintDocument

fredriko

Active member
Joined
Jun 3, 2004
Messages
35
Programming Experience
10+
Using a printdocument class I want to print a rectangle in the middle of an A4 page that is 3 x 5 inches.
I have used the following code in the PrintDocument_PrintPage event method but the box is massive and I can't seem to calculate how to convert inches into dots/pixels. Can somebody help me please.
VB.NET:
Private Sub m_Document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles m_Document.PrintPage
   dim iHeight as integer = 3
   dim iWidth as integer = 5
   dim Rect as new Rectangle(100, 100, iWidth * e.Graphics.DpiX, iHeight * e.Graphics.DpiY)
   e.Graphics.DrawRectangle(Pens.Black, Rect)
end sub
 
I havent tried the following code, but it should give you some direction.

VB.NET:
    e.Graphics.PageUnit = GraphicsUnit.Inch
    dim Rect as new Rectangle(2.5, 2.5, 5.0, 3.0)
    e.Graphics.DrawRectangle(Pens.Black, Rect)
 
Back
Top