c#中打印预览图像

本文关键字:图像 打印 | 更新日期: 2023-09-27 18:07:40

我尝试自动填充pdf表单,但它不允许我在pdf上绘制。所以我创建了一个表单的图像,并使用drawstring方法填充它。问题是,即使打印评论正确地显示了文档,当我试图打印它时,我得到的是一个放大的文档,比纸张大小还大。我不知道为什么。下面是我的代码:

private void buttonPrint_Click(object sender, EventArgs e)
    {
        Image img = panelForm.BackgroundImage;
        printDocumentForm.DefaultPageSettings.PaperSize = new PaperSize("A4", img.Width, img.Height);
        printPreviewForm.Document = printDocumentForm;
        printPreviewForm.ShowDialog();
    }
    private void printDocumentForm_PrintPage(object sender, PrintPageEventArgs e)
    {
        Image img = panelForm.BackgroundImage;
        Image copy = (Image) img.Clone();
        // I've added some drawstring methods here to fill the blanks
        // but I removed them in this example to save some space
        e.Graphics.DrawImage(copy, new Point(0, 0));
    }

预览窗口显示填满空白的正确文档。但是当我按下预览窗口上的打印按钮时,我得到了放大的文档……编辑:我应该补充一下,当我用Paint打开bmp文件时,我可以正确打印图像。同样的图像在我的项目中没有任何明显的原因就显得更大和模糊。为什么打印机把我的图像拉长了?为什么预览窗口会显示正确的图像大小?

c#中打印预览图像

我是这么想的:

        printDoc.DefaultPageSettings.PaperSize=printDoc.PrinterSettings.PaperSizes[4];
        // this sets the printPreview document size to A4. The previously presented code doesn't
        Image img = panelDM.BackgroundImage; // this is what I want to print
        Rectangle bounds = e.PageBounds;            
        int mx = 30;
        int my = 30; // add borders
        int x = img.Width;
        int y = img.Height;
        int xp = bounds.Width*9/10; // for some reason (I can't explain why)
        int yp = bounds.Height*9/10; // 100% appears larger than the page...
        int xr, yr;
        if (x > y)
        {
            xr = xp-mx;
            yr = xr*y/x;
        }
        else
        {
            yr = yp-my;
            xr = yr*x/y;
        }
        Size size = new Size(xr,yr);
        Image copy = (Image)img.Clone();            
        e.Graphics.DrawImage(copy, new Rectangle(new Point (mx,my), size));

所以我认为您需要通过更改默认页面设置来设置printpreview页面大小。pagesize属性。我不知道为什么自定义的纸张尺寸不起作用。然后你需要在DrawImage方法中设置图像大小。最后一件事:图像不像原点那样清晰。它是可用的,但模糊。