您的位置:首页 > 编程语言 > C#

C#调用打印机打印图片

2014-03-11 16:43 555 查看
可以使用打印控件打印,在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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: