winfrom打印图片文件

本文介绍了如何在Windows Forms应用程序中实现图片的打印功能,详细记录了相关的代码实现过程,旨在解决工作中遇到的图片打印需求。

工作中存在需要把图片打印出来,现在记录代码实现方式。

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|否

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值