c#打印图片文件问题
当前程序文件夹pic下有4张jpg图片文件,单击winform窗体上的"打印"按钮后想把这4张图片通过A4的纸打印出来,每一张图片打印一页,请教代码如何实现
[解决办法]
- C# code
拉一个printDocument控件到界面。 打印按钮的代码: C# codeCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/ private void button2_Click(object sender, EventArgs e)//执行打印 { PrintDialog MyPrintDg = new PrintDialog(); MyPrintDg.Document = printDocument1; if (MyPrintDg.ShowDialog() == DialogResult.OK) { try { printDocument1.Print(); } catch { //停止打印 printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs()); } } }另外还要设置PrintPage事件:C# codeCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/ private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImage(pictureBox1.Image, 20, 20); }
[解决办法]
先在界面上拖入printDocument控件,然后打印事件中写:
byte[] input = new Byte[100000];
//读取图片
FileStream fs = new FileStream(@"图片路径", FileMode.Open, FileAccess.Read);
fs.Read(input, 0, 100000);
//设置打印页面
printDocument1.DefaultPageSettings.Landscape = true;
//向打印页绘制图片
Image image = Image.FromStream(fs);
e.Graphics.DrawImage(image, 20, 20);
//打印
printDocument1.Print();