根据用户的选择,开始打印:
if (result==DialogResult.OK)
{
docToPrint.Print();
}
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);
}//将事件处理函数添加到PrintDocument的PrintPage中
// 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;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
// 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;
}
}
}
}
相关推荐
在C#中实现打印功能(C#中PrintDialog-PrintDocument的使用
C#打印功能(PrintDialog,PrintDocument).
打印功能,打印一个图形,printDocument和printPreviewDialog的应用,C#源代码
主要介绍了C#打印类PrintDocument、PrintDialog、PrintPreviewDialog使用示例,本文分别给出了示例代码,需要的朋友可以参考下
C#中Windows打印组件的使用 PageSetupDialog 页面设置 PrintDialog 打印对话框(选择打印机) printDocument 打印的文档 printPreviewControl 打印预览文档 PrintPreviewDialog 显示打印预览
主要给大家介绍了关于C#利用PrintDocument定制打印单据的小例子,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
PageSetupDialog 、 PrintDialog 、 PrintDocument 、PrintPreviewDialog 使用
打印图像,C#源代码,显示打印设置对话框 PrintDialog MySettings = new PrintDialog(); MySettings.Document = this.printDocument1; if (MySettings.ShowDialog() == DialogResult.OK) { this.printDocument1....
如何在报表中计算百分比 Windows打印控件应用技巧 如何使用PrintDocument控件打印窗体数据 如何使用PrintPreviewContronl控件预览 使用PrintPreviewDialog对话框 如何使用PrintDialog...
要在vs2008中实现最基本的打印文本基本上需要以下几个步骤 1 新建项目(窗体) 2 在窗体上添加printDocument 控件 添加 Button textBox 控件 3 在 printDocument 控间的 PrintPage 事件中添加代码 e.Graphics....
求打印预览和打印的C#代码和命名空间 VS2005 C# 提问者:tomore 提问时间:08-10-20 20:29 其他答案 -------------------------------------------------------------------------------- winu 的答案 /// ...
用C#非报表形式做收银小票打印,PrintDocument控件和PrintDialog控件实现打印小票
打印对话框的应用,刚学的可以看看
打印方法小议,页面设置对话框、打印预览对话框、打印对话框等功能,C#源代码 //字符串流对 象,一行一行读取文本 private StringReader MyReader; private void button1_Click(object sender, EventArgs e) { ...
1009 如何在报表中使用文本对象 588 1010 如何移动对象操作 588 1011 如何对齐多个对象 589 1012 如何使用公式字段 589 1013 如何在报表中使用线 590 1014 如何在报表中使用方框 590 21.3 报表数据排序...
C#控件大全 C#属性大全 C#事件...21、PrintDialog控件和 PrintDocument 控件 15 22、用户自定义对话框 15 23、菜单控件 15 25、MDI 应用程序设计 16 26、键盘事件处理 17 27、鼠标事件处理 18 28、排列窗体上的控件 18
92 <br>0140 如何对计算结果四舍五放入 92 <br>0141 如何将商品金额小写转换成大写 92 <br>0142 如何根据生日自动计算员工年龄 93 <br>0143 如何设置货币值中使用的小数位数 93 <br>0144 ...
5.2.1 打印设置(PrintDocument类) 5.2.2 打印预览对话框(PrintPreviewDialog) 5.2.3 打印对话框(PrintDialog) 5.2.4 获取指定颜色值和字体样式 5.2.5 DataGridView控件的相关应用 5.3 设计过程 5.3.1 打印信息...