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

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

    其步骤如下:

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

    例子如下:

    使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);
   
None.gif   using  System;
None.gif    
using  System.Drawing.Printing;
None.gif    
using  System.Windows.Forms;
None.gif    
using  System.IO;
None.gif
None.gif    
namespace  EDImageSystem
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 〈summary〉
InBlock.gif    
/// PrintService 的摘要说明。
ExpandedSubBlockEnd.gif    
/// 〈/summary〉

InBlock.gif    public class PrintService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
public PrintService()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
//
InBlock.gif    
// TODO: 在此处添加构造函数逻辑
InBlock.gif    
//
InBlock.gif
    this.docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
ExpandedSubBlockEnd.gif    }
//将事件处理函数添加到PrintDocument的PrintPage中
InBlock.gif
InBlock.gif    
// Declare the PrintDocument object.
InBlock.gif
    private System.Drawing.Printing.PrintDocument docToPrint =
InBlock.gif    
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例
InBlock.gif

InBlock.gif    
private System.IO.Stream streamToPrint;
InBlock.gif    
string streamType;
InBlock.gif
InBlock.gif    
// This method will set properties on the PrintDialog object and
InBlock.gif    
// then display the dialog.
InBlock.gif
    public void StartPrint(Stream streamToPrint,string streamType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif    
this.streamToPrint=streamToPrint;
InBlock.gif    
this.streamType=streamType;
InBlock.gif    
// Allow the user to choose the page range he or she would
InBlock.gif    
// like to print.
InBlock.gif
    System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();//创建一个PrintDialog的实例。
InBlock.gif
    PrintDialog1.AllowSomePages = true;
InBlock.gif
InBlock.gif    
// Show the help button.
InBlock.gif
    PrintDialog1.ShowHelp = true;
InBlock.gif
InBlock.gif    
// Set the Document property to the PrintDocument for
InBlock.gif    
// which the PrintPage Event has been handled. To display the
InBlock.gif    
// dialog, either this property or the PrinterSettings property
InBlock.gif    
// must be set
InBlock.gif
    PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
InBlock.gif

InBlock.gif    DialogResult result 
= PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
InBlock.gif
InBlock.gif    
// If the result is OK then print the document.
InBlock.gif
    if (result==DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    docToPrint.Print();
//开始打印
ExpandedSubBlockEnd.gif
    }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// The PrintDialog will print the document
InBlock.gif    
// by handling the document’s PrintPage event.
InBlock.gif
    private void docToPrint_PrintPage(object sender,
InBlock.gif    System.Drawing.Printing.PrintPageEventArgs e)
//设置打印机开始打印的事件处理函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif
InBlock.gif    
// Insert code to render the page here.
InBlock.gif    
// This code will be called when the control is drawn.
InBlock.gif
InBlock.gif    
// The following code will render a simple
InBlock.gif    
// message on the printed document
InBlock.gif
    switch(this.streamType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case “txt“:
InBlock.gif    
string text = null;
InBlock.gif    System.Drawing.Font printFont 
= new System.Drawing.Font
InBlock.gif    (“Arial“, 
35, System.Drawing.FontStyle.Regular);
InBlock.gif
InBlock.gif    
// Draw the content.
InBlock.gif
    System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
InBlock.gif    text
=streamReader.ReadToEnd();
InBlock.gif    e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
InBlock.gif    
break;
InBlock.gif    
case “image“:
InBlock.gif    System.Drawing.Image image
=System.Drawing.Image.FromStream(this.streamToPrint);
InBlock.gif    
int x=e.MarginBounds.X;
InBlock.gif    
int y=e.MarginBounds.Y;
InBlock.gif    
int width=image.Width;
InBlock.gif    
int height=image.Height;
InBlock.gif    
if((width/e.MarginBounds.Width)〉(height/e.MarginBounds.Height))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    width
=e.MarginBounds.Width;
InBlock.gif    height
=image.Height*e.MarginBounds.Width/image.Width;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    height
=e.MarginBounds.Height;
InBlock.gif    width
=image.Width*e.MarginBounds.Height/image.Height;
ExpandedSubBlockEnd.gif    }

InBlock.gif    System.Drawing.Rectangle destRect
=new System.Drawing.Rectangle(x,y,width,height);
InBlock.gif    e.Graphics.DrawImage(image,destRect,
0,0,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel);
InBlock.gif    
break;
InBlock.gif    
default:
InBlock.gif    
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    }
 

转载于:https://www.cnblogs.com/kkdd/archive/2006/05/23/406817.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#实现打印功能的步骤如下: 1. 引入命名空间 ```csharp using System.Drawing.Printing; using System.Windows.Forms; ``` 2. 创建打印对话框 ```csharp PrintDialog printDialog = new PrintDialog(); ``` 3. 显示打印对话框 ```csharp if (printDialog.ShowDialog() == DialogResult.OK) { // 用户点击了“打印”按钮 } ``` 4. 创建打印文档对象 ```csharp PrintDocument printDocument = new PrintDocument(); ``` 5. 设置打印文档的打印机 ```csharp printDocument.PrinterSettings = printDialog.PrinterSettings; ``` 6. 设置打印文档的打印事件 ```csharp printDocument.PrintPage += new PrintPageEventHandler(PrintPage); ``` 7. 实现打印事件 ```csharp private void PrintPage(object sender, PrintPageEventArgs e) { // 在此处编写打印逻辑 } ``` 8. 开始打印 ```csharp printDocument.Print(); ``` 完整的代码示例: ```csharp using System.Drawing.Printing; using System.Windows.Forms; public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { PrintDocument printDocument = new PrintDocument(); printDocument.PrinterSettings = printDialog.PrinterSettings; printDocument.PrintPage += new PrintPageEventHandler(PrintPage); printDocument.Print(); } } private void PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString("Hello, World!", new Font("Arial", 12), Brushes.Black, new PointF(0, 0)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值