在 .NET 中打印 - 从毫米到像素的转换 [英] Print in .NET - Conversion from millimeter to pixel

查看:68
本文介绍了在 .NET 中打印 - 从毫米到像素的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将用户输入从毫米转换为像素,以便将其打印在页面的正确位置?

How can I convert user input from millimeter to pixels so that it's printed on the right position of the page?

我使用以下代码:

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 的值为9.5",edtBorderTop.Text 的值为21,5".这些值为毫米.如果我使用此代码检查输出:

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:

    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();
        }            
    }

它几乎在页面中央显示图像.一个计算示例:

It displays the image nearly in the center of the page. A calculation example:

mmToPixel(float.Parse(edtBorderLeft.Text), dpiX)edtBorderLeft.Text = "9.5"dpiX = 600;返回:224

mmToPixel(float.Parse(edtBorderLeft.Text), dpiX) edtBorderLeft.Text = "9.5" dpiX = 600; returns: 224

如何计算打印图像的正确点?

How can I calculate the right point for the printed image?

推荐答案

我找到了解决方案.您可以使用以下代码更改页面单位.所以我不需要转换:

I found a solution. You can change the page unit with the following code. So I don't need a conversion:

 e.Graphics.PageUnit = GraphicsUnit.Millimeter;

e.Graphics.PageUnit = GraphicsUnit.Pixel;

我可以使用上面的代码.

and I can use the code above.

这篇关于在 .NET 中打印 - 从毫米到像素的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆