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. }
相关文章
- el-table如何实现筛选功能,以及filters,filter_method详解
- web页面实现指定区域打印功能
- java翻译到mono C#实现系列(4) 利用CountDownTimer类实现倒计时功能 mono版
- C#委托(delegate)的常用方式- 委托的定义 // 委托的核心是跟委托的函数结构一样 public delegate string SayHello(string c); public delegate string SayHello(string c);:定义了一个公共委托类型 SayHello,该委托接受一个 string 类型的参数 c,并返回一个 string 类型的值。 Main 方法 static void Main(string args) { // 本质上其实就是把方法当作委托的参数 SayHello sayC = new SayHello(SayChinese); Console.WriteLine(sayC("欢迎大家")); SayHello sayE = new SayHello(SayEgnlish); Console.WriteLine(sayE("Welcome to")); // 简单的写法:必须类型一样 SayHello s1 = SayChinese; SayHello s2 = SayEgnlish; Console.WriteLine(s1("好好好")); Console.WriteLine(s2("Gooood")); // 最推荐 SayHello ss1 = con => con; Console.WriteLine(ss1("niiiice")); // 匿名委托:一次性委托 SayHello ss3 = delegate(string s) { return s; }; Console.WriteLine(ss3("说中国话")); } 常规实例化委托 SayHello sayC = new SayHello(SayChinese);:创建了一个 SayHello 委托的实例 sayC,并将 SayChinese 方法作为参数传递给委托的构造函数。 Console.WriteLine(sayC("欢迎大家"));:通过委托实例调用 SayChinese 方法,并输出结果。 同理,SayHello sayE = new SayHello(SayEgnlish); 和 Console.WriteLine(sayE("Welcome to")); 是对 SayEgnlish 方法的委托调用。 简化的委托赋值方式 SayHello s1 = SayChinese; 和 SayHello s2 = SayEgnlish;:当委托类型和方法签名一致时,可以直接将方法赋值给委托变量,无需使用 new 关键字。 Console.WriteLine(s1("好好好")); 和 Console.WriteLine(s2("Gooood"));:通过委托实例调用相应的方法。 使用 Lambda 表达式实例化委托 SayHello ss1 = con => con;:使用 Lambda 表达式创建委托实例 ss1,con => con 表示接受一个参数 con 并返回该参数本身。 Console.WriteLine(ss1("niiiice"));:通过委托实例调用 Lambda 表达式。 匿名委托 SayHello ss3 = delegate(string s) { return s; };:使用匿名委托创建委托实例 ss3,delegate(string s) { return s; } 是一个匿名方法,直接在委托实例化时定义了方法体。 Console.WriteLine(ss3("说中国话"));:通过委托实例调用匿名方法。 委托引用的方法定义 public static string SayChinese(string content) { return content; } public static string SayEgnlish(string content) { return content; } public static string SayChinese(string content) 和 public static string SayEgnlish(string content):定义了两个静态方法,分别接受一个 string 类型的参数 content,并返回该参数本身。这两个方法的签名与 SayHello 委托一致,可以被 SayHello 委托引用。 常规的委托实例化、简化的赋值方式、Lambda 表达式和匿名委托。委托在 C# 中是一种强大的机制,它允许将方法作为参数传递,实现了代码的灵活性和可扩展性。
- SpringBoot 实现“图片验证码”登录验证 功能 【简单实例】
- Java 实现打印文件详解(附demo)
- vue实现复制功能详解(使用v-clipboard)
- 安卓手机连接IP100蓝牙打印机实现打印功能
- vue实现打印功能
- 使用Redis实现中英文自动补全功能详解