• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)(转)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

C#中实现打印功能(C#PrintDialog,PrintDocument的使用)

 

 C#中使用PrintDialog可以很方便的实现程序的打印功能。

其步骤如下:

  1. 创建一个PrintDialog的实例。如下:
    System.
    Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();
  2. 创建一个PrintDocument的实例.如下:
    System.Drawing.Printing.PrintDocument docToPrint =
       new System.Drawing.Printing.PrintDocument();
  3. 设置打印机开始打印的事件处理函数.函数原形如下
    void docToPrint_PrintPage(object sender,
       System.Drawing.Printing.PrintPageEventArgs e)
  4. 将事件处理函数添加到PrintDocumentPrintPage事件中
    docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
  5. 设置PrintDocument的相关属性,如:
    PrintDialog1.AllowSomePages = true;PrintDialog1.ShowHelp = true;
  6. PrintDialogDocument属性设为上面配置好的PrintDocument的实例
    PrintDialog1.Document = docToPrint;
  7. 调用PrintDialogShowDialog函数显示打印对话框
    DialogResult result = PrintDialog1.ShowDialog();
  8. 根据用户的选择,开始打印
    if (result==DialogResult.OK)
       {
        docToPrint.Print();
       }

例子如下:

使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);

 

using System;
using System.Drawing.Printing;
using System.
Windows.Forms;
using System.IO;

namespace EDImageSystem
{
 /// <summary>
 /// PrintService
的摘要说明。
 /// </summary>
 public class PrintService
 {
  public PrintService()
  {
   //
   // TODO:
在此处添加构造函数逻辑
   //
   this.docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
  }//
将事件处理函数添加到PrintDocumentPrintPage

  // Declare the PrintDocument object.
  private System.Drawing.Printing.PrintDocument docToPrint =
   new System.Drawing.Printing.PrintDocument();//
创建一个PrintDocument的实例

  private System.IO.Stream streamToPrint;
  string streamType;

  // This method will set properties on the PrintDialog object and
  // then display the dialog.
  public void StartPrint(Stream streamToPrint,string streamType)
  {

   this.streamToPrint=streamToPrint;
   this.streamType=streamType;
   // Allow the user to choose the page range he or she would
   // like to print.
   System.
Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();//创建一个PrintDialog的实例。
   PrintDialog1.AllowSomePages = true;

   // Show the help button.
   PrintDialog1.ShowHelp = true;

   // Set the Document property to the PrintDocument for
   // which the PrintPage Event has been handled. To display the
   // dialog, either this property or the PrinterSettings property
   // must be set
   PrintDialog1.Document = docToPrint;//
PrintDialogDocument属性设为上面配置好的PrintDocument的实例

   DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialogShowDialog函数显示打印对话框

   // If the result is OK then print the document.
   if (result==DialogResult.OK)
   {
    docToPrint.Print();//
开始打印
   }

  }

// The PrintDialog will print the document
  // by handling the document's PrintPage event.
  private void docToPrint_PrintPage(object sender,
   System.Drawing.Printing.PrintPageEventArgs e)//
设置打印机开始打印的事件处理函数
  {

   // Insert code to render the page here.
   // This code will be called when the control is drawn.

   // The following code will render a simple
   // message on the printed document
   switch(this.streamType)
   {
    case "txt":
     string text = null;
     System.Drawing.Font printFont = new System.Drawing.Font
      ("Arial", 35, System.Drawing.FontStyle.Regular);

     // Draw the content.
     System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
     text=streamReader.ReadToEnd();
     e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
     break;
    case "image":
     System.Drawing.Image image=System.Drawing.Image.FromStream(this.streamToPrint);
     int x=e.MarginBounds.X;
     int y=e.MarginBounds.Y;
     int width=image.Width;
     int height=image.Height;
     if((width/e.MarginBounds.Width)>(height/e.MarginBounds.Height))
     {
      width=e.MarginBounds.Width;
      height=image.Height*e.MarginBounds.Width/image.Width;
     }
     else
     {
      height=e.MarginBounds.Height;
      width=image.Width*e.MarginBounds.Height/image.Height;
     }
     System.Drawing.Rectangle destRect=new System.Drawing.Rectangle(x,y,width,height);
     e.Graphics.DrawImage(image,destRect,0,0,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel);
     break;
    default:
     break;
   }
   
  }

 }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中staticvoidMain(string[]args)的含义发布时间:2022-07-10
下一篇:
C#导入文件日期格式(dd/MM/yyyy)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap