直接打印到打印机是指在C# MVC(Model-View-Controller)开发框架中,通过编程方式将数据直接发送到打印机进行打印的操作。
在C# MVC中,可以使用System.Drawing.Printing命名空间中的PrintDocument类来实现直接打印到打印机的功能
1)绝对路径方式
/// <summary> /// 打印订单信息(调用方法) /// </summary> /// <param name="wapBillNo"></param> /// <param name="wapCarNo"></param> public void PrintBillInfo(string wapBillNo, string wapCarNo) { //调用浏览器的打印 // 创建一个PrintDocument对象 PrintDocument printDoc = new PrintDocument(); // 设置打印事件处理程序 printDoc.PrintPage += new PrintPageEventHandler(PrintPage); // 调用Print方法开始打印 printDoc.Print(); } private void PrintPage(object sender, PrintPageEventArgs e) { // 在打印页面事件中编写打印逻辑 Graphics graphics = e.Graphics; Font font = new Font("Arial", 12); Brush brush = Brushes.Black; float lineHeight = font.GetHeight(); // 打印文本 string text = "这是要打印的文本"; graphics.DrawString(text, font, brush, new PointF(10, 10)); // 打印图片(只支持相对路径或绝对路径,不支持网络图片地址) Image image = Image.FromFile("path/to/image.jpg"); // Image image = Image.FromFile("C://20240920114351.jpg"); graphics.DrawImage(image, new PointF(10, 30)); }
打印纸条信息如下:
2)网络图片方式(主要代码)
处理方式:先根据需要生成二维码图片Bitmap对象,再转成二维码图片并返回网络图片地址,根据网络图片地址,绘制二维码图片信息
/// <summary> /// 打印新的纸条信息 /// </summary> /// <param name="wapBillNo"></param> /// <param name="wapCarNo"></param> public void PrintBillInfo(string wapBillNo, string wapCarNo) { string qrCodeImgUrl = string.Empty; //生成二维码图片的bitmap Bitmap bitBillNo = CreateQRCode(wapBillNo,2); //2 //将bitmap图像转成图片文件 bool isSave = SaveQRCode(bitBillNo,wapBillNo,ref qrCodeImgUrl); if (isSave) { //全局变量赋值 qrCode_wapBillNo = wapBillNo; qrCode_wapCarNo = wapCarNo; qrCode_imageUrl = qrCodeImgUrl; //调用浏览器的打印 // 创建一个PrintDocument对象 PrintDocument printDoc = new PrintDocument(); // 设置打印事件处理程序 printDoc.PrintPage += new PrintPageEventHandler(PrintPage); // 调用Print方法开始打印 printDoc.Print(); //清空全局变量 qrCode_wapBillNo = ""; qrCode_wapCarNo = ""; qrCode_imageUrl = "";; } else { } } /// <summary> /// 委托打印的实现方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PrintPage(object sender, PrintPageEventArgs e) { // 在打印页面事件中编写打印逻辑 Graphics graphics = e.Graphics; Font font9 = new Font("Arial", 9); Brush brush = Brushes.Black; // float lineHeight = font.GetHeight(); // 打印文本 string text1 = "预约单号:" + qrCode_wapBillNo; graphics.DrawString(text1, font9, brush, new PointF(5, 10)); string text2 = "车牌号码:"+ qrCode_wapCarNo; graphics.DrawString(text2, font9, brush, new PointF(5, 30)); // 打印图片(只支持相对路径或绝对路径) // Image image = Image.FromFile("path/to/image.jpg"); // Image image = Image.FromFile("C://20240920114351.jpg"); //打印图片(支持网络图片路径) string imgUrl = qrCode_imageUrl; //获取网络图片的图片流对象 Image image = GetImageByNetWork(imgUrl); //绘制二维码图片 //graphics.DrawImage(image, new PointF(5, 60)); graphics.DrawImage(image, new PointF( (image.Width / 2), (image.Height / 2)+5)); } /// <summary> /// 生成二维码图片(Bitmap) /// </summary> /// <param name="str"></param> /// <param name="scale"></param> /// <param name="version"></param> /// <returns></returns> public Bitmap CreateQRCode(string str,int scale=4,int version=7) { Bitmap bitmap; string enCodeString = str; //生成设置编码实例 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); //设置二维码的编码方式(BYTE:可识别文字、字母、数字,NUMERIC:只识别数字,不识别文字和字母,ALPHA_NUMERIC:可识别文字、字母、数字,数字识别更准) // qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //设置二维码的规模,默认4(1-40) qrCodeEncoder.QRCodeScale = scale; //设置二维码的版本,默认7 qrCodeEncoder.QRCodeVersion = version; //设置错误校验级别,默认中等 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //生成二维码图片 bitmap = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8); return bitmap; } /// <summary> /// 保存二维码图片 /// </summary> /// <param name="bitResult"></param> public bool SaveQRCode(Bitmap bitResult,string fileName,ref string qrCodeImgUrl) { bool result = false; string msg = string.Empty; try { //获取当前程序集的文件路径 string applicationPath = AppDomain.CurrentDomain.BaseDirectory.ToString(); //获取程序集路径+文件夹路径 string toServerPath = applicationPath + "\\" + ConfigurationManager.AppSettings["UploadImage"]; //拼接上年月文件夹( C:\\UploadImages\\Sys\\201904 ) toServerPath = toServerPath + "\\" + "QRCode"; //判断服务器文件夹路径是否存在 if (!Directory.Exists(toServerPath)) { //不存在路径,则创建 Directory.CreateDirectory(toServerPath); } string fullName = fileName.Contains('.') == true ? fileName : (fileName + ".png"); string savePath = toServerPath + "\\" + fullName; //保存二维码图片在photos路径下 // bitResult.Save("C://" + fileName + ".jpg"); bitResult.Save(savePath); //获取QRCode(二维码)的网络图片地址 qrCodeImgUrl = GetQRCodeImgUrl(applicationPath,savePath); result = true; } catch (Exception ex) { msg = ex.Message; result = false; } return result; } /// <summary> /// 获取网络图片的图片流对象 /// </summary> /// <param name="imgUrl">网络图片地址</param> /// <returns></returns> public Image GetImageByNetWork (string imgUrl) { WebRequest req = WebRequest.Create(imgUrl); WebResponse resp = req.GetResponse(); using (Stream stream = resp.GetResponseStream()) { return Image.FromStream(stream); } } /// <summary> /// 获取QRCode(二维码)的网络图片地址 /// </summary> /// <param name="appPath"></param> /// <param name="savePath"></param> /// <returns></returns> public string GetQRCodeImgUrl(string appPath,string savePath) { string result = string.Empty; //获取相对路径,返回:\UploadImages\QRCode\202409090015.png string relatePath = savePath.Substring(appPath.Length, savePath.Length - appPath.Length); //返回图片路径,将符号“\”替换成符号“/”,变成:/UploadImages/QRCode/202409090015.png relatePath = relatePath.Replace('\\', '/'); string qrServerUrl = ConfigurationManager.AppSettings["SetQRCodePrintServerUrl"] == null ? "" : ConfigurationManager.AppSettings["SetQRCodePrintServerUrl"].ToString(); //拼接二维码图片的网络地址(如:http://59.37.60.202:8000/scan/QRCode/202409090015.png) result = qrServerUrl + relatePath; return result; }
打印纸条信息如下:
参考网站:https://cloud.tencent.com/developer/information/%E7%9B%B4%E6%8E%A5%E6%89%93%E5%8D%B0%E5%88%B0%E6%89%93%E5%8D%B0%E6%9C%BAC%23%20MVC
标签:浏览器,string,C#,image,打印,二维码,mvc,new,图片 From: https://www.cnblogs.com/xielong/p/18427633