您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C#中怎么實(shí)現(xiàn)打印功能,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
C#實(shí)現(xiàn)打印功能具體的操作步驟如下:
創(chuàng)建一個(gè)PrintDialog的實(shí)例。如下:
System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();
創(chuàng)建一個(gè)PrintDocument的實(shí)例.如下:
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
設(shè)置打印機(jī)開(kāi)始打印的事件處理函數(shù).函數(shù)原形如下:
void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
將事件處理函數(shù)添加到PrintDocument的PrintPage事件中。
docToPrint.PrintPage+= new PrintPageEventHandler(docToPrint_PrintPage);
設(shè)置PrintDocument的相關(guān)屬性,如:
PrintDialog1.AllowSomePages = true;PrintDialog1.ShowHelp = true;
把PrintDialog的Document屬性設(shè)為上面配置好的PrintDocument的實(shí)例:
PrintDialog1.Document = docToPrint;
調(diào)用PrintDialog的ShowDialog函數(shù)顯示打印對(duì)話框:
DialogResult result = PrintDialog1.ShowDialog();
根據(jù)用戶(hù)的選擇,開(kāi)始打?。?/p>
if (result==DialogResult.OK) { docToPrint.Print(); }
C#實(shí)現(xiàn)打印功能的實(shí)例如下:
使用時(shí)先創(chuàng)建PrintService類(lèi)的實(shí)例,然后調(diào)用void StartPrint(Stream streamToPrint,string streamType)函數(shù)開(kāi)始打印。其中streamToPrint是要打印的內(nèi)容(字節(jié)流),streamType是流的類(lèi)型(txt表示普通文本,image表示圖像);
using System; using System.Drawing.Printing; using System.Windows.Forms; using System.IO; namespace EDImageSystem { /// <summary> /// PrintService 的摘要說(shuō)明。 /// </summary> public class PrintService { public PrintService() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // this.docToPrint.PrintPage+= new PrintPageEventHandler(docToPrint_PrintPage); }//將事件處理函數(shù)添加到PrintDocument的PrintPage中 // Declare the PrintDocument object. private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument(); //創(chuàng)建一個(gè)PrintDocument的實(shí)例 private System.IO.Stream streamToPrint; string streamType; // This method will set properties on the PrintDialog object and // then display the dialog. public void StartPrint(Stream streamToPrint,string streamType) { this.streamToPrint=streamToPrint; this.streamType=streamType; // Allow the user to choose the page range he or she would // like to print. System.Windows.Forms.PrintDialog PrintDialog1= new PrintDialog ();//實(shí)現(xiàn)C#打印之創(chuàng)建一個(gè)PrintDialog的實(shí)例。 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屬性設(shè)為上面配置好的PrintDocument的實(shí)例 DialogResult result = PrintDialog1.ShowDialog(); //調(diào)用PrintDialog的ShowDialog函數(shù)顯示打印對(duì)話框 // If the result is OK then print the document. if (result==DialogResult.OK) { docToPrint.Print();//實(shí)現(xiàn)C#打印之開(kāi)始打印 } } // The PrintDialog will print the document // by handling the document's PrintPage event. private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) //設(shè)置打印機(jī)開(kāi)始打印的事件處理函數(shù) { // 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. System.IO.StreamReader streamReader= new StreamReader(this.streamToPrint); text=streamReader.ReadToEnd(); e.Graphics.DrawString(text,printFont, System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y); break; case "image": System.Drawing.Image image= System.Drawing.Image.FromStream(this.streamToPrint); 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; } } } }
上述就是小編為大家分享的C#中怎么實(shí)現(xiàn)打印功能了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。