C#实现打印功能是通过什么来实现的?C#实现打印功能的步骤是什么呢?那么本文就向你介绍这方面的内容。
C#实现打印功能是通过使用PrintDialog控件来实现的。任何物有所值的应用程序都会拥有某种打印功能,不管是基本的打印功能还是更为复杂的打印功能,比如允许用户只打印所选的文本或某个范围内的页面。本节将探讨一下实现基本的C#打印功能,看看哪些类有助于打印文件中的文本。C#实现打印功能的过程是:在调用PrintDialog控件的ShowDialog方法之前,必须先设置PrintDialog类的Document属性。该属性接受一个PrintDocument类,以获得打印机设置并将输出内容发送给打印机。PrintDocument类需要System.Drawing.Printing名称空间,因此,在定义使用PrintDocument类的对象前,必须包含这个名称空间。
C#实现打印功能具体的操作步骤如下:
创建一个PrintDialog的实例。如下:
1. System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();
创建一个PrintDocument的实例.如下:
2. System.Drawing.Printing.PrintDocument docToPrint =
3. new System.Drawing.Printing.PrintDocument();
设置打印机开始打印的事件处理函数.函数原形如下:
4. void docToPrint_PrintPage(object sender,
5. System.Drawing.Printing.PrintPageEventArgs e)
将事件处理函数添加到PrintDocument的PrintPage事件中。
6. docToPrint.PrintPage+=
7.
8. new PrintPageEventHandler(docToPrint_PrintPage);
设置PrintDocument的相关属性,如:
9. PrintDialog1.AllowSomePages =
10.
11. true;PrintDialog1.ShowHelp = true;
把PrintDialog的Document属性设为上面配置好的PrintDocument的实例:
12. PrintDialog1.Document = docToPrint;
调用PrintDialog的ShowDialog函数显示打印对话框:
13. DialogResult result = PrintDialog1.ShowDialog();
根据用户的选择,开始打印:
14. if (result==DialogResult.OK)
15. {
16. docToPrint.Print();
17. }
C#实现打印功能的实例如下:
使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);
18. using System;
19. using System.Drawing.Printing;
20. using System.Windows.Forms;
21. using System.IO;
22.
23. namespace EDImageSystem
24. {
25. /// <summary>
26. /// PrintService 的摘要说明。
27. /// </summary>
28. public class PrintService
29. {
30. public PrintService()
31. {
32. //
33. // TODO: 在此处添加构造函数逻辑
34. //
35. this.docToPrint.PrintPage+=
36. new PrintPageEventHandler(docToPrint_PrintPage);
37. }//将事件处理函数添加到PrintDocument的PrintPage中
38.
39. // Declare the PrintDocument object.
40. private System.Drawing.Printing.PrintDocument docToPrint =
41. new System.Drawing.Printing.PrintDocument();
42. //创建一个PrintDocument的实例
43.
44. private System.IO.Stream streamToPrint;
45. string streamType;
46.
47. // This method will set properties on the PrintDialog object and
48. // then display the dialog.
49. public void StartPrint(Stream streamToPrint,string streamType)
50. {
51.
52. this.streamToPrint=streamToPrint;
53. this.streamType=streamType;
54. // Allow the user to choose the page range he or she would
55. // like to print.
56. System.Windows.Forms.PrintDialog PrintDialog1=
57. new PrintDialog ();//实现C#打印之创建一个PrintDialog的实例。
58. PrintDialog1.AllowSomePages = true;
59.
60. // Show the help button.
61. PrintDialog1.ShowHelp = true;
62.
63. // Set the Document property to the PrintDocument for
64. // which the PrintPage Event has been handled. To display the
65. // dialog, either this property or the PrinterSettings property
66. // must be set
67. PrintDialog1.Document = docToPrint;
68. //把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
69.
70. DialogResult result = PrintDialog1.ShowDialog();
71. //调用PrintDialog的ShowDialog函数显示打印对话框
72.
73. // If the result is OK then print the document.
74. if (result==DialogResult.OK)
75. {
76. docToPrint.Print();//实现C#打印之开始打印
77. }
78.
79. }
80.
81. // The PrintDialog will print the document
82. // by handling the document's PrintPage event.
83. private void docToPrint_PrintPage(object sender,
84. System.Drawing.Printing.PrintPageEventArgs e)
85. //设置打印机开始打印的事件处理函数
86. {
87.
88. // Insert code to render the page here.
89. // This code will be called when the control is drawn.
90.
91. // The following code will render a simple
92. // message on the printed document
93. switch(this.streamType)
94. {
95. case "txt":
96. string text = null;
97. System.Drawing.Font printFont = new System.Drawing.Font
98. ("Arial", 35, System.Drawing.FontStyle.Regular);
99.
100. // Draw the content.
101. System.IO.StreamReader streamReader=
102. new StreamReader(this.streamToPrint);
103. text=streamReader.ReadToEnd();
104. e.Graphics.DrawString(text,printFont,
105. System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
106. break;
107. case "image":
108. System.Drawing.Image image=
109. System.Drawing.Image.FromStream(this.streamToPrint);
110. int x=e.MarginBounds.X;
111. int y=e.MarginBounds.Y;
112. int width=image.Width;
113. int height=image.Height;
114. if((width/e.MarginBounds.Width)>(
115. height/e.MarginBounds.Height))
116. {
117. width=e.MarginBounds.Width;
118. height=image.Height*e.MarginBounds.Width/image.Width;
119. }
120. else
121. {
122. height=e.MarginBounds.Height;
123. width=image.Width*e.MarginBounds.Height/image.Height;
124. }
125. System.Drawing.Rectangle destRect=
126. new System.Drawing.Rectangle(x,y,width,height);
127. e.Graphics.DrawImage(image,
128. destRect,0,0,image.Width,image.Height,
129. System.Drawing.GraphicsUnit.Pixel);
130. break;
131. default:
132. break;
133. }
134.
135. }
136.
137. }
138. }