国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

在C#中實現打印功能(C#中PrintDialog,PrintDocument的使用)

2024-07-21 02:18:50
字體:
來源:轉載
供稿:網友

最大的網站源碼資源下載站,

在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();
根據用戶的選擇,開始打?。?br>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;
}

}

}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北安市| 平山县| 剑川县| 达州市| 南丰县| 右玉县| 神木县| 宣威市| 肇东市| 吴江市| 孝感市| 墨脱县| 布尔津县| 绥德县| 永新县| 游戏| 祁连县| 红桥区| 榆中县| 济源市| 健康| 杭州市| 广平县| 青阳县| 米脂县| 高邮市| 句容市| 基隆市| 余庆县| 保亭| 铁力市| 鄂尔多斯市| 巩义市| 青神县| 嘉禾县| 秦皇岛市| 大石桥市| 宜兰市| 富源县| 彰化市| 张家港市|