温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么打印窗体

发布时间:2021-08-06 15:29:26 来源:亿速云 阅读:233 作者:Leah 栏目:编程语言

C#中怎么打印窗体,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

CopyFromScreen 方法来实现C#打印窗体的副本。

using System;  using System.Windows.Forms;  using System.Drawing;  using System.Drawing.Printing;   public class Form1 :   Form  {//实现C#打印窗体  private Button printButton = new Button();  private PrintDocument printDocument1 = new PrintDocument();   public Form1()   {   printButton.Text = "Print Form";   printButton.Click += new EventHandler(printButton_Click);   printDocument1.PrintPage +=   new PrintPageEventHandler(printDocument1_PrintPage);  this.Controls.Add(printButton);   }   void printButton_Click(object sender, EventArgs e)   {   CaptureScreen();   printDocument1.Print();   }  //实现C#打印窗体   Bitmap memoryImage;   private void CaptureScreen()   {   Graphics myGraphics = this.CreateGraphics();   Size s = this.Size;   memoryImage = new Bitmap(s.Width, s.Height, myGraphics);   Graphics memoryGraphics = Graphics.FromImage(memoryImage);   memoryGraphics.CopyFromScreen(  this.Location.X, this.Location.Y, 0, 0, s);   }   private void printDocument1_PrintPage(System.Object sender,     System.Drawing.Printing.PrintPageEventArgs e)   {   e.Graphics.DrawImage(memoryImage, 0, 0);   }      //实现C#打印窗体   public static void Main()   {   Application.Run(new Form1());   }  }

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

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

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

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

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

3、没有安装打印机。

◆C#打印窗体之安全

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI