winfrom 打印图片,打印A4纸大小,自适应

           private int printNum = 0;//多页打印
    private string imageFile = "";//单个图片文件
    private ArrayList fileList = new ArrayList();//多个图片文件
    private void button2_Click_1(object sender, EventArgs e)
    {
        PrintPreview();
    }



    public void PrintPreview()
    {
        PrintDocument docToPrint = new PrintDocument();
        docToPrint.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_BeginPrint);
        docToPrint.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_EndPrint);
        docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.docToPrint_PrintPage);
        docToPrint.DefaultPageSettings.Landscape = false;

        PrintDialog printDialog = new PrintDialog();
        printDialog.AllowSomePages = true;
        printDialog.ShowHelp = true;
        printDialog.Document = docToPrint;
        if (printDialog.ShowDialog() == DialogResult.OK)
        {

            docToPrint.Print();
        }
    }
    private void docToPrint_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        if (fileList.Count == 0)
            fileList.Add(imageFile);
    }
    private void docToPrint_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {

    }
    private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        FileStream fsa = new FileStream(fileList[printNum].ToString().Trim(), FileMode.Open);
        byte[] bytes = new byte[fsa.Length];
        fsa.Read(bytes, 0, bytes.Length);
        fsa.Close();
        MemoryStream ms = new MemoryStream(bytes);
        Bitmap image = new Bitmap(ms);
        //pictureBox1.Image = image;

        //图片抗锯齿
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Stream fs = new FileStream(fileList[printNum].ToString().Trim(), FileMode.Open, FileAccess.Read);
        //System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
        int x = e.MarginBounds.X;
        int y = e.MarginBounds.Y;
        int width = image.Width;
        int height = image.Height;
        if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
        {
            width = e.MarginBounds.Width;
            height = image.Height * e.MarginBounds.Width / image.Width;
        }
        else
        {
            height = e.MarginBounds.Height;
            width = image.Width * e.MarginBounds.Height / image.Height;
        }

        //DrawImage参数根据打印机和图片大小自行调整
        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
        //System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height);
        if (image.Height < 310)
        {
            e.Graphics.DrawImage(image, 0, 30, image.Width + 20, image.Height);
            //    System.Drawing.Rectangle destRect1 = new System.Drawing.Rectangle(0, 30, image.Width, image.Height);
            //    e.Graphics.DrawImage(image, destRect1, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
        }
        else
        {
            //  e.Graphics.DrawImage(image, 0, 0, 800, 1123);
            //   System.Drawing.Rectangle destRect2 = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);
            System.Drawing.Rectangle destRect2 = new System.Drawing.Rectangle(x, y, width, height);
            e.Graphics.DrawImage(image, destRect2, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
        }

        if (printNum < fileList.Count - 1)
        {
            printNum++;
            e.HasMorePages = true;//HasMorePages为true则再次运行PrintPage事件
            return;
        }
        printNum = 0;
        e.HasMorePages = false;
        image.Dispose();
        fs.Dispose();
        fs.Close();
       




    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值