问题描述
问题1:每打印一页需要2秒左右,执行打印的代码块运行时间是毫秒级的..不知道哪里耗费的时间?intpWidth=pd.PrinterSettings.DefaultPageSettings.PaperSize.Width
这句执行要3秒,,怎么这么慢?问题2:打印预览以后,,如何指定打印某页???下面是代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;usingSystem.Drawing.Printing;usingSystem.Collections;usingSystem.Windows.Forms;namespaceAZBookManger.CaiBian{classBookLabelToPrint{publicboolisFX=false;publicintrowCount=0;//每页打印书标行数publicintcolCount=0;//列数intbookCount=0;//书标总数intbookPageCouct=0;//每页打印的书标数publicfloattop=0;publicfloatbottom=0;publicfloatleft=0;publicfloatright=0;publicfloatrowspan=0;publicfloatcolspan=0;publicFontfont=Form1.FontLabelBcid;SizeFbooklabelSize;privateArrayListlst;privateintstartIndex=0;publicBookLabelToPrint(ArrayListl){this.lst=l;}publicvoidprint(){bookPageCouct=rowCount*colCount;bookCount=lst.Count;PrintDocumentpd=newPrintDocument();intpWidth=pd.PrinterSettings.DefaultPageSettings.PaperSize.Width;intPHeight=pd.PrinterSettings.DefaultPageSettings.PaperSize.Height;booklabelSize.Width=(pWidth-left-right-(colCount-1)*colspan)/colCount;booklabelSize.Height=(PHeight-top-bottom-(rowCount-1)*rowspan)/rowCount;pd.PrintController=newStandardPrintController();pd.PrintPage+=newPrintPageEventHandler(pd_PrintPage);pd.Print();}publicvoidprintPreview(){DateTimestart=DateTime.Now;Console.WriteLine(DateTime.Now.ToLongTimeString());bookPageCouct=rowCount*colCount;bookCount=lst.Count;Console.WriteLine(DateTime.Now.ToLongTimeString());PrintDocumentpd=newPrintDocument();Console.WriteLine(DateTime.Now.ToLongTimeString());intpWidth=pd.PrinterSettings.DefaultPageSettings.PaperSize.Width;//这里耗费3秒Console.WriteLine(DateTime.Now.ToLongTimeString());intPHeight=pd.PrinterSettings.DefaultPageSettings.PaperSize.Height;//这里也是3秒,怎么获取这么慢Console.WriteLine(DateTime.Now.ToLongTimeString());booklabelSize.Width=(pWidth-left-right-(colCount-1)*colspan)/colCount;booklabelSize.Height=(PHeight-top-bottom-(rowCount-1)*rowspan)/rowCount;Console.WriteLine(DateTime.Now.ToLongTimeString());pd.PrintController=newStandardPrintController();pd.PrintPage+=newPrintPageEventHandler(pd_PrintPage);Console.WriteLine(DateTime.Now.ToLongTimeString());PrintPreviewDialogcppd=newPrintPreviewDialog();cppd.Document=pd;Console.WriteLine(DateTime.Now.ToLongTimeString());cppd.Document.PrintController=newStandardPrintController();Console.WriteLine(DateTime.Now.ToLongTimeString());cppd.ShowDialog();DateTimestop=DateTime.Now;Console.WriteLine(stop.ToLongTimeString());TimeSpanspan=stop-start;Console.WriteLine(span.TotalSeconds.ToString());}voidpd_PrintPage(objectsender,PrintPageEventArgse){//这块代码耗费时间是毫秒级的,但是每次执行之前要耗费2秒时间,怎么回事????Console.WriteLine("P0:"+DateTime.Now.ToLongTimeString());Graphicsg=e.Graphics;floatcurrentTop=top;FontbidFont=Form1.FontLabelBid;if(bookCount-startIndex<bookPageCouct)//如果未打印条码数小于每页可打印条码数{bookPageCouct=bookCount-startIndex;//最后一页条码数}for(inti=0;i<bookPageCouct;i++){intii=startIndex+i;BookLabelbl=(BookLabel)lst[ii];if(isFX){stringbc=string.Empty;stringid=string.Empty;try{bc=bl.bcid.Substring(0,bl.bcid.IndexOf("/"));id=bl.bcid.Substring(bl.bcid.IndexOf("/")+1,bl.bcid.Length-bl.bcid.IndexOf("/")-1);}catch{bc=bl.bcid.ToString();}SizeFbcSize=g.MeasureString(bc,font);SizeFidSize=g.MeasureString(id,font);SizeFbidSize=g.MeasureString(bl.bid,bidFont);floatcurrentX=left+((booklabelSize.Width+rowspan)*(i%colCount))+(booklabelSize.Width-bcSize.Width)/2;floatcurrentY=top+((booklabelSize.Height+colspan)*(Convert.ToInt32(i/colCount)%rowCount))+(booklabelSize.Height-bcSize.Height)/2;g.DrawString(bc,font,Brushes.Black,newPointF(currentX,currentY));currentX=left+((booklabelSize.Width+rowspan)*(i%colCount))+(booklabelSize.Width-idSize.Width)/2;currentY=currentY+bcSize.Height;g.DrawString(id,font,Brushes.Black,newPointF(currentX,currentY));currentX=left+((booklabelSize.Width+rowspan)*(i%colCount))+(booklabelSize.Width-bidSize.Width)/2;currentY=currentY+bcSize.Height;g.DrawString(bl.bid,bidFont,Brushes.Black,newPointF(currentX,currentY));}else{SizeFbcidSize=g.MeasureString(bl.bcid,font);SizeFbidSize=g.MeasureString(bl.bid,bidFont);floatcurrentX=left+((booklabelSize.Width+rowspan)*(i%colCount))+(booklabelSize.Width-bcidSize.Width)/2;floatcurrentY=top+((booklabelSize.Height+colspan)*(Convert.ToInt32(i/colCount)%rowCount))+(booklabelSize.Height-bcidSize.Height)/2;g.DrawString(bl.bcid,font,Brushes.Black,newPointF(currentX,currentY));currentX=left+((booklabelSize.Width+rowspan)*(i%colCount))+(booklabelSize.Width-bidSize.Width)/2;currentY=currentY+bcidSize.Height;g.DrawString(bl.bid,bidFont,Brushes.Black,newPointF(currentX,currentY));}}startIndex+=bookPageCouct;if(startIndex<bookCount)//分页{e.HasMorePages=true;}else{e.HasMorePages=false;startIndex=0;bookPageCouct=rowCount*colCount;}g.Dispose();Console.WriteLine("P:"+DateTime.Now.ToLongTimeString());}}}
解决方案
解决方案二:
指定打印页应该通过弹出打印对话框进行设置,然后调整打印代码,跳过不需要打印的页.
解决方案三:
3秒的改成这样看看height获取到要几秒呢,你是不是连得网络打印机?varpageSize=pd.PrinterSettings.DefaultPageSettings.PaperSize;intpWidth=pageSize.Width;//这里耗费3秒Console.WriteLine(DateTime.Now.ToLongTimeString());intPHeight=pageSize.Height;
解决方案四:
引用2楼starfd的回复:
3秒的改成这样看看height获取到要几秒呢,你是不是连得网络打印机?varpageSize=pd.PrinterSettings.DefaultPageSettings.PaperSize;intpWidth=pageSize.Width;//这里耗费3秒Console.WriteLine(DateTime.Now.ToLongTimeString());intPHeight=pageSize.Height;
本地打印机...这和什么打印机没有关系吧?宽和高获取都是用了3秒....点击打印就假死.
解决方案五:
没人知道什么情况?