c# - 是否可以在 C# 中双面打印,例如打印身份证或驾驶执照?
问题描述
我有两张图片 front.jpg 和 back.jpg。我想将它们正面和背面打印在同一页上以制作一张“卡片”。我遇到了“双工”属性,但不确定它在这方面如何帮助我。
我尝试使用双工。我正在尝试在 printDocument1_PrintPage 事件中为正面和背面打印两个图像
private void button1_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += printDocument1_PrintPage;
//here to select the printer attached to user PC
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = pd;
DialogResult result = printDialog1.ShowDialog();
pd.PrinterSettings.Duplex = Duplex.Horizontal;
if (result == DialogResult.OK)
{
pd.Print();//this will trigger the Print Event handeler PrintPage
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
#region Front
//Load the image from the file
System.Drawing.Image img = System.Drawing.Image.FromFile(@"E:\front.jpg");
//Adjust the size of the image to the page to print the full image without loosing any part of it
Rectangle m = e.MarginBounds;
if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
}
e.Graphics.DrawImage(img, m);
#endregion
//Load the image from the file
System.Drawing.Image img1 = System.Drawing.Image.FromFile(@"E:\back.jpg");
//Adjust the size of the image to the page to print the full image without loosing any part of it
Rectangle m1 = e.MarginBounds;
if ((double)img1.Width / (double)img1.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img1.Height / (double)img1.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img1.Width / (double)img1.Height * (double)m.Height);
}
e.Graphics.DrawImage(img1, m1);
}
catch (Exception)
{
}
}
解决方案
我相信您的问题是您不是在打印一个双面页面,而是在打印两页,一张纸的每一面。双工魔法发生在打印机/驱动程序中,而不是 GDI。
我只看到这里打印了一页。不要尝试一次打印两个图像。
- 完成打印第一张图像的工作。
- 设置 (PrintPageEventArgs) e.HasMorePages = true;
- 从此函数返回,您将再次被调用到第二页。
- 现在,打印第二张图片并设置 e.HasMorePages = false;
那应该行得通。祝你好运!
推荐阅读
- visual-studio - .Net 5 RC 1.20451 Blazor 应用程序无法启动
- java - Jsoup connect(url) 获取编码
- ios - Xcode 12 中的“无法在此文件中预览 - 连接中断:向代理发送 previewInstances 消息”错误
- c# - 在 C# 中的 CultureInfo 之间切换
- python - 何时使用字典 | (合并) vs |= (更新) 运算符
- python - 从 Python 调用具有默认参数的 C++ 函数
- internet-explorer-11 - IE 11 不接受 SameSite cookie
- ios - SWIFT:从定义 tableView 到 tableViewCell 的 ViewController 通信数据
- docker - Overlayfs 卷到 docker 容器
- authentication - cakephp 3.8.13 添加 admad/cakephp-jwt-auth