ImagePrinter 類是封裝好的打印類,打印前需要調用 public bool PrinterReady(Bitmap printContent) 方法傳入一個圖片文件, 之後就可以調用 PrintView() 進行打印浏覽,調用 PrintOut() 就可以打印了。 ExpressPrinter 類是調用 ImagePrinter 打印封裝類的一個實例,旨在與看看怎麼使用GDI+繪制需要打印的內容和字體。 ================================================ using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Drawing.Printing; using Lib.Biz.ClientService; using Lib.Utility.Extension; using PrintOrder.Utility; namespace PrintOrder.ExpressPrinter.PrintDoc { /// <summary> /// 打印圖片內容 /// </summary> public class ExpressPrinter { /// <summary> /// 創建快遞單打印對象實例 /// </summary> /// <param name="printerName">打印機名稱,如果為空將使用默認打印機</param> /// <param name="template">打印模板</param> /// <param name="items">打印項</param> /// <param name="isCoordinateRule">是否打印定位標尺</param> public ExpressPrinter(string printerName, Print_Template template, List<Print_Item> items, bool isCoordinateRule) { m_template = template; m_items = items; m_print = new ImagePrinter(printerName); m_isCoordinateRule = isCoordinateRule; InitPrinter(); } #region 變量 private Print_Template m_template; private List<Print_Item> m_items; private Bitmap m_bitmap = new Bitmap(10, 10); private Brush m_brush = new SolidBrush(Color.Black); private ImagePrinter m_print = null; private bool m_isCoordinateRule = false; #endregion /// <summary> /// 設置紙張大小 /// </summary> private void SetPrintPageSize() { //使用百分之一英寸為單位重新創建繪圖圖面 using (Graphics g = Graphics.FromImage(m_bitmap)) { //注:使用百分之一為單位 SizeF sizeF = new SizeF( CommonMethod.PixelsToInches(m_template.Width,g.DpiX)*100, CommonMethod.PixelsToInches(m_template.Height,g.DpiY)*100); Size size = Size.Ceiling(sizeF); m_print.DefaultPaperSize = new PaperSize("NewPage", size.Width,size.Height); } m_print.DefaultMargins = new Margins(0, 0, 0, 0); m_print.PrintDocName = string.Format("{0}({1})",m_template.TemplateName,m_template.Modifier); } /// <summary> /// 打印前初始化 /// </summary> private void InitPrinter() { SetPrintPageSize(); //設置紙張大小 m_bitmap = new Bitmap(m_template.Width, m_template.Height); Func<Print_Item, Font> getFont = (o => { FontStyle fontStyle = FontStyle.Regular; if (o.FontBold == 1) fontStyle = fontStyle | FontStyle.Bold; if (o.FontItalic == 1) fontStyle = fontStyle | FontStyle.Italic; if (o.FontStrikeout == 1) fontStyle = fontStyle | FontStyle.Strikeout; if (o.FontUnderline == 1) fontStyle = fontStyle | FontStyle.Underline; Font font = new Font(o.FontName, o.FontSize, fontStyle); return font; }); using (Graphics g = Graphics.FromImage(m_bitmap)) { g.PageUnit = GraphicsUnit.Pixel; //填充數據到圖片模板(位置要在制作圖片模板的時候度量好) g.FillRectangle(Brushes.White, 0f, 0f ,m_template.Width ,m_template.Height); //測試紙張大小 //this.DrawTestRectangle(g); if (m_isCoordinateRule) { TestPageSize(g); } foreach (Print_Item item in m_items) { g.DrawString( item.Text , getFont(item) , Brushes.Black , new RectangleF( item.Offset_X + m_template.Offset_X //設置全局打印偏移 X 坐標 ,item.Offset_Y + m_template.Offset_Y //設置全局打印偏移 Y 坐標 ,item.Width ,item.Height)); } g.Dispose(); } //m_bitmap.Save(@"D:\WorkTest\temp\express.jpg"); m_print.PrinterReady(m_bitmap); } private void DrawTestRectangle(Graphics g) { g.PageUnit = GraphicsUnit.Pixel; SizeF size = new SizeF( Utility.CommonMethod.MillimetersToPixel(30, g.DpiX) , Utility.CommonMethod.MillimetersToPixel(30, g.DpiY)); g.DrawRectangle(new Pen(Color.Black, 1), 50, 50, size.Width, size.Height); } /// <summary> /// 打印標尺 /// </summary> private void TestPageSize(Graphics g) { g.PageUnit = GraphicsUnit.Pixel; //八個定位方格 Size size = new Size(20,20); Point ltop = new Point(0,0); Point rtop = new Point(m_template.Width - size.Width, 0); Point ldwn = new Point(0,m_template.Height - size.Height); Point rdwn = new Point(m_template.Width - size.Width, m_template.Height - size.Height); Point tmdio = new Point(m_template.Width/2-size.Width/2,0); Point dmdio = new Point(m_template.Width / 2 - size.Width/ 2, m_template.Height - size.Height); Point lmdio = new Point(0, m_template.Height / 2 - size.Height / 2); Point rmdio = new Point(m_template.Width - size.Width, m_template.Height / 2 - size.Height / 2); //Point mdio = new Point(m_template.Width / 2 - size.Width / 2, m_template.Height / 2 - size.Height / 2); //左下和右上的限度矩形 Size tRec = new Size(m_template.Width/4,10); Size lRec = new Size(10, m_template.Height / 4); Point ldRec = new Point(0, lmdio.Y + size.Height + lRec.Height);//左下 Point rtRec = new Point(tmdio.X + size.Width + tRec.Width, 0);//右上 //十字中心 Size tdCrossSize = new Size(10,50); Size lrCrossSize = new Size(50,10); Point tdCrossPnt = new Point(m_template.Width / 2 - tdCrossSize.Width / 2, m_template.Height / 2 - tdCrossSize.Height/2); Point lrCrossPnt = new Point(m_template.Width / 2 - lrCrossSize.Width / 2, m_template.Height / 2 - lrCrossSize.Height / 2); g.FillRectangle(m_brush, new Rectangle(ltop, size)); g.FillRectangle(m_brush, new Rectangle(rtop, size)); g.FillRectangle(m_brush, new Rectangle(ldwn, size)); g.FillRectangle(m_brush, new Rectangle(rdwn, size)); g.FillRectangle(m_brush, new Rectangle(tmdio, size)); g.FillRectangle(m_brush, new Rectangle(dmdio, size)); g.FillRectangle(m_brush, new Rectangle(lmdio, size)); g.FillRectangle(m_brush, new Rectangle(rmdio, size)); g.FillRectangle(m_brush, new Rectangle(rtRec, tRec)); g.FillRectangle(m_brush, new Rectangle(ldRec, lRec)); g.FillRectangle(m_brush, new Rectangle(tdCrossPnt, tdCrossSize)); g.FillRectangle(m_brush, new Rectangle(lrCrossPnt, lrCrossSize)); string defaultSize = string.Format("紙張:{0}X{1}(px),{2}X{3}(mm)" , CommonMethod.InchesToPixels(m_print.DefaultPaperSize.Width / 100f,g.DpiX) , CommonMethod.InchesToPixels(m_print.DefaultPaperSize.Height / 100f, g.DpiY) , CommonMethod.PixelToMillimeters(m_print.DefaultPaperSize.Width,g.DpiX) , CommonMethod.PixelToMillimeters(m_print.DefaultPaperSize.Height, g.DpiY)); g.DrawString(defaultSize,new Font("宋體",9), m_brush,50,50); string defaultMargin = string.Format("頁邊距:上={0},下={1},左={2},右={3}" , CommonMethod.InchesToPixels(m_print.DefaultMargins.Top, g.DpiY) , CommonMethod.InchesToPixels(m_print.DefaultMargins.Bottom, g.DpiY) , CommonMethod.InchesToPixels(m_print.DefaultMargins.Left/ 100f,g.DpiX) , CommonMethod.InchesToPixels(m_print.DefaultMargins.Right/ 100f,g.DpiX)); g.DrawString(defaultMargin, new Font("宋體", 12), m_brush, 50, 70); string lrCrossStr = string.Format("左右各{0}毫米" , CommonMethod.PixelToMillimeters(m_template.Width / 2F, g.DpiX)); g.DrawString(lrCrossStr, new Font("宋體", 12) , m_brush , m_print.DefaultPaperSize.Width / 3F , m_print.DefaultPaperSize.Height / 2 + 30); string tdCrossStr = string.Format("上下各{0}毫米" , CommonMethod.PixelToMillimeters(m_template.Height / 2F, g.DpiY)); g.DrawString(tdCrossStr, new Font("宋體", 12) , m_brush , m_print.DefaultPaperSize.Width / 3F , m_print.DefaultPaperSize.Height / 2 + 45); } #region 公共方法 /// <summary> /// 打印預覽 /// </summary> /// <returns></returns> public DialogResult PrintProview() { return m_print.PrintView(); } /// <summary> /// 開始打印 /// </summary> public void PrintOut() { m_print.PrintOut(); } #endregion } } ====================================================== using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Drawing.Printing; using Lib.Utility.Extension; using PrintOrder.Utility; namespace PrintOrder.ExpressPrinter.PrintDoc { /// <summary> /// 打印圖片內容 /// </summary> public class ImagePrinter:IDisposable { public ImagePrinter(string printerName) { if (!printerName.IsNullOrEmpty()) this.DefaultPrinterName = printerName; //打印事件設置 m_printDoc.PrintPage += new PrintPageEventHandler(this.m_printDoc_PrintPage); } #region 變量 /// <summary> /// 打印內容 /// </summary> protected Bitmap m_printContent = null; /// <summary> /// 打印機名稱 /// </summary> protected string m_printerName = string.Empty; /// <summary> /// 打印預覽 /// </summary> PrintPreviewDialog m_printPreview = new PrintPreviewDialog(); /// <summary> /// 待打印文檔 /// </summary> PrintDocument m_printDoc = new PrintDocument(); #endregion #region 屬性 /// <summary> /// 待打印文檔名,打印隊列中的顯示值 /// </summary> /// <remarks>創建人員(日期):★草青工作室★(111216 12:13)</remarks> public string PrintDocName { set { m_printDoc.DocumentName = value; } get { return m_printDoc.DocumentName ; } } /// <summary> /// 獲取或設置默認紙張名稱 /// </summary> /// <remarks>創建人員(日期):★草青工作室★(111216 12:13)</remarks> public string DefaultPrinterName { set { m_printDoc.DefaultPageSettings.PrinterSettings.PrinterName = value; } get { return m_printDoc.DefaultPageSettings.PrinterSettings.PrinterName; } } /// <summary> /// 獲取或設置默認紙張大小 /// </summary> /// <remarks>創建人員(日期):★草青工作室★(111216 12:13)</remarks> public PaperSize DefaultPaperSize { set { m_printDoc.DefaultPageSettings.PaperSize = value; } get { return m_printDoc.DefaultPageSettings.PaperSize; } } /// <summary> /// 獲取或設置默認頁邊距 /// </summary> /// <remarks>創建人員(日期):★草青工作室★(111216 12:13)</remarks> public Margins DefaultMargins { set { m_printDoc.DefaultPageSettings.Margins = value; } get { return m_printDoc.DefaultPageSettings.Margins; } } #endregion #region 方法 /// <summary> /// 准備打印 /// </summary> /// <param name="printContent">打印內容</param> /// <returns></returns> /// <remarks>創建人員(日期):★草青工作室★(111217 16:45)</remarks> public bool PrinterReady(Bitmap printContent) { m_printContent = printContent; //打印事件設置 m_printDoc.PrintPage += new PrintPageEventHandler(this.m_printDoc_PrintPage); return true; } /// <summary> /// 打印浏覽 /// </summary> /// <remarks>創建人員(日期):★草青工作室★(111215 21:50)</remarks> public DialogResult PrintView() { //打印預覽 PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = m_printDoc; return ppd.ShowDialog(); } /// <summary> /// 開始打印 /// </summary> /// <remarks>創建人員(日期):★草青工作室★(111215 21:50)</remarks> public void PrintOut() { try { m_printDoc.Print(); } catch (Exception ex) { //RegisterLog.ExceptionsStack.RegisterError(ex); MessageBox.Show(ex.Message, "打印出錯,詳見異常日志!", MessageBoxButtons.OK, MessageBoxIcon.Error); m_printDoc.PrintController.OnEndPrint(m_printDoc, new PrintEventArgs()); } } /// <summary> /// 打印事件處理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void m_printDoc_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.PageUnit = GraphicsUnit.Pixel; //目標大小(注:打印區域使用的是‘百分之一英寸’為單位) int x = e.MarginBounds.X; int y = e.MarginBounds.Y; float width = CommonMethod.InchesToPixels((float)Math.Round(e.MarginBounds.Width/100.0,2),e.Graphics.DpiX); float height = CommonMethod.InchesToPixels((float)Math.Round(e.MarginBounds.Height / 100.0, 2), e.Graphics.DpiY); RectangleF destRectF = new RectangleF(x, y, width, height);//目標大小 //原圖大小 Rectangle srcRectF = new Rectangle(0, 0, m_printContent.Width, m_printContent.Height); //繪制打印內容www.2cto.com e.Graphics.DrawImage(m_printContent , destRectF, srcRectF , GraphicsUnit.Pixel); } #endregion public void Dispose() { if(m_printDoc != null) m_printDoc.PrintPage -= new PrintPageEventHandler(this.m_printDoc_PrintPage); } } }