Exemplo n.º 1
0
        /// <summary>
        /// 打印过程处理
        /// </summary>
        private void PrintPage(object sender, PrintPageEventArgs args)
        {
            //没有数据,直接返回
            if (listImagePath == null || listImagePath.Count == 0)
            {
                args.HasMorePages = false;
                return;
            }

            //处理当前页面数据
            Image pageImage = null;

            try
            {
                //获得需要打印的数据
                string fileName = listImagePath[indexPrinting];
                pageImage = ImageHelper.LoaderImage(fileName);

                //获得打印位置信息
                GraphicsUnit unit          = GraphicsUnit.Millimeter;
                RectangleF   rectImage     = pageImage.GetBounds(ref unit);
                RectangleF   rectContainer = args.PageBounds;
                RectangleF   rectDraw      = rectContainer; //初始值

                //根据是否缩放设置,获得对应的绘制区域
                if (saver.KeepRatio)
                {
                    rectDraw = ImageHelper.GetKeepRadioRect(rectImage, rectContainer,
                                                            saver.MarginsCustom.Left, saver.MarginsCustom.Right,
                                                            saver.MarginsCustom.Top, saver.MarginsCustom.Bottom);
                }
                else
                {
                    rectDraw = ImageHelper.GetContainerMarginRect(rectContainer,
                                                                  saver.MarginsCustom.Left, saver.MarginsCustom.Right,
                                                                  saver.MarginsCustom.Top, saver.MarginsCustom.Bottom);
                }

                //绘制
                args.Graphics.DrawImage(pageImage, rectDraw);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                //调用 ImageHelper.LoaderImage(fileName) 后要求使用完立刻释放图片资源
                if (null != pageImage)
                {
                    pageImage.Dispose();
                }
            }

            //分析是否有下一页
            ++indexPrinting;
            if (indexPrinting == listImagePath.Count)
            {
                args.HasMorePages = false;
                indexPrinting     = 0;
            }
            else
            {
                args.HasMorePages = true;
            }
        }