C#链接打印机(多个打印机选择、图片加载)

后台代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Net;
using System.IO;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {

      PrintDocument pd = new PrintDocument();
      pd.PrinterSettings.PrinterName = "XP-58II";//指定打印机(如果只有一台可不写 如果你的电脑连有多个打印机则需要指定你希望输出的打印机名称 否则按默认打印机)
        PaperSize p=null;
        foreach (PaperSize ps in pd.PrinterSettings.PaperSizes)
        {
            if (ps.PaperName.Equals("Printer 58(48) x 210 mm"))
            {
                p = ps;
                 break;
           }

        }
        pd.DefaultPageSettings.Landscape = true; //   横向  
        pd.DefaultPageSettings.Landscape = false; //  综向
        pd.DefaultPageSettings.PaperSize = p;
        pd.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
        pd.Print();
    }

    public void pdoc_PrintPage( object sender,System.Drawing.Printing.PrintPageEventArgs e)
    {
            //读取图片模板
            string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + @"\resources\proof_ic.png");
            System.Drawing.Image temp = System.Drawing.Image.FromFile(path);
            Brush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(brush);
            Font f = new Font("宋体", 9);
            GetResultIntoImage(ref temp);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = temp.Width;
            int height = temp.Height;
            Rectangle destRect = new Rectangle(0, 0, width, height);
            e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
            e.Graphics.DrawString("客服电话 96718" , new Font("宋体", 9, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(88, 24));
            e.Graphics.DrawString("签购单", new Font("宋体", 13, FontStyle.Bold), new SolidBrush(Color.Black), new PointF(67, 60));
            e.Graphics.DrawLine(pen, 5, 90, 200, 90);
            e.Graphics.DrawLine(pen, 5, 93, 200, 93);
            e.Graphics.DrawString("i超市名称: ", f, new SolidBrush(Color.Black), new PointF(10, 110));
            e.Graphics.DrawString("账户:     ", f, new SolidBrush(Color.Black), new PointF(10, 135));
            e.Graphics.DrawString("i超市编号: "", f, new SolidBrush(Color.Black), new PointF(10, 160));
            e.Graphics.DrawString("交易单号: ", f, new SolidBrush(Color.Black), new PointF(10, 185));
            e.Graphics.DrawString("交易类型: ", f, new SolidBrush(Color.Black), new PointF(10, 210));
            e.Graphics.DrawString("商品内容: ", f, new SolidBrush(Color.Black), new PointF(10, 235));

            e.Graphics.DrawString("交易金额:" + "30.00元", f, new SolidBrush(Color.Black), new PointF(10, 270));
            e.Graphics.DrawString("交易时间:" +"", new Font("宋体", 8), new SolidBrush(Color.Black), new PointF(10, 295));
            e.Graphics.DrawLine(pen, 5, 320, 200, 320);
            e.Graphics.DrawLine(pen, 5, 323, 200, 323);
            e.Graphics.DrawString("本单据为本次交易的有", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(15, 330));
            e.Graphics.DrawString("效凭证,请妥善保存", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(20, 360));
            
         
    }

    private void GetResultIntoImage(ref System.Drawing.Image temp )
        {
            //读取图片模板
            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.Dispose();
        }


}

转载于:https://www.cnblogs.com/dannyqiu/articles/2306213.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值