【问题标题】:Center Image in Print Preview C#打印预览 C# 中的中心图像
【发布时间】:2016-05-19 20:51:37
【问题描述】:

我正在使用 WinForms。在我的表单中,我有一个图片框和一个打印按钮。有没有办法让我加载到图片框中的图像始终位于打印预览窗口的中心?下图显示了我的表单和打印预览屏幕中未居中的图像。

       

        

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

【问题讨论】:

  • 而不是绝对/固定坐标(50, 50),计算中心
  • 您没有在文档中心绘制图像。你在(50,50) 绘制它
  • 我明白了,所以我猜没有像 e.image.center 这样的方式来做到这一点。
  • 如果我使用e.Graphics.DrawImage(pictureBox1.Image, e.MarginBounds);,它会给它一个边距,但它也会拉伸图像。

标签: c# .net image winforms printing


【解决方案1】:

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

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

【讨论】:

  • 感谢另一个很好的回答! :)
猜你喜欢
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 2015-12-06
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
相关资源
最近更新 更多