工作中存在需要把图片打印出来,现在记录代码实现方式。
public static void PdfPrint()
{
string msg = string.Empty;
try
{
#region 打印图片文件
imageIndex = 0;
//打印字符串用
SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
System.Drawing.Font DrawFont = new System.Drawing.Font("Arial", 30);
PrintDocument pda = new PrintDocument();
pda.PrintPage += PicturePrintDocument_PrintPage; //注册打印事件
pda.PrinterSettings.PrinterName = "Zan 黑白图像打印机"; //打印机选择,使用默认打印机不需要设置
pda.Print();
#endregion
}
catch (Exception ex)
{
msg = "ReportPrint:" + ex.Message;
}
}
private static void PicturePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打开文件
string path = (++imageIndex).ToString() + ".png";
//FileStream fs = File.OpenRead(path); //OpenRead
// 获取打印页面的宽度和高度
float pageWidth = e.PageBounds.Width;
float pageHeight = e.PageBounds.Height;
System.Drawing.Image image = System.Drawing.Image.FromFile(path);//读取图片进内存Image
// 计算缩放比例
float scaleX = pageWidth / image.Width;
float scaleY = pageHeight / image.Height;
float scale = Math.Min(scaleX, scaleY);
//转换为 IMAGE
//image = new Bitmap(image, (int)(image.Width * scale), (int)(image.Height * scale)); // 创建缩放后的图片
e.Graphics.DrawImage(image, 0, 0); //img大小
//e.Graphics.DrawString(TicCode, DrawFont, brush, 600, 600); //绘制字符串
if (imageIndex >= 10)
e.HasMorePages = false;
else
e.HasMorePages = true;//是否需要循环打印:true|是;false|否
}
本文介绍了如何在Windows Forms应用程序中实现图片的打印功能,详细记录了相关的代码实现过程,旨在解决工作中遇到的图片打印需求。

2237

被折叠的 条评论
为什么被折叠?



