Encapsulate a printed public category and share it with kids shoes.

Source: Internet
Author: User

The ImagePrinter class is a encapsulated printing class. before printing, you must call the public bool PrinterReady (Bitmap printContent) method to input an image file. Then, you can call PrintView () to print the image and call PrintOut () you can print it. The ExpressPrinter class is an instance that calls the ImagePrinter print encapsulation class. It is designed to show how to use GDI + to draw the content and font to be printed. ========================================================== ======== 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> // print the image content /// </summa Ry> public class ExpressPrinter {// <summary> /// create an instance of the printed object of the express order /// </summary> /// <param name = "printerName"> printer name, if it is null, the default printer </param> /// <param name = "template"> Print template </param> /// <param name = "items"> Print item </param> /// <param name = "isCoordinateRule"> whether to print the positioning ruler </param> public ExpressPrinter (string printerName, print_Template template, List <Print_Item> items, bool isCoordinateRule) {m_template = templat E; m_items = items; m_print = new ImagePrinter (printerName); response = isCoordinateRule; InitPrinter () ;}# region variable 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>/ // Set the paper size /// </summary> private void SetPrintPageSize () {// use 1 inch to recreate the drawing using (Graphics g = Graphics. fromImage (m_bitmap) {// note: the unit is SizeF sizeF = new SizeF (CommonMethod. pixelsToInches (m_template.Width, g. dpiX) * 100, CommonMethod. pixelsToInches (m_template.Height, g. dpiY) * 100); Size 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> /// initialize before printing /// </summary> private void InitPrinter () {SetPrintPageSize (); // set the paper size m_bitmap = new Bitmap (m_template.Width, m_template.Height); Func <Print_Item, Font> getFont = (o => {FontStyle fontStyle = FontStyle. regular; if (o. fontBo Ld = 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 = Graph IcsUnit. pixel; // fill in the data to the image template (the position must be measured when the image template is created) g. fillRectangle (Brushes. white, 0f, 0f, m_template.Width, m_template.Height); // test the paper size. // 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 // set the global offset X coordinate, item. offset_Y + m_template.Of Fset_Y // sets the global offset Y coordinate, 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> // print the ruler // </summary> private void TestPageSize (Graphics g) {g. pageUnit = GraphicsUnit. pixel; // eight positioning boxes Size = new size (20, 20); Point ltop = new Point (); 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.H Eight-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. wid Th/2, m_template.Height/2-size. height/2); // the upper left and lower right limit rectangle 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); // the lower left Point rtRec = new Point (tmdio. X + size. width + tRec. width, 0); // top right corner // cross center Size tdCrossSize = new Size (); Size lrCrossSize = new Size (); 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 (tdCross Pnt, tdCrossSize); g. fillRectangle (m_brush, new Rectangle (lrCrossPnt, lrCrossSize); string defaultSize = string. format ("Paper: {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. pixelToM Illimeters (m_print.DefaultPaperSize.Height, g. dpiY); g. drawString (defaultSize, new Font ("", 9), m_brush, 50, 50); string defaultMargin = string. format ("margin: Top = {0}, Bottom = {1}, Left = {2}, Right = {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), CommonMetho D. inchesToPixels (m_print.DefaultMargins.Right/100f, g. dpiX); g. drawString (defaultMargin, new Font ("", 12), m_brush, 50, 70); string lrCrossStr = string. format ("{0} millimeters each", 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 = str Ing. format ("Up and down {0} millimeters", CommonMethod. pixelToMillimeters (m_template.Height/2F, g. dpiY); g. drawString (tdCrossStr, new Font ("", 12), m_brush, m_print.DefaultPaperSize.Width/3F, m_print.defapappapersize.height/2 + 45 );} # region public method // <summary> // print preview /// </summary> /// <returns> </returns> public DialogResult PrintProview () {return m_print.PrintView () ;}/// <summary> /// start printing /// </ 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> /// Print the image content /// </summary> public class ImagePrinter: IDisposable {public ImagePrinter (string printerName) {if (! PrinterName. isNullOrEmpty () this. defaultPrinterName = printerName; // print event settings m_printDoc.PrintPage + = new PrintPageEventHandler (this. m_printDoc_PrintPage);} # region variable // <summary> // print the content /// </summary> protected Bitmap m_printContent = null; /// <summary> /// printer name /// </summary> protected string m_printerName = string. empty; // <summary> // print preview /// </summary> PrintPreviewDialog m_printPreview = new PrintPreviewDialog (); /// <summary> /// document to be printed /// </summary> PrintDocument m_printDoc = new PrintDocument (); # endregion # region attributes /// <summary> /// file name to be printed, print the displayed value in the queue /// </summary> /// <remarks> creator (date ):★Caoqing Studio★(111216) </remarks> public string PrintDocName {set {m_printDoc.DocumentName = value;} get {return m_printDoc.DocumentName ;}} /// <summary> /// get or set the default paper name /// </summary> /// <remarks> creator (date ):★Caoqing Studio★(111216) </remarks> public string DefaultPrinterName {set {m_printDoc.DefaultPageSettings.PrinterSettings.PrinterName = value;} get {return success ;}} /// <summary> /// get or set the default paper size /// </summary> /// <remarks> creator (date ):★Caoqing Studio★(111216) </remarks> public PaperSize DefaultPaperSize {set {m_printdoc.defapagpagesettings.papersize = value;} get {return m_printdoc.defapagpagesettings.papersize ;}} /// <summary> /// get or set the default margin /// </summary> /// <remarks> creator (date ):★Caoqing Studio★(111216) </remarks> public Margins DefaultMargins {set {m_printDoc.DefaultPageSettings.Margins = value;} get {return m_printdoc.defapagpagesettings.margins ;}} # endregion # region method // <summary> // prepare for printing /// </summary> /// <param name = "printContent"> print content </param> /// <returns> </returns> /// <remarks> creator (date):★Caoqing Studio★(111217) </remarks> public bool PrinterReady (Bitmap printContent) {m_printContent = printContent; // print event settings m_printDoc.PrintPage + = new PrintPageEventHandler (this. m_printDoc_PrintPage); return true ;}/// <summary> // print the Browse /// </summary> /// <remarks> creator (date ):★Caoqing Studio★(111215) </remarks> public DialogResult PrintView () {// print the preview PrintPreviewDialog ppd = new PrintPreviewDialog (); ppd. document = m_printDoc; return ppd. showDialog () ;}/// <summary> /// start printing //</summary> /// <remarks> creator (date ):★Caoqing Studio★(111215) </remarks> public void PrintOut () {try {m_printDoc.Print ();} catch (Exception ex) {// RegisterLog. predictionsstack. registerError (ex); MessageBox. show (ex. message, "print error. For details, see the exception log! ", MessageBoxButtons. OK, MessageBoxIcon. error); m_printDoc.PrintController.OnEndPrint (m_printDoc, new PrintEventArgs ());}} /// <summary> /// print event processing /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void m_printDoc_PrintPage (object sender, printPageEventArgs e) {e. graphics. pageUnit = GraphicsUnit. pixel; // target size (Note: The print area is in the unit of 1 inch) int x = e. marginBounds. x; in T 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); // target size // source size Rectangle srcRectF = new Rectangle (0, 0, m_printContent.Width, m_printContent.Hei Ght); // draw printed content 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 );}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.