C#调用打印机打印图片

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


双击PringDocument进行监听方法的实现
private void PicturePrintDocument_PrintPage(object sender,
            System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                if (pictureBox.Image != null)
                {
                    e.Graphics.DrawImage((pictureBox.Image,
                        e.Graphics.VisibleClipBounds);
                    e.HasMorePages = false;
                }
            }
            catch (Exception exception)
            {
                Log...
            }
        }


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

如何打印一个Reporting Service报表?
 
LocalReport report = new LocalReport();
 //...对于report的各种设置,在这里组成一个完整的报表,接下来就是打印报表
private IList<Stream> m_streams;


        private Stream CreateStream(string name,
          string fileNameExtension, Encoding encoding,
          string mimeType, bool willSeek)
        {
            Stream stream = new FileStream(@"" + name +
               "." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);
            return stream;
        }




private void P1()
        {
		m_streams = new List<Stream>();
                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>";
                Warning[] warnings;


                report.Render("Image", deviceInfo, CreateStream, 
                   out warnings);
                foreach (Stream stream in m_streams)
                    stream.Position = 0;




                if (m_streams == null || m_streams.Count == 0)
                    return;
                PrintDocument printDoc = new PrintDocument();


                PrintDialog pdi = new PrintDialog();
                pdi.Document = printDoc;
                printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                if (pdi.ShowDialog() == DialogResult.OK)
                {
                    printDoc.Print();
                }
}


private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }

这里没有用到控件,所需要的类都是通过new的方式获得的。其实,控件的方式就是让用户更加直接简单的使用工具类,与new的方式没有本质的区别。如果想更加可控的使用这个类并且很熟悉的时候,可以绕过拖动控件直接new.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值