C# 打印

## 使用fastreport打印 //private bool GodownEntryPrint(string instorageNumber, string localhostCode) private bool GodownEntryPrint(string instorageNumber) { try { //创建Report对象 var report = new Report(); //获得模板的路径 string reportLabel = Application.StartupPath + @"\" + "code.frx"; //判断文件是否存在 if (!File.Exists(reportLabel)) { MessageBox.Show("打印模板不存在!请先获取打印模板!"); } //清空 report.Clear(); //加载报表模板 report.Load(reportLabel); ////获取模板中,对应的TextObject节点-替换模板内容 //var textObject = report.FindObject("Text2") as TextObject; //if (textObject != null) textObject.Text = localhostCode; //库位编码 //同理:一/二维码文本替换 var barcodeObject = report.FindObject("Barcode1") as BarcodeObject; if (barcodeObject != null) { barcodeObject.Text = instorageNumber; //入库单号 //更改一维码的生成方式为Code128码 barcodeObject.Barcode = new Barcode128(); } //预览面单 //report.Show(); //默认不显示打印机选择页面 report.PrintSettings.ShowDialog = false; //获取打印机的名称,这里是通过封装的方法去获取打印机名,这里可以直接指定“打印机名称”; string strPrintName = printerComboBox.Text; if (string.IsNullOrWhiteSpace(strPrintName)) { MessageBox.Show("未先选择打印机"); return false; } //当前操作打印机 report.PrintSettings.Printer = strPrintName; //启动打印 report.Print(); } catch (Exception) { return false; } return true; } ## 使用图片拼接打印 string file; private void PicturePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //打开文件 FileStream fs = File.OpenRead(file); //OpenRead //转换为BYTE int filelength = 0; filelength = (int)fs.Length; //获得文件长度 Byte[] imageByte = new Byte[filelength]; //建立一个字节数组 fs.Read(imageByte, 0, filelength); //按字节流读取 //转换为 IMAGE System.Drawing.Image image = System.Drawing.Image.FromStream(fs); fs.Close(); e.Graphics.DrawImage(image, 0, 0); //img大小 //e.Graphics.DrawString(TicCode, DrawFont, brush, 600, 600); //绘制字符串 e.HasMorePages = false; } public void printwork() { if (printerComboBox.Text == "") { MessageBox.Show("请在首页选择默认打印机"); } else { //打印字符串用 SolidBrush brush = new SolidBrush(System.Drawing.Color.Black); Font DrawFont = new Font("Arial", 22); PrintDocument pd = new PrintDocument(); pd.PrintPage += PicturePrintDocument_PrintPage; //注册打印事件 pd.PrinterSettings.PrinterName = printerComboBox.Text; //打印机选择,使用默认打印机不需要设置 pd.Print(); } }