自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#實現(xiàn)打印功能實例詳解

開發(fā) 后端
C#實現(xiàn)打印功能是通過什么來實現(xiàn)的?C#實現(xiàn)打印功能的步驟是什么呢?那么本文就向你介紹這方面的內容。

C#實現(xiàn)打印功能是通過使用PrintDialog控件來實現(xiàn)的。任何物有所值的應用程序都會擁有某種打印功能,不管是基本的打印功能還是更為復雜的打印功能,比如允許用戶只打印所選的文本或某個范圍內的頁面。本節(jié)將探討一下實現(xiàn)基本的C#打印功能,看看哪些類有助于打印文件中的文本。C#實現(xiàn)打印功能的過程是:在調用PrintDialog控件的ShowDialog方法之前,必須先設置PrintDialog類的Document屬性。該屬性接受一個PrintDocument類,以獲得打印機設置并將輸出內容發(fā)送給打印機。PrintDocument類需要System.Drawing.Printing名稱空間,因此,在定義使用PrintDocument類的對象前,必須包含這個名稱空間。

C#實現(xiàn)打印功能具體的操作步驟如下:

創(chuàng)建一個PrintDialog的實例。如下:

  1. System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();  

創(chuàng)建一個PrintDocument的實例.如下:

  1. System.Drawing.Printing.PrintDocument docToPrint =   
  2.  new System.Drawing.Printing.PrintDocument();  

設置打印機開始打印的事件處理函數(shù).函數(shù)原形如下:

  1. void docToPrint_PrintPage(object sender,   
  2.  System.Drawing.Printing.PrintPageEventArgs e)  

將事件處理函數(shù)添加到PrintDocument的PrintPage事件中。

  1. docToPrint.PrintPage+=  
  2.  
  3. new PrintPageEventHandler(docToPrint_PrintPage);   

設置PrintDocument的相關屬性,如:

  1. PrintDialog1.AllowSomePages =   
  2.  
  3. true;PrintDialog1.ShowHelp = true;   

把PrintDialog的Document屬性設為上面配置好的PrintDocument的實例:

  1. PrintDialog1.Document = docToPrint;  

調用PrintDialog的ShowDialog函數(shù)顯示打印對話框:

  1. DialogResult result = PrintDialog1.ShowDialog();  

根據(jù)用戶的選擇,開始打?。?/P>

  1. if (result==DialogResult.OK)  
  2.  {  
  3. docToPrint.Print();  
  4.  } 

C#實現(xiàn)打印功能的實例如下:

使用時先創(chuàng)建PrintService類的實例,然后調用void StartPrint(Stream streamToPrint,string streamType)函數(shù)開始打印。其中streamToPrint是要打印的內容(字節(jié)流),streamType是流的類型(txt表示普通文本,image表示圖像);

  1. using System;  
  2. using System.Drawing.Printing;  
  3. using System.Windows.Forms;  
  4. using System.IO;   
  5.  
  6. namespace EDImageSystem  
  7. {  
  8.  /// <summary>  
  9.  /// PrintService 的摘要說明。  
  10.  /// </summary>  
  11.  public class PrintService  
  12.  {  
  13. public PrintService()  
  14. {  
  15.  //  
  16.  // TODO: 在此處添加構造函數(shù)邏輯  
  17.  //  
  18.  this.docToPrint.PrintPage+=  
  19. new PrintPageEventHandler(docToPrint_PrintPage);  
  20. }//將事件處理函數(shù)添加到PrintDocument的PrintPage中   
  21.  
  22. // Declare the PrintDocument object.  
  23. private System.Drawing.Printing.PrintDocument docToPrint =   
  24.  new System.Drawing.Printing.PrintDocument();  
  25. //創(chuàng)建一個PrintDocument的實例   
  26.  
  27. private System.IO.Stream streamToPrint;  
  28. string streamType;   
  29.  
  30. // This method will set properties on the PrintDialog object and  
  31. // then display the dialog.  
  32. public void StartPrint(Stream streamToPrint,string streamType)  
  33. {   
  34.  
  35.  this.streamToPrint=streamToPrint;  
  36.  this.streamType=streamType;  
  37.  // Allow the user to choose the page range he or she would  
  38.  // like to print.  
  39.  System.Windows.Forms.PrintDialog PrintDialog1=  
  40. new PrintDialog ();//實現(xiàn)C#打印之創(chuàng)建一個PrintDialog的實例。  
  41.  PrintDialog1.AllowSomePages = true;   
  42.  
  43.  // Show the help button.  
  44.  PrintDialog1.ShowHelp = true;   
  45.  
  46.  // Set the Document property to the PrintDocument for   
  47.  // which the PrintPage Event has been handled. To display the  
  48.  // dialog, either this property or the PrinterSettings property   
  49.  // must be set   
  50.  PrintDialog1.Document = docToPrint;  
  51. //把PrintDialog的Document屬性設為上面配置好的PrintDocument的實例   
  52.  
  53.  DialogResult result = PrintDialog1.ShowDialog();  
  54. //調用PrintDialog的ShowDialog函數(shù)顯示打印對話框   
  55.  
  56.  // If the result is OK then print the document.  
  57.  if (result==DialogResult.OK)  
  58.  {  
  59. docToPrint.Print();//實現(xiàn)C#打印之開始打印  
  60.  }   
  61.  
  62. }   
  63.  
  64. // The PrintDialog will print the document  
  65. // by handling the document's PrintPage event.  
  66. private void docToPrint_PrintPage(object sender,   
  67.  System.Drawing.Printing.PrintPageEventArgs e)  
  68. //設置打印機開始打印的事件處理函數(shù)  
  69. {   
  70.  
  71.  // Insert code to render the page here.  
  72.  // This code will be called when the control is drawn.   
  73.  
  74.  // The following code will render a simple  
  75.  // message on the printed document  
  76.  switch(this.streamType)  
  77.  {  
  78. case "txt":  
  79.  string text = null;  
  80.  System.Drawing.Font printFont = new System.Drawing.Font  
  81. ("Arial", 35, System.Drawing.FontStyle.Regular);   
  82.  
  83.  // Draw the content.  
  84.  System.IO.StreamReader streamReader=  
  85. new StreamReader(this.streamToPrint);  
  86.  text=streamReader.ReadToEnd();  
  87.  e.Graphics.DrawString(text,printFont,  
  88. System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);  
  89.  break;  
  90. case "image":  
  91.  System.Drawing.Image image=  
  92. System.Drawing.Image.FromStream(this.streamToPrint);  
  93.  int x=e.MarginBounds.X;  
  94.  int y=e.MarginBounds.Y;  
  95.  int width=image.Width;  
  96.  int height=image.Height;  
  97.  if((width/e.MarginBounds.Width)>(  
  98. height/e.MarginBounds.Height))  
  99.  {  
  100. width=e.MarginBounds.Width;  
  101. height=image.Height*e.MarginBounds.Width/image.Width;  
  102.  }  
  103.  else 
  104.  {  
  105. height=e.MarginBounds.Height;  
  106. width=image.Width*e.MarginBounds.Height/image.Height;  
  107.  }  
  108.  System.Drawing.Rectangle destRect=  
  109. new System.Drawing.Rectangle(x,y,width,height);  
  110.  e.Graphics.DrawImage(image,  
  111. destRect,0,0,image.Width,image.Height,  
  112. System.Drawing.GraphicsUnit.Pixel);  
  113.  break;  
  114. default:  
  115.  break;  
  116.  }  
  117.    
  118. }   
  119.  
  120. }  
  121. }  

實現(xiàn)C#打印的具體實現(xiàn)步驟和具體的實例演示就向你介紹到這里,希望對你了解實現(xiàn)C#打印以及學習C#有所幫助。

【編輯推薦】

  1. 創(chuàng)建C#串口通信程序詳解
  2. 詳解C#串口監(jiān)聽的實現(xiàn)
  3. C#入門之概念簡介
  4. C#入門之C#特點淺析
  5. .NET Framework概念及開發(fā)淺析
責任編輯:仲衡 來源: 博客園
相關推薦

2009-08-26 11:07:36

C#打印窗體

2009-08-26 11:32:37

C#打印文檔

2009-08-26 10:43:14

C#實現(xiàn)打印功能

2009-08-26 12:59:08

C#打印設置

2009-08-21 10:13:02

C#異步初步

2009-08-26 13:22:24

C#打印程序

2009-08-26 11:53:56

C#打印文本文件

2009-09-03 19:00:15

C#判斷瀏覽器

2009-09-09 12:55:59

C# TextBox事

2009-09-03 14:55:56

C#實現(xiàn)DataGri

2009-08-26 12:14:44

C#打印設置

2009-08-18 10:14:19

C#插件構架

2009-08-20 11:01:51

C#操作內存

2009-09-11 12:31:52

C#實例詳解TypeConvert

2009-09-02 17:12:06

C#關機代碼

2009-08-26 09:54:45

C#打印預覽C#打印

2009-08-26 14:31:08

C#打印文件

2009-08-28 13:12:56

C#反射實例C#反射

2009-09-01 11:25:08

C#讀取Word文件

2009-09-02 19:12:37

C#遞歸
點贊
收藏

51CTO技術棧公眾號