实现C#打印窗体实例详解

开发 后端
实现C#打印窗体实例向你详细介绍了在 Windows 下实现C#打印窗体的具体步骤和实现实例演示。希望对你学习C#打印窗体有所帮助。

如何在 Windows 下实现C#打印窗体作为C#开发过程的一部分,通常会希望C#打印窗体的副本。下面的代码示例演示如何使用 CopyFromScreen 方法来实现C#打印窗体的副本。

  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Drawing;  
  4. using System.Drawing.Printing;  
  5.  
  6. public class Form1 :  
  7.  Form  
  8. {//实现C#打印窗体  
  9. private Button printButton = new Button();  
  10. private PrintDocument printDocument1 = new PrintDocument();  
  11.  
  12. public Form1()  
  13.  {  
  14.  printButton.Text = "Print Form";  
  15.  printButton.Click += new EventHandler(printButton_Click);  
  16.  printDocument1.PrintPage +=   
  17. new PrintPageEventHandler(printDocument1_PrintPage);  
  18. this.Controls.Add(printButton);  
  19.  }  
  20.  
  21. void printButton_Click(object sender, EventArgs e)  
  22.  {  
  23.  CaptureScreen();  
  24.  printDocument1.Print();  
  25.  }  
  26. //实现C#打印窗体  
  27.  Bitmap memoryImage;  
  28.  
  29. private void CaptureScreen()  
  30.  {  
  31.  Graphics myGraphics = this.CreateGraphics();  
  32.  Size s = this.Size;  
  33.  memoryImage = new Bitmap(s.Width, s.Height, myGraphics);  
  34.  Graphics memoryGraphics = Graphics.FromImage(memoryImage);  
  35.  memoryGraphics.CopyFromScreen(  
  36. this.Location.X, this.Location.Y, 0, 0, s);  
  37.  }  
  38.  
  39. private void printDocument1_PrintPage(System.Object sender,     
  40. System.Drawing.Printing.PrintPageEventArgs e)  
  41.  {  
  42.  e.Graphics.DrawImage(memoryImage, 0, 0);  
  43.  }  
  44.  
  45.    //实现C#打印窗体  
  46.  
  47. public static void Main()  
  48.  {  
  49.  Application.Run(new Form1());  
  50.  }  
  51. }  

◆C#打印窗体之编译代码

这是一个完整的代码示例,其中包含 Main 方法。

◆C#打印窗体之可靠编程

1、以下情况可能会导致异常:

2、您没有访问该打印机的权限。

3、没有安装打印机。

◆C#打印窗体之安全

为了运行此代码示例,您必须能够访问与计算机一起使用的打印机。

C#打印窗体的具体内容就向你介绍到这里,希望对你了解和学习C#打印窗体有所帮助。

【编辑推荐】

  1. C#入门之C#特点浅析
  2. .NET Framework概念及开发浅析
  3. C#实现打印功能实例详解
  4. 浅析C#打印和C#打印预览的实现
  5. 全面解析C#实现打印功能
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-26 11:32:37

C#打印文档

2009-08-26 09:22:44

C#实现打印功能

2009-09-07 06:18:57

C#窗体设计器

2009-09-07 03:44:50

C#窗体间传值

2009-09-07 06:31:32

C#窗体移动

2009-08-26 12:59:08

C#打印设置

2009-09-07 06:48:13

C#透明窗体

2009-08-21 10:13:02

C#异步初步

2009-08-26 11:53:56

C#打印文本文件

2009-09-09 12:55:59

C# TextBox事

2009-09-07 05:10:52

C#模式窗体

2009-08-26 12:14:44

C#打印设置

2009-08-25 09:19:01

C#实现窗体显示

2009-09-01 18:06:06

c#保存窗体状态

2009-04-28 16:18:29

.Net MicroSideShow

2009-08-26 09:54:45

C#打印预览C#打印

2009-09-02 17:12:06

C#关机代码

2009-08-26 14:31:08

C#打印文件

2009-08-18 10:14:19

C#插件构架

2009-09-11 12:31:52

C#实例详解TypeConvert
点赞
收藏

51CTO技术栈公众号