在C#中的打印预览中居中图片

4
我正在使用WinForms。在我的表单中,我有一个picturebox和一个打印按钮。是否有一种方法可以使我加载到picturebox中的图像始终位于打印预览窗口的中心?下面的图像显示了我的表单和打印预览屏幕中未居中的图像。

       enter image description here

        enter image description here

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.Image = new Bitmap(@"C:\Users\Nav\Pictures\Test_Image.png");
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(pictureBox1.Image,50,50);
    }

    private void Btn_Print_Click(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }

1
不要使用绝对/固定坐标(“50, 50”),计算中心。 - Ňɏssa Pøngjǣrdenlarp
1
你没有在文档中心绘制图像。你在(50,50)处绘制了它。 - Reza Aghaei
我明白了,所以我猜这并没有像 e.image.center 这样的方法来实现。 - taji01
如果我使用 e.Graphics.DrawImage(pictureBox1.Image, e.MarginBounds);,它会给它一个边距,但同时也会拉伸图像。 - taji01
1个回答

13

你没有在文档的中心绘制图像。你在 (50,50) 处绘制了它。相反,你可以使用DrawImage将其绘制在文档中心:

e.Graphics.DrawImage(img,
                     (e.PageBounds.Width - img.Width) / 2,
                     (e.PageBounds.Height - img.Height) / 2,
                     img.Width,
                     img.Height);

感谢您的又一次出色回答! :) - taji01

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接