打印图片,自动调整宽高

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;

namespace BIPS.Print
{
    public class PrintDocumentImage
    {

        public event Action<object> EndPrint;

        List<string> listimage;

        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PrintName">打印机名称</param>
        /// <param name="PageSize">打印纸张大小</param>
        /// <param name="color">如果该页应以彩色打印,则为 true;反之,则为 false。默认值由打印机决定。</param>
        /// <param name="img">图片</param>
        /// <param name="Copies">份数</param>
        /// <param name="documentNo">双面打印设置</param>
        public void PrintStrat(string PrintName, string documentNo, string PageSize, bool color, List<string> img, int Copies)
        {
            PrintDocument printdoc = new PrintDocument();//Duplex
            printdoc.DocumentName = documentNo;
            printdoc.PrinterSettings.PrinterName = PrintName;
            printdoc.PrinterSettings.Copies = (short)Copies;
            printdoc.DefaultPageSettings.Color = color;//参数设置
            printdoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

            if (img.Count >= 2)
            {
                //printdoc.PrinterSettings.Duplex = Duplex.Horizontal;
                printdoc.PrinterSettings.Duplex = Duplex.Default;
            }

            foreach (PaperSize ps in printdoc.PrinterSettings.PaperSizes)
            {
                if (ps.Kind.ToString().Equals(PageSize))
                {
                    printdoc.PrinterSettings.DefaultPageSettings.PaperSize = ps;
                    printdoc.DefaultPageSettings.PaperSize = ps;
                }
            }

            this.listimage = img;
            printdoc.PrintPage += new PrintPageEventHandler(printdoc_PrintPage);
            printdoc.EndPrint += new PrintEventHandler(printdoc_EndPrint);
            this.listimage = img;
            printdoc.Print();
        }

        void printdoc_EndPrint(object sender, PrintEventArgs e)
        {
            if (EndPrint != null)
            {
                EndPrint("打印完成");
            }
        }

        private void printdoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            if (listimage.Count > 0)
            {
                //如果是base64字符串则将base64转成图片
                Image inBitmap;
                if (listimage[0].Length > 500)
                {
                    //字符串过程认为是base64string
                    byte[] buffer = System.Convert.FromBase64String(listimage[0]);
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer))
                    {
                        inBitmap = Image.FromStream(ms);
                    }
                }
                else
                {
                    inBitmap = Image.FromFile(listimage[0]);
                }
                using (inBitmap)
                {
                    //打印机绘图宽高
                    int stdBitmapWidth = e.PageBounds.Width;
                    int stdBitmapHeight = e.PageBounds.Height;
                    //如果宽度比高度大,则进行旋转
                    if (inBitmap.Width > inBitmap.Height)
                    {
                        inBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    }
                    //计算 打印机绘图区域宽高比
                    double width_Height = stdBitmapWidth * 1.0 / stdBitmapHeight;
                    //计算 原图宽高比
                    double sWidth_Height = inBitmap.Width * 1.0 / inBitmap.Height;

                    //打印机绘制区域的 区域
                    int heigth = stdBitmapHeight;
                    int width = stdBitmapWidth;
                    int x = 0;
                    int y = 0;
                    //如果图片区域比 打印机绘图区域大则需要缩放
                    if (stdBitmapHeight < inBitmap.Height || stdBitmapWidth < inBitmap.Width)
                    {
                        //如果是高度很高的图片,则按图片比例调整绘制宽度
                        if (width_Height > sWidth_Height)
                        {
                            width = (int)(sWidth_Height * heigth);
                            //调整到左右的中间。
                            x = (stdBitmapWidth - width) / 2;
                        }
                        //如果是宽度比较宽的图片,则按图片比例调整绘制高度
                        else if (width_Height < sWidth_Height)
                        {
                            heigth = (int)(width / sWidth_Height);
                            //t调整到上下的中间。
                            y = (stdBitmapHeight - heigth) / 2;
                        }
                    }
                    //如果图片宽高都比绘图区域小,则不需要进行缩放。直接绘制在打印机绘图区域中央。
                    else
                    {
                        width = inBitmap.Width;
                        heigth = inBitmap.Height;
                        x = (stdBitmapWidth - inBitmap.Width) / 2;
                        y = (stdBitmapHeight - inBitmap.Height) / 2;
                    }
                    //绘制区域
                    Rectangle stdRec = new Rectangle(x, y, width, heigth);
                    //将图片绘制到打印机,绘图区域。
                    e.Graphics.DrawImage(inBitmap, stdRec, new Rectangle(0, 0, inBitmap.Width, inBitmap.Height), GraphicsUnit.Pixel);

                    listimage.RemoveAt(0);
                    if (listimage.Count > 0)
                    {
                        e.HasMorePages = true;
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                }

            }
        }

    }

}

 

posted @ 2018-10-16 15:21  HandsomeBoy  阅读(783)  评论(0编辑  收藏  举报