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

C#调用打印机,打印图片

2013-11-07 11:11 351 查看
PrintPreviewDialog是打印预览对话框,需要传一个 printDocument给它才可以显示该对话框

PrintDocument是具体要打印的内容,可以是图片,也可以是加载文档,文字。

代码如下:

//打印
private void button5_Click(object sender, EventArgs e)
{
//获取或设置一个值,该值指示是否发送到文件或端口
printDocument1.PrinterSettings.PrintToFile = true;
//设置打印时横向还是纵向
printDocument1.DefaultPageSettings.Landscape = true;
//打印预览
// PrintPreviewDialog ppd = new PrintPreviewDialog();
if (bmp == null)
{
return;
}

//设置边距
Margins margin = new Margins(20, 20, 20, 20);
printDocument1.DefaultPageSettings.Margins = margin;
////纸张设置默认
//PaperSize pageSize = new PaperSize("First custom size", 800, 600);
//pd.DefaultPageSettings.PaperSize = pageSize;
//打印事件设置
printDocument1.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
//ppd.Document = printDocument1;
// ppd.ShowDialog();
try
{
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument1.PrintController.OnEndPrint(printDocument1, new PrintEventArgs());
}

}

//打印事件处理
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = bmp.Width;
int height = bmp.Height;
Rectangle destRect = new Rectangle(x, y, width, height);
e.Graphics.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: