C#呼叫系統預設印表機列印文字和圖片
阿新 • • 發佈:2018-11-16
本方法適用於有列印驅動的印表機列印。直接用電腦預設印表機進行列印文字和圖片。
首先安裝印表機驅動,然後在裝置和印表機中將要用的印表機設為預設印表機,然後呼叫該方法即可
Pulic Void Print()
{
PrintService ps = new PrintService();
ps.StartPrint("33333","txt");//列印文字
ps.StartPrint(Image.FromFile(Application.StartupPath+"\\2.jpeg"), "image");//列印圖片
}
PrintService類原始碼
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 系統列印demo { public class PrintService { public PrintService() { // // TODO: 在此處新增建構函式邏輯 // this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage); }//將事件處理函式新增到PrintDocument的PrintPage中 // Declare the PrintDocument object. private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();//建立一個PrintDocument的例項 private string streamType; private string streamtxt; private Image streamima; // This method will set properties on the PrintDialog object and // then display the dialog. public void StartPrint(string txt, string streamType) { this.streamType = streamType; this.streamtxt = txt; // Allow the user to choose the page range he or she would // like to print. System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//建立一個PrintDialog的例項。 PrintDialog1.AllowSomePages = true; // Show the help button. PrintDialog1.ShowHelp = true; // Set the Document property to the PrintDocument for // which the PrintPage Event has been handled. To display the // dialog, either this property or the PrinterSettings property // must be set PrintDialog1.Document = docToPrint;//把PrintDialog的Document屬性設為上面配置好的PrintDocument的例項 DialogResult result = PrintDialog1.ShowDialog();//呼叫PrintDialog的ShowDialog函式顯示列印對話方塊,如果不要註釋即可,直接呼叫docToPrint.Print() // If the result is OK then print the document. if (result == DialogResult.OK) { docToPrint.Print();//開始列印 } } public void StartPrint(Image ima, string streamType) { this.streamType = streamType; this.streamima = ima; // Allow the user to choose the page range he or she would // like to print. System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//建立一個PrintDialog的例項。 PrintDialog1.AllowSomePages = true; // Show the help button. PrintDialog1.ShowHelp = true; // Set the Document property to the PrintDocument for // which the PrintPage Event has been handled. To display the // dialog, either this property or the PrinterSettings property // must be set PrintDialog1.Document = docToPrint;//把PrintDialog的Document屬性設為上面配置好的PrintDocument的例項 DialogResult result = PrintDialog1.ShowDialog();//呼叫PrintDialog的ShowDialog函式顯示列印對話方塊,如果不要註釋即可,直接呼叫docToPrint.Print() // If the result is OK then print the document. if (result == DialogResult.OK) { docToPrint.Print();//開始列印 } } // The PrintDialog will print the document // by handling the document's PrintPage event. private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//設定印表機開始列印的事件處理函式 { // Insert code to render the page here. // This code will be called when the control is drawn. // The following code will render a simple // message on the printed document switch (this.streamType) { case "txt": string text = null; System.Drawing.Font printFont = new System.Drawing.Font ("Arial", 35, System.Drawing.FontStyle.Regular); // Draw the content. text = streamtxt; e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y); break; case "image": System.Drawing.Image image = streamima; int x = e.MarginBounds.X; int y = e.MarginBounds.Y; int width = image.Width; int height = image.Height; if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height)) { width = e.MarginBounds.Width; height = image.Height * e.MarginBounds.Width / image.Width; } else { height = e.MarginBounds.Height; width = image.Width * e.MarginBounds.Height / image.Height; } System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height); e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel); break; default: break; } } } }