c# - 打印适合纸张 C#

标签 c# .net image winforms printing

关于

我正在使用 WinForms。在我的表单中,我有一个打开和打印按钮。打开按钮将 tif 图像打开到图片框中。打印按钮从图片框中打印这些图片。我使用大图像文档,例如宽度和长度:(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);
        }


它应该如何打印

enter image description here

当前打印情况 图 A.2 enter image description here

最佳答案

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

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

关于c# - 打印适合纸张 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35804663/

相关文章:

c# - 保存到数据库时验证数据字段是否太大?

c# - 使用 ConfigurationManager 加载 System.ServiceModel 配置部分

c# - 简单的 HTTPS 到 PHP 脚本

image - 如何在javafx imageView中获取显示图像的宽度/高度?

javascript - 在图像上使用 max-width = 100% 和 max-height = 100%,计算显示宽度/高度

ios - 在 Twitter 上分享时 GIF 没有动画

c# - 在c#中将消息从子任务传递到父任务

c# - IEnumerable<T> 作为 WCF 方法的返回类型

c# - 检索 blob 文件名

.net - 将 MS sql server (aspnetdb) 表、存储过程等与我自己的数据库合并?