欢迎光临
我们一直在努力

在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)-.NET教程,C#语言

建站超值云服务器,限时71元/月

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

}

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)-.NET教程,C#语言
分享到: 更多 (0)