[英]C# Graphics.DrawImage truncated/cropped image when printing
我正在尝试使用 C# 打印图像。问题是图像在打印时被裁剪/截断。 分辨率和宽度/高度是正确的,因为 2 个边框与打印页面匹配,但图像只打印了一半。
图像是 6''x5'' (152.4mm x 127mm) 的.png,但我可以将图像转换为.tiff(同一期)。 打印机选择了正确的纸张。 如果我将 png 转换为 pdf 并使用 Adobe Reader 打印它我没有问题(没有 C#)
这是部分代码(实现是一个Class但只是变量初始化)
private void PD_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
if (e != null)
{
PD_PrintPage_DrawImage(e);
PD_PrintPage_Close(e);
}
}
catch (Exception)
{
// Error handling.
e.HasMorePages = false;
e.Cancel = true;
_exceptionFlag = true;
}
}
public Print(String devName, String portName, int totalPrintPage)
{
try
{
// Create the PrintDocumentObject.
_pdPrint = new PrintDocument();
// Add the PrintPageEventHandler.
_pdPrint.PrintPage += new PrintPageEventHandler(PD_PrintPage);
// Set the using Printer.
_pdPrint.PrinterSettings.PrinterName = devName;
_pdPrint.DocumentName = "Label Print";
_totalPrintPage = 1;
return;
}
catch
{
throw;
}
}
public void DoPrinting()
{
try
{
_pdPrint.Print();
if(_exceptionFlag == true)
{
throw new Exception();
}
}
catch (Exception)
{
throw;
}
}
private void PD_PrintPage_DrawImage(PrintPageEventArgs e)
{
try
{
float width = 152.4F;
float height = 127.0F;
// Set page unit.
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
// String currentDirectryPath = Directory.GetCurrentDirectory();
String imageFilePath = "C:\\Users\\uservm\\Documents\\interfaces\\label6x5.png";
//String imageFilePath = Path.Combine(currentDirectryPath, imageFileName);
using (FileStream fs = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
using (Image img = Image.FromStream(fs))
{
// Draw image.
e.Graphics.DrawImage(img, 0, 0, width, height);
}
}
catch (Exception)
{
// Error handling.
MessageString.GetSystemError((int)MessageString.STATE_DRAWIMAGE_ERROR);
}
}
new Rectangel()
方法只接受int
作为变量,但我的页面大小是float
。 尽管如此,我如何在我的功能上实现它? 我认为问题在于绘图区域未满。
有任何想法吗?
这种类型的结果或其他格式的更糟通常是表单定义问题的结果,在 windows 默认纵向和尺寸过小的页面上打印横向屏幕时常见。 这是完全相同的测试打印输入,但有两种不同的纸张 output 类型。
Acrobat 是由印刷厂专家(与 Microsoft 不同)编写的,因此他们的论文 output 通常更符合预期。 分辨率的关键是确保所有单位都是已知的,并且您发布的图像是无单位的,因此不是 6 x 5,如果设置为 100 dpi,它几乎是(x 和 y 中的一个像素)所以如果设置为 101 可能会有所帮助dpi。 或者更好的是使用 576 宽和 480 高的比例,在许多应用程序中默认为 6X5。 还要仔细检查 windows 打印管理认为页面布局应该是什么,例如确保它被设置为无边界和打印机驱动程序的正确方向,因为有些将使用与正常 90 度的卷筒进纸。
如果您的 FORMS 设置正确,那么在任何方向打印完整介质应该没有问题,所以我在这里定义了 6x5 和 5x6,这是测量的 output
尝试不同的小格式专业驱动程序可能会有所帮助,因此对于标签,通常建议使用“Bartender Seagull”驱动程序(如果适用于该打印机型号)。
免责声明我支持 SumatraPDF 并且可以对其进行修改以适合您的打印,但是对于小格式来说这不是一项简单的任务并且通常需要大量覆盖 windows 默认设置。 最重要的是不要使用 -silent 开关,尤其是在打印服务时。
请注意,我必须在 Windows 打印管理中添加一个 6x5 表格,虽然它说的是 6.00x5.00,但它实际上定义为 5 英寸宽 x 6 英寸高。 由于您希望将其应用为横向旋转,因此我必须确保驱动程序默认设置为横向。 如果需要无头旋转,它很可能会失败。 所以现在我的媒体结构为 paperkind=138(每台机器上的数字都不同,我可以使用设置paper=6x5
,它也有效)编号对于某些驱动程序媒体列表更可靠。 此外,由于我们知道您的图像尺寸过大,因此我需要添加收缩。 到目前为止,使用 Acrobats 默认值比使用 Microsoft 更容易,而且 Acrobat 可以在待机状态下运行 (/T),因此它不需要为每次打印缓慢地重新加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.