C#打印图片

      private void btnPrint_Click(object sender, EventArgs e)
        {
            #region 打印
        
            //打印预览
            PrintPreviewDialog ppd = new PrintPreviewDialog();

            if (picBox.Image == null)
            {
                Auda.Form.Class.MessageBoxA.MsgBoxWarning(null, "请先选择你要打印的附件!");
                return;
            }


            PrintDocument pd = new PrintDocument();
            //设置边距
            Margins margin = new Margins(20, 20, 20, 20);
            pd.DefaultPageSettings.Margins = margin;
            纸张设置默认
            //PaperSize pageSize = new PaperSize("First custom size", 800, 600);
            //pd.DefaultPageSettings.PaperSize = pageSize;
            //打印事件设置
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            ppd.Document = pd;
            ppd.ShowDialog();
            try
            {
           
              // pd.Print();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
            }
        }
        //打印事件处理
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            string date = DateTime.Now.ToString(); //当前日期
            string name = 公共类.PublicVariable.UserInfo.CurrentUser.UName;
            //读取图片

            Image temp = picBox.Image;
            GetResultIntoImage(ref temp,name,date);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = temp.Width;
            int height = temp.Height;
            Rectangle destRect = new Rectangle(x, y, width, height);
            e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
        }

        /// <summary>
        /// 将结果填充到图片
        /// </summary>
        private void GetResultIntoImage(ref Image temp, string name, string date)
        {
            //读取图片

            Graphics g = Graphics.FromImage(temp);
            Font f = new Font("宋体", 12);
            Brush b = new SolidBrush(Color.Black);
           
            //填充数据到图片

            g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
            g.DrawString(name, f, b, 168, 105);
            g.DrawString(date, f, b, 176, 269);
            g.Dispose();
        }
        #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值