首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网页制作-> Discuz!专栏
在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)-.NET教程,C#语言
作者:网友供稿 点击:316
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
在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表示图像);




--------------------------------------------------------------------------------



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;
}

}

}
}

文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Eclipse + Lomboz + Tomcat JSP 开发配置-JSP教程,Jsp/Servlet
·利用Java调用可执行命令实例-JSP教程,Java技巧及代码
·彻底搞定JSP在线人数-JSP教程,Jsp/Servlet
·数据库操作类实现(C#,SqlClient)-.NET教程,C#语言
·在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)-.NET教程,C#语言
·结合PHP使用HTML表单(2)-PHP教程,PHP应用
·Java中利用JMF编写摄像头拍照程序-JSP教程,Java技巧及代码
·解析.Net框架下的XML编程技术-.NET教程,XML应用
·ASP.net Logion用户登陆验证代码-.NET教程,Asp.Net开发
·Java中精确计算的一个类用BigDecimal-JSP教程,Java技巧及代码
最新文章
·超越adsense:另类方法赚取巨额收益_网赚技巧
·google adwords优化技巧_网赚技巧
·自己误点adsense广告不用再通知google了_网赚技巧
·用fireworks滤镜轻松制作可爱gif动画_fireworks教程
·网站赚钱:google关键词广告创建的十二高招_站长心得
·提升网站使用性 打造实用性网站_站长心得
·最快速登录到google的10点主要经验_google推广
·制作主页的四十个技巧1_站长心得
·利用rss和gmail备份你的blog_站长心得
·seo终极方法_seo网站优化
相关主题
  • 在c#中实现socket端口复用_c#应用
  • 在c#中实现3层架构_c#应用
  • 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)-.NET教程,C#语言
  • 在C#中实现高性能计时-.NET教程,VB.Net语言
  • 在C#中实现消息过滤-.NET教程,C#语言
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭