打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#调用打印机打印图片

可以使用打印控件打印,在Toolbox中有PrintDocument,并且设置相应的事件监听。


双击PringDocument进行监听方法的实现
  1. private void PicturePrintDocument_PrintPage(object sender,  
  2.             System.Drawing.Printing.PrintPageEventArgs e)  
  3.         {  
  4.             try  
  5.             {  
  6.                 if (pictureBox.Image != null)  
  7.                 {  
  8.                     e.Graphics.DrawImage((pictureBox.Image,  
  9.                         e.Graphics.VisibleClipBounds);  
  10.                     e.HasMorePages = false;  
  11.                 }  
  12.             }  
  13.             catch (Exception exception)  
  14.             {  
  15.                 Log...  
  16.             }  
  17.         }  


设置一个button来触发打印,在这个button的click事件中编写代码
  1. private void button_Click(object sender, System.EventArgs e)  
  2.         {  
  3.         PrintDialog printDialog = new PrintDialog();  
  4.         printDialog.Document = picturePrintDocument;  
  5.             if (printDialog.ShowDialog(this) == DialogResult.OK) //到这里会出现选择打印项的窗口  
  6.                 {  
  7.            sectionPicturePrintDocument.Print(); //到这里会出现给文件命名的窗口,点击确定后进行打印并完成打印  
  8.         }  
  9.           }  

如何打印一个Reporting Service报表?
 
  1. LocalReport report = new LocalReport();  
  2.  //...对于report的各种设置,在这里组成一个完整的报表,接下来就是打印报表  
  3. private IList<Stream> m_streams;  
  4.   
  5.   
  6.         private Stream CreateStream(string name,  
  7.           string fileNameExtension, Encoding encoding,  
  8.           string mimeType, bool willSeek)  
  9.         {  
  10.             Stream stream = new FileStream(@"" + name +  
  11.                "." + fileNameExtension, FileMode.Create);  
  12.             m_streams.Add(stream);  
  13.             return stream;  
  14.         }  
  15.   
  16.   
  17.   
  18.   
  19. private void P1()  
  20.         {  
  21.         m_streams = new List<Stream>();  
  22.                 string deviceInfo = "<DeviceInfo>" + "  <OutputFormat>EMF</OutputFormat>" + "  <PageWidth>8.5in</PageWidth>" + "  <PageHeight>11in</PageHeight>" + "  <MarginTop>0.25in</MarginTop>" + "  <MarginLeft>0.25in</MarginLeft>" + "  <MarginRight>0.25in</MarginRight>" + "  <MarginBottom>0.25in</MarginBottom>" + "</DeviceInfo>";  
  23.                 Warning[] warnings;  
  24.   
  25.   
  26.                 report.Render("Image", deviceInfo, CreateStream,   
  27.                    out warnings);  
  28.                 foreach (Stream stream in m_streams)  
  29.                     stream.Position = 0;  
  30.   
  31.   
  32.   
  33.   
  34.                 if (m_streams == null || m_streams.Count == 0)  
  35.                     return;  
  36.                 PrintDocument printDoc = new PrintDocument();  
  37.   
  38.   
  39.                 PrintDialog pdi = new PrintDialog();  
  40.                 pdi.Document = printDoc;  
  41.                 printDoc.PrintPage += new PrintPageEventHandler(PrintPage);  
  42.                 if (pdi.ShowDialog() == DialogResult.OK)  
  43.                 {  
  44.                     printDoc.Print();  
  45.                 }  
  46. }  
  47.   
  48.   
  49. private void PrintPage(object sender, PrintPageEventArgs ev)  
  50.         {  
  51.             Metafile pageImage = new  
  52.                Metafile(m_streams[m_currentPageIndex]);  
  53.             ev.Graphics.DrawImage(pageImage, ev.PageBounds);  
  54.             m_currentPageIndex++;  
  55.             ev.HasMorePages = (m_currentPageIndex < m_streams.Count);  
  56.         }  

这里没有用到控件,所需要的类都是通过new的方式获得的。其实,控件的方式就是让用户更加直接简单的使用工具类,与new的方式没有本质的区别。如果想更加可控的使用这个类并且很熟悉的时候,可以绕过拖动控件直接new.                     
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#之winfrom打印图片
C# 打印详解
C#打印操作——打印设置、打印预览及打
c_对话框大全
全面解析C#实现打印功能
在Excel中实现双面打印
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服