title: Print in .NET - Conversion from millimeter to pixel
body: <p>How can I convert user input from millimeter to pixels so that it's printed on the right position of the page?</p>
  
  <p>I use the following code:</p>
  
  <pre><code>private void document_PrintPage(object sender, PrintPageEventArgs e)
  {
      float dpiX = e.Graphics.DpiX;
      float dpiY = e.Graphics.DpiY;
      Point p = new Point(mmToPixel(float.Parse(edtBorderLeft.Text), dpiX), 
              mmToPixel(float.Parse(edtBorderTop.Text), dpiY));
      e.Graphics.DrawImage(testImage, p);
  
  }
  
  private int mmToPixel(float mm, float dpi)
  {
      return (int)Math.Round((mm / 25.4)  * dpi);
  }
  </code></pre>
  
  <p>edtBorderLeft.Text got the value of "9.5" and edtBorderTop.Text the value of "21,5". These values are millimeters. If I check the output with this code:</p>
  
  <pre><code>    private void printPage()
      {
          PrintDialog dialog = new PrintDialog();
          dialog.Document = document;
          if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
              PrintPreviewDialog preview = new PrintPreviewDialog();
              preview.Document = document;
              preview.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
              preview.Show();
              //document.Print();
          }            
      }
  </code></pre>
  
  <p>It displays the image nearly in the center of the page. A calculation example:</p>
  
  <p>mmToPixel(float.Parse(edtBorderLeft.Text), dpiX)
  edtBorderLeft.Text = "9.5"
  dpiX = 600;
  returns: 224</p>
  
  <p>How can I calculate the right point for the printed image?</p>