可以使用打印控件打印,在Toolbox中有PrintDocument,并且设置相应的事件监听。
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... } }
private void button_Click(object sender, System.EventArgs e) { PrintDialog printDialog = new PrintDialog(); printDialog.Document = picturePrintDocument; if (printDialog.ShowDialog(this) == DialogResult.OK) //到这里会出现选择打印项的窗口 { sectionPicturePrintDocument.Print(); //到这里会出现给文件命名的窗口,点击确定后进行打印并完成打印 } }
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); }