使用 PrintDocument 打印图像.如何调整图像以适合纸张大小 [英] Printing image with PrintDocument. how to adjust the image to fit paper size

查看:56
本文介绍了使用 PrintDocument 打印图像.如何调整图像以适合纸张大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我尝试使用 PrintDocument 类和以下代码打印图像.图像大小为 1200 px 宽和 1800 px 高.我正在尝试使用小型 zeebra 打印机在 4*6 的纸上打印此图像.但该程序仅打印 4*6 的大图像.这意味着它没有将图像调整为纸张尺寸!

In C#, I am trying to print an image using PrintDocument class with the below code. The image is of size 1200 px width and 1800 px height. I am trying to print this image in a 4*6 paper using a small zeebra printer. But the program is printing only 4*6 are of the big image. that means it is not adjusting the image to the paper size !

     PrintDocument pd = new PrintDocument();
     pd.PrintPage += (sender, args) =>
     {
           Image i = Image.FromFile("C://tesimage.PNG");
           Point p = new Point(100, 100);
           args.Graphics.DrawImage(i, 10, 10, i.Width, i.Height);
     };
     pd.Print();

当我使用 Window Print 打印相同的图像时(右键单击并选择打印,它会自动缩放到纸张大小并正确打印.这意味着所有内容都是 4*6 纸张.)我如何在我的 C# 中做同样的事情程序?

When i print the same image using Window Print (right click and select print, it is scaling automatically to paper size and printing correctly. that means everything came in 4*6 paper.) How do i do the same in my C# program ?

推荐答案

你传递给 DrawImage 方法的参数应该是你想要在纸上的图像的大小而不是图像本身的大小,DrawImage 命令然后将为您处理缩放.可能最简单的方法是使用以下 DrawImage 命令的覆盖.

The parameters that you are passing into the DrawImage method should be the size you want the image on the paper rather than the size of the image itself, the DrawImage command will then take care of the scaling for you. Probably the easiest way is to use the following override of the DrawImage command.

args.Graphics.DrawImage(i, args.MarginBounds);

注意:如果图像的比例与矩形不同,这将使图像倾斜.对图像大小和纸张大小进行一些简单的数学计算,您就可以创建一个适合纸张边界的新矩形,而不会使图像倾斜.

Note: This will skew the image if the proportions of the image are not the same as the rectangle. Some simple math on the size of the image and paper size will allow you to create a new rectangle that fits in the bounds of the paper without skewing the image.

这篇关于使用 PrintDocument 打印图像.如何调整图像以适合纸张大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆