当前程序文件夹pic下有4张jpg图片文件,单击winform窗体上的"打印"按钮后想把这4张图片通过A4的纸打印出来,每一张图片打印一页,请教代码如何实现

解决方案 »

  1.   

    MARK
      

  2.   

    拉一个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);        }
      

  3.   

    先在界面上拖入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();
      

  4.   

    在printDocument1_PrintPage事件中,参数PrintPageEventArgs e有个HasMorePages属性(获取或设置一个值,该值提示是否打印附加页),通过控制这个属性值来实现多页打印
    private int printIndex=0;  //打印索引,下面调用要用到
    private int pageSize=3; //有三张图片要打印        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                if (printIndex < pageSize)
                {
                    e.Graphics.DrawImage(Image, 0, 0, photoWidth, photoHeight);
                    printIndex++;                //如果打印的页数还不足3页,那么我要继续打印
                    if (printIndex < pageSize)
                    {
                        e.HasMorePages = true;
                    }
                }
                else
                {
                    e.HasMorePages = false;
                }
       }