适应纸张大小打印 C#

3

关于

我正在使用WinForms。在我的窗体中,有一个“打开”按钮和一个“打印”按钮。点击“打开”按钮会将tif图像打开到一个picturebox中。点击“打印”按钮会从picturebox中打印这些图片。我处理大型图像文档,例如宽度和长度:(3000,3600)。因此,我将这些tif图像文件缩放以适合普通打印纸张大小(8.5 x 11)。我之所以这样做是为了使tif图像上的字母不模糊,使用如下方法:

问题

好消息是它缩放得很好,这意味着它不会模糊。坏消息是它缩小太多了。见图A.2。

测试

测试我增加和减少了* 100,奇怪的是它并没有增加大小,而是减小了大小。

float newWidth = i.Width * 100 / i.HorizontalResolution;
float newHeight = i.Height * 100 / i.VerticalResolution;


代码

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        { //pageViewer = picturebox
            Image i = pageViewer.Image;

            float newWidth = i.Width * 100 / i.HorizontalResolution;
            float newHeight = i.Height * 100 / i.VerticalResolution;

            float widthFactor = newWidth / e.MarginBounds.Width;
            float heightFactor = newHeight / e.MarginBounds.Height;

            if (widthFactor > 1 | heightFactor > 1)
            {
                if (widthFactor > heightFactor)
                {
                    newWidth = newWidth / widthFactor;
                    newHeight = newHeight / widthFactor;
                }
                else
                {
                    newWidth = newWidth / heightFactor;
                    newHeight = newHeight / heightFactor;
                }
            }
            e.Graphics.DrawImage(i, 0, 0, (int)newWidth, (int)newHeight);
        }


应该如何打印

输入图像描述

当前的打印效果 图 A.2 输入图像描述


1
代码似乎运行正常,除了在页边打印。尝试使用 e.Graphics.DrawImage(i, e.MarginBounds.Left, e.MarginBounds.Top, (int)newWidth, (int)newHeight); - LarsTech
我现在测试了一下。它所做的是将图像居中,但图像仍然被缩小到很小的尺寸。@LarsTech - taji01
我可以提供一个链接,让我创建它。 - taji01
1
该图像周围的文本有一个粗白色边框。请使用e.PageBounds而不是e.MarginBounds,并再次在0,0位置打印。 - LarsTech
非常感谢,它起作用了!我不知道为什么被投票否决了:( - taji01
显示剩余2条评论
1个回答

4

您的图像已经包含了边距,因此当您使用e.MarginBounds属性时,实际上会使您的边距加倍。要修复此问题,请改用PageBounds属性。

float widthFactor = newWidth / e.PageBounds.Width;
float heightFactor = newHeight / e.PageBounds.Height;

再次感谢LarsTech,你真的很有帮助 :) - taji01

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