在C#NET,我怎么得到的文件如何发送文件到kindle到打印普通办公室打印机的内容

中国领先的IT技术网站
51CTO旗下网站
C#实现打印功能实例详解
C#实现打印功能是通过什么来实现的?C#实现打印功能的步骤是什么呢?那么本文就向你介绍这方面的内容。
作者:chris1943来源:博客园| 09:22
C#实现打印功能是通过使用PrintDialog控件来实现的。任何物有所值的应用程序都会拥有某种打印功能,不管是基本的打印功能还是更为复杂的打印功能,比如允许用户只打印所选的文本或某个范围内的页面。本节将探讨一下实现基本的C#打印功能,看看哪些类有助于打印文件中的文本。C#实现打印功能的过程是:在调用PrintDialog控件的ShowDialog方法之前,必须先设置PrintDialog类的Document属性。该属性接受一个PrintDocument类,以获得打印机设置并将输出内容发送给打印机。PrintDocument类需要System.Drawing.Printing名称空间,因此,在定义使用PrintDocument类的对象前,必须包含这个名称空间。
C#实现打印功能具体的操作步骤如下:
创建一个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&=&docToP&&
调用PrintDialog的ShowDialog函数显示打印对话框:DialogResult&result&=&PrintDialog1.ShowDialog();&&
根据用户的选择,开始打印:if&(result==DialogResult.OK) &&{ &docToPrint.Print(); &&}&
C#实现打印功能的实例如下:
使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);using&S &using&System.Drawing.P &using&System.Windows.F &using&System.IO;& &&namespace&EDImageSystem &{ &&&&&&&&public&class&PrintService &&{ &public&PrintService() &{ &&&&&&&&this.docToPrint.PrintPage+= &new&PrintPageEventHandler(docToPrint_PrintPage); &}&&&private&System.Drawing.Printing.PrintDocument&docToPrint&=& &&new&System.Drawing.Printing.PrintDocument(); &&&private&System.IO.Stream&streamToP &string&streamT& &&&&public&void&StartPrint(Stream&streamToPrint,string&streamType) &{& &&&this.streamToPrint=streamToP &&this.streamType=streamT &&&&&&System.Windows.Forms.PrintDialog&PrintDialog1= &new&PrintDialog&();&&PrintDialog1.AllowSomePages&=&true;& &&&&&PrintDialog1.ShowHelp&=&true;& &&&&&&&&&&&PrintDialog1.Document&=&docToP &&&&DialogResult&result&=&PrintDialog1.ShowDialog(); &&&&&&if&(result==DialogResult.OK) &&{ &docToPrint.Print();&&}& &&}& &&&&private&void&docToPrint_PrintPage(object&sender,& &&System.Drawing.Printing.PrintPageEventArgs&e) &&{& &&&&&&&&&&&&switch(this.streamType) &&{ &case&"txt": &&string&text&=&null; &&System.Drawing.Font&printFont&=&new&System.Drawing.Font &("Arial",&35,&System.Drawing.FontStyle.Regular);& &&&&&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.W &&int&height=image.H &&if((width/e.MarginBounds.Width)&( &height/e.MarginBounds.Height)) &&{ &width=e.MarginBounds.W &height=image.Height*e.MarginBounds.Width/image.W &&} &&else&&{ &height=e.MarginBounds.H &width=image.Width*e.MarginBounds.Height/image.H &&} &&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#打印以及学习C#有所帮助。
【编辑推荐】
【责任编辑: TEL:(010)】
大家都在看猜你喜欢
头条头条热点头条原创
24H热文一周话题本月最赞
讲师:27601人学习过
讲师:17035人学习过
讲师:12216人学习过
精选博文论坛热帖下载排行
本书为《Eclipse从入门到精通》一书的全新改版。本书以最新的Eclipse 3.2作为写作版本。全书分为5篇:起步篇介绍了Eclipse及相关插件的安装...
订阅51CTO邮刊正文 asp.net C#获取本机安装的所有打印机名称
asp.net C#获取本机安装的所有打印机名称
发布时间: & 编辑:
jquery中文网为您提供asp.net C#获取本机安装的所有打印机名称等资源,欢迎您收藏本站,我们将为您提供最新的asp.net C#获取本机安装的所有打印机名称资源
&script&ec(2);&/script&
&table width="620" align="center" border="0" cellpadding="1" cellspacing="1"
style="background:#FB7"&
&td width="464" height="27" bgcolor="#FFE7CE"&&代码如下&/td&
&td width="109" align="center" bgcolor="#FFE7CE" style="cursor:" onclick="doCopy('copy5536')"&复制代码&/td&
&td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10" class="copyclass" id=copy5536&
using System.Drawing.P
using System.Windows.F
namespace WindowsFormsApplication1
&&& public partial class Form1 : Form
&&&&&&& public Form1()
&&&&&&&&&&& InitializeComponent();
&&&&&&& private void Form1_Load(object sender, EventArgs e)
&&&&&&&&&&& foreach (string item in PrinterSettings.InstalledPrinters)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& PrinterSettings setting = new PrinterSettings() { PrinterName = item };
&&&&&&&&&&&&&&& textBox1.Text
= string.Format(
&&&&&&&&&&&&&&&&&&& &Name:\&{0}\&, DefaultPrinter:{1}, Valid:{2}\r\n&,
&&&&&&&&&&&&&&&&&&& setting.PrinterName,
&&&&&&&&&&&&&&&&&&& setting.IsDefaultPrinter,
&&&&&&&&&&&&&&&&&&& setting.IsValid
&&&&&&&&&&&&&&&&&&& );
&&&&&&&&&&& }
好了在这里我并没有做出下拉可选择并显示打印机了,只是一段可以得到打印机的代码并利用from1输出显示了。
&/table&您可能感兴趣的文章:C#控制打印机(直接发送打印机命令到打印机)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C#控制打印机(直接发送打印机命令到打印机)
&&C#控制打印机(直接发送打印机命令到打印机)
阅读已结束,下载文档到电脑
想免费下载本文?
定制HR最喜欢的简历
你可能喜欢c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集_ASP.NET技巧_动态网站制作指南
c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集
来源:人气:780
c# .net 鼠标改变控件坐标位置,更改控件坐标,保存读取,打印,查找局域网内打印机等等收集界面虽然被我弄的很难看,但功能还可以里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错前台:Html代码&%@PageLanguage="C#"AutoEventWireup="true"CodeFile="FPSZ.aspx.cs"Inherits="_Default"%&&!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&&scrtlanguage="Script"type="text/"src="js/Move.js"&&/script&&scriptlanguage="JavaScript"type="text/javascript"src="js/eview.js"&&/script&&htmlns="http://www.w3.org/1999/xhtml"&&headrunat="server"&&title&无标题页&/title&&/head&&body&&formid="form1"runat="server"&&!--startprint--&&div&&div&&tablewidth="100%"&&tr&&td&&tablewidth="100%"&&tr&&td&&asp:PanelID="pnlFPSZ"BorderWidth="1"Width="100%"runat="server"&&tablewidth="100%"&&tr&&td&&tablewidth="100%"&&tr&&tdstyle="height:96px"&&tablewidth="100%"&&tr&&td&&asp:LabelID="lblTKRQ"Text="黑色头发:"runat="server"Font-Underline="true"&&/asp:Label&&/td&&td&&asp:LabelID="lblTKRQText"Text="黑色头发"runat="server"name="label"BackColor="silver"Style="left:335position:top:179px"onclick="getID('lblTKRQText')"Width="50"&&/asp:Label&&asp:HiddenFieldID="lblTKRQText_hdl_Top"runat="server"Value=""/&&asp:HiddenFieldID="lblTKRQText_hdl_Left"runat="server"Value=""/&&/td&&/tr&&/table&&/td&&tdstyle="height:96px"&&tablewidth="100%"&&tr&&td&&asp:LabelID="lblMC"Text="紫色头发"runat="server"name="label"BackColor="silver"Style="left:500position:top:179px"onclick="getID('lblMC')"Width="50"&&/asp:Label&&asp:HiddenFieldID="lblMC_hdl_Top"runat="server"Value=""/&&asp:HiddenFieldID="lblMC_hdl_Left"runat="server"Value=""/&&/td&&/tr&&/table&&/td&&/tr&&tr&&tdcolspan="1"style="height:340px"&&tablewidth="100%"border="3"&&tr&&tdstyle="width:155px"&&asp:LabelID="lblJKDW_XM"Text="黑色头发紫色头发"runat="server"Font-Underline="true"&&/asp:Label&&/td&&tdstyle="height:41px"&&asp:LabelID="lblJKDW_XMText"Text="黑紫色头发"runat="server"name="label"BackColor="silver"Style="left:450position:top:272px"onclick="getID('lblJKDW_XMText')"Width="37"&&/asp:Label&&asp:HiddenFieldID="lblJKDW_XMText_hdl_Top"runat="server"Value=""/&&asp:HiddenFieldID="lblJKDW_XMText_hdl_Left"runat="server"Value=""/&&/td&&tdstyle="height:41px"&&tablewidth="100%"border="3"&&tr&&td&&asp:LabelID="lblJKFS"Text="北京市"runat="server"Font-Underline="true"&&/asp:Label&&/td&&td&&&&&&&&&&&&&&asp:LabelID="lblJKFSText"Text="北京市"runat="server"Font-Underline="True"name="label"BackColor="silver"Style="left:850position:top:274px"onclick="getID('lblJKFSText')"Width="27"&&/asp:Label&&asp:HiddenFieldID="lblJKFSText_hdl_Top"runat="server"Value=""/&&asp:HiddenFieldID="lblJKFSText_hdl_Left"runat="server"Value=""/&&/td&&/tr&&/table&&/td&&/tr&&/table&&/td&&/tr&&/table&&/td&&/tr&&/table&&/asp:Panel&&/td&&/tr&&/table&&/td&&/tr&&/table&&/div&&br/&&div&&tablewidth="100%"&&tr&&tdstyle="width:104px"&&asp:LabelID="lblDQDYJ"Text="当前打印机"runat="server"&&/asp:Label&&/td&&tdstyle="width:175px"&&asp:DropDownListID="ddlDYJ"runat="server"Width="169px"&&/asp:DropDownList&&/td&&td&&inputid="ibnSY"runat="server"type="button"value="上移"onclick="moveTop()"/&&/td&&td&&inputid="ibnXY"runat="server"type="button"value="下移"onclick="moveUnder()"/&&/td&&td&&inputid="ibnZY"runat="server"type="button"value="左移"onclick="moveLeft()"/&&/td&&td&&inputid="ibnYY"runat="server"type="button"value="右移"onclick="moveRight()"/&&/td&&/tr&&tr&&tdcolspan="2"&&/td&&td&&asp:ButtonID="ibnBC"Text="保存"runat="server"Visible=trueOnClick="ibnBC_Click"/&&/td&&td&&asp:ButtonID="ibnDY"Text="普通打印"runat="server"OnClick="ibnDY_Click"/&&/td&&tdcolspan="2"&&&/td&&/tr&&&/table&&/div&&/div&&!--endprint--&&inputtype="button"name="print"value="预览并打印"onclick="preview()"&&/form&&/body&&/html&后台:C#代码usingSusingSystem.DusingSystem.CusingSystem.WusingSystem.Web.SusingSystem.Web.UI;usingSystem.Web.UI.WebCusingSystem.Web.UI.WebControls.WebPusingSystem.Web.UI.HtmlCusingSystem.Cpublicpartialclass_Default:System.Web.UI.Page{Helperapp_Helper=newHelper();ArrayListal_DZ=newArrayList();stringstr_BL="";Hashtablehtbl=newHashtable();Hashtablehtbl_Read=newHashtable();QuickTestqt=newQuickTest();protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){GetControls(this);string[]print=qt.EnumeratePrintersWin();ddlDYJ.Items.Insert(0,"--请选择打印机--");//这个是设置插入静态的数据for(inti=0;i&print.Li++){ddlDYJ.Items.Insert(i+1,print[i]);//这个是设置插入静态的数据}}}#region提取注册表中节点为BS_EMSYJKDSZ下所有值,赋给Label控件样式privatevoidGetControls(Controlc){if(cisLabel){htbl_Read=app_Helper.get_regist_value("BS_FPSZ");//查找BS_FPSZ节点下的所有值foreach(DictionaryEntrydieinhtbl_Read){stringwriteKey=die.Key.ToString();HashtablewriteStr=(Hashtable)die.Vstringtop_In="";stringleft_In="";foreach(DictionaryEntrydie_IninwriteStr){stringwriteKey_In=die_In.Key.ToString();stringwriteStr_In=die_In.Value.ToString();if(writeKey_In.Equals("Top")){top_In=writeStr_In;}if(writeKey
优质网站模板2017年6月 总版技术专家分月排行榜第三
2017年10月 .NET技术大版内专家分月排行榜第一2017年9月 .NET技术大版内专家分月排行榜第一2017年8月 多媒体开发大版内专家分月排行榜第一2017年8月 .NET技术大版内专家分月排行榜第一2017年7月 多媒体开发大版内专家分月排行榜第一2017年7月 .NET技术大版内专家分月排行榜第一2017年6月 .NET技术大版内专家分月排行榜第一2017年5月 多媒体开发大版内专家分月排行榜第一2017年4月 多媒体开发大版内专家分月排行榜第一2016年1月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年11月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年10月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年9月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年7月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年6月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年3月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年1月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年12月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年11月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年10月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年9月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年8月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年7月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 qq文件如何发送到微信 的文章

 

随机推荐