实现C 实现财务自由需要多少钱
创始人
2024-05-05 00:01:39
0

如何在 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#实现打印功能

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...