How can I convert user input from millimeter to pixels so that it prints in the correct position on the page?
I am using the following 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);
}
edtBorderLeft.Text got the value "9.5" and edtBorderTop.Text got the value "21.5". These values are millimeters. If I check the output with this 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();
}
}
It displays an image almost in the center of the page. Calculation example:
mmToPixel (float.Parse (edtBorderLeft.Text), dpiX) edtBorderLeft.Text = "9.5" dpiX = 600; returns: 224
How to calculate the correct point for a printed image?
source
share