Пример #1
0
        private TImgExportInfo GetExportInfo(IFlxGraphics aCanvas)
        {
            TImgExportInfo Result = new TImgExportInfo();

            if (AllVisibleSheets)
            {
                int FirstVisibleSheet = -1;
                int SaveActiveSheet   = Workbook.ActiveSheet;
                try
                {
                    Result.Sheets = new TOneImgExportInfo[Workbook.SheetCount];
                    for (int sheet = 1; sheet <= Workbook.SheetCount; sheet++)
                    {
                        Workbook.ActiveSheet = sheet;
                        if (Workbook.SheetVisible != TXlsSheetVisible.Visible)
                        {
                            continue;
                        }
                        if (FirstVisibleSheet < 0)
                        {
                            FirstVisibleSheet = sheet;
                        }

                        TOneImgExportInfo OneResult = new TOneImgExportInfo();

                        OneResult.FPrintRanges = FRenderer.InternalCalcPrintArea(FPrintRange);
                        TPaperDimensions pd = GetRealPageSize();
                        OneResult.FPageBounds = new RectangleF(0, 0, pd.Width, pd.Height);

                        FRenderer.InitializePrint(aCanvas, OneResult.PageBounds, OneResult.PageBounds, OneResult.PrintRanges, out OneResult.FPaintClipRect, out OneResult.FTotalPages, out OneResult.FPagePrintRange);
                        Result.Sheets[sheet - 1] = OneResult;
                    }
                }
                finally
                {
                    Workbook.ActiveSheet = SaveActiveSheet;
                }

                Result.CurrentSheet = FirstVisibleSheet;
            }
            else
            {
                TOneImgExportInfo OneResult = new TOneImgExportInfo();
                OneResult.FPrintRanges = FRenderer.InternalCalcPrintArea(FPrintRange);
                TPaperDimensions pd = GetRealPageSize();
                OneResult.FPageBounds = new RectangleF(0, 0, pd.Width, pd.Height);

                FRenderer.InitializePrint(aCanvas, OneResult.PageBounds, OneResult.PageBounds, OneResult.PrintRanges, out OneResult.FPaintClipRect, out OneResult.FTotalPages, out OneResult.FPagePrintRange);
                Result.Sheets       = new TOneImgExportInfo[1];
                Result.Sheets[0]    = OneResult;
                Result.CurrentSheet = Workbook.ActiveSheet;
            }
            Result.ResetCurrentPage();
            LastInitSheet = 0;
            return(Result);
        }
Пример #2
0
        /// <summary>
        /// Return the pages to print. This is a costly operation, so cache the results.
        /// </summary>
        /// <returns></returns>
        public int TotalPages()
        {
            TImgExportInfo ei = null;

            if (!ExportNext(null, ref ei))
            {
                return(0);
            }
            return(ei.TotalPages);
        }
Пример #3
0
        private Bitmap CreateBitmap(float Resolution, ref TImgExportInfo ExportInfo, PixelFormat PxFormat)
        {
            if (ExportInfo == null)
            {
                ExportInfo = GetFirstPageExportInfo();
            }

            TPaperDimensions pd     = GetRealPageSize(ExportInfo.NextSheet);
            Bitmap           Result =
                BitmapConstructor.CreateBitmap((int)Math.Ceiling(pd.Width / 96F * Resolution),
                                               (int)Math.Ceiling(pd.Height / 96F * Resolution), PxFormat);

            Result.SetResolution(Resolution, Resolution);
            return(Result);
        }
Пример #4
0
        private void CreateImg(Stream OutStream, ImageFormat ImgFormat, ImageColorDepth Colors)
        {
            TImgExportInfo ExportInfo = null;

            PixelFormat RgbPixFormat = Colors != ImageColorDepth.TrueColor ? PixelFormat.Format32bppPArgb : PixelFormat.Format24bppRgb;
            PixelFormat PixFormat    = PixelFormat.Format1bppIndexed;

            switch (Colors)
            {
            case ImageColorDepth.TrueColor: PixFormat = RgbPixFormat; break;

            case ImageColorDepth.Color256: PixFormat = PixelFormat.Format8bppIndexed; break;
            }

            using (Bitmap OutImg = CreateBitmap(Resolution, ref ExportInfo, PixFormat))
            {
                Bitmap ActualOutImg = Colors != ImageColorDepth.TrueColor? CreateBitmap(Resolution, ref ExportInfo, RgbPixFormat): OutImg;
                try
                {
                    using (Graphics Gr = Graphics.FromImage(ActualOutImg))
                    {
                        Gr.FillRectangle(Brushes.White, 0, 0, ActualOutImg.Width, ActualOutImg.Height); //Clear the background
                        ExportNext(Gr, ref ExportInfo);
                    }

                    if (Colors == ImageColorDepth.BlackAndWhite)
                    {
                        FloydSteinbergDither.ConvertToBlackAndWhite(ActualOutImg, OutImg);
                    }
                    else
                    if (Colors == ImageColorDepth.Color256)
                    {
                        OctreeQuantizer.ConvertTo256Colors(ActualOutImg, OutImg);
                    }
                }
                finally
                {
                    if (ActualOutImg != OutImg)
                    {
                        ActualOutImg.Dispose();
                    }
                }

                OutImg.Save(OutStream, ImgFormat);
            }
        }
Пример #5
0
        /// <summary>
        /// Returns a deep copy of a TImgExportInfo. This method will work even if the source object is null.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TImgExportInfo Clone(TImgExportInfo source)
        {
            if (source == null)
            {
                return(null);
            }
            TImgExportInfo Result = (TImgExportInfo)source.MemberwiseClone();

            if (source.FSheets != null)
            {
                Result.FSheets = new TOneImgExportInfo[source.FSheets.Length];
                for (int i = 0; i < source.FSheets.Length; i++)
                {
                    Result.FSheets[i] = TOneImgExportInfo.Clone(source.FSheets[i]);
                }
            }
            return(Result);
        }
Пример #6
0
        private TImgExportInfo ExportAllImagesButFirst(EncoderParameters ep, bool Is1bpp, TImgExportInfo ExportInfo, PixelFormat RgbPixFormat, Bitmap OutImg, ImageColorDepth ColorDepth)
        {
            if (ExportInfo == null)
            {
                ExportInfo = GetFirstPageExportInfo();
            }
            for (int i = ExportInfo.CurrentPage; i < ExportInfo.TotalPages; i++)
            {
                using (Bitmap TmpImg = CreateBitmap(Resolution, ref ExportInfo, RgbPixFormat))
                {
                    using (Graphics Gr = Graphics.FromImage(TmpImg))
                    {
                        Gr.FillRectangle(Brushes.White, 0, 0, TmpImg.Width, TmpImg.Height); //Clear the background
                        ExportNext(Gr, ref ExportInfo);

                        if (Is1bpp)
                        {
                            using (Bitmap BwImg = FloydSteinbergDither.ConvertToBlackAndWhite(TmpImg))
                            {
                                OutImg.SaveAdd(BwImg, ep);
                            }
                        }
                        else
                        if (ColorDepth == ImageColorDepth.Color256)
                        {
                            using (Bitmap IndexImg = OctreeQuantizer.ConvertTo256Colors(TmpImg))
                            {
                                OutImg.SaveAdd(IndexImg, ep);
                            }
                        }
                        else
                        {
                            OutImg.SaveAdd(TmpImg, ep);
                        }
                    }
                }
            }
            return(ExportInfo);
        }
Пример #7
0
        private void CreateMultiPageTiff(Stream OutStream, ImageColorDepth ColorDepth, ImageExportType ExportType)
        {
            ImageCodecInfo info = GetTiffEncoder();

            int ParamCount = 1;

            if (ExportType == ImageExportType.Fax || ExportType == ImageExportType.Fax4)
            {
                ParamCount++;
            }

            EncoderParameters ep = new EncoderParameters(ParamCount);

            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);

            bool IsFax = false;

            switch (ExportType)
            {
            case ImageExportType.Fax:
                ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT3);
                IsFax       = true;
                break;

            case ImageExportType.Fax4:
                ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                IsFax       = true;
                break;
            }

            bool Is1bpp = IsFax || ColorDepth == ImageColorDepth.BlackAndWhite;

            TImgExportInfo ExportInfo = null;

            PixelFormat RgbPixFormat = IsFax || ColorDepth != ImageColorDepth.TrueColor ? PixelFormat.Format32bppPArgb : PixelFormat.Format24bppRgb;
            PixelFormat PixFormat    = PixelFormat.Format1bppIndexed;

            if (!IsFax)
            {
                switch (ColorDepth)
                {
                case ImageColorDepth.TrueColor: PixFormat = RgbPixFormat; break;

                case ImageColorDepth.Color256: PixFormat = PixelFormat.Format8bppIndexed; break;
                }
            }

            using (Bitmap OutImg = CreateBitmap(Resolution, ref ExportInfo, PixFormat))
            {
                //First image is handled differently.
                Bitmap ActualOutImg = Is1bpp || ColorDepth != ImageColorDepth.TrueColor ? CreateBitmap(Resolution, ref ExportInfo, RgbPixFormat) : OutImg;
                try
                {
                    using (Graphics Gr = Graphics.FromImage(ActualOutImg))
                    {
                        Gr.FillRectangle(Brushes.White, 0, 0, ActualOutImg.Width, ActualOutImg.Height); //Clear the background
                        ExportNext(Gr, ref ExportInfo);
                    }

                    if (Is1bpp)
                    {
                        FloydSteinbergDither.ConvertToBlackAndWhite(ActualOutImg, OutImg);
                    }
                    else
                    if (!IsFax && ColorDepth == ImageColorDepth.Color256)
                    {
                        OctreeQuantizer.ConvertTo256Colors(ActualOutImg, OutImg);
                    }
                }
                finally
                {
                    if (ActualOutImg != OutImg)
                    {
                        ActualOutImg.Dispose();
                    }
                }

                OutImg.Save(OutStream, info, ep);
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);


                //Now the rest of images.
                ExportInfo = ExportAllImagesButFirst(ep, Is1bpp, ExportInfo, RgbPixFormat, OutImg, ColorDepth);

                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                OutImg.SaveAdd(ep);
            }
        }
Пример #8
0
        /// <summary>
        /// Exports the associated xls workbook to a file.
        /// </summary>
        /// <param name="fileName">File to export.</param>
        /// <param name="format">Format for the created image.</param>
        /// <param name="imgFormat">Format for the saved image</param>
        /// <param name="exportInfo"> Information needed to export, cached for speed. The first time you call this method (or when you change xls.ActiveSheet), make exportInfo=null</param>
        public bool ExportNext(string fileName, PixelFormat format, ImageFormat imgFormat, ref TImgExportInfo exportInfo)
        {
            bool Result = false;

            if (exportInfo != null && exportInfo.CurrentPage >= exportInfo.TotalPages)
            {
                return(false);
            }
            //if (Workbook.RowCount<=0) return false; A sheet might have only images.
            try
            {
                FileMode fm = FileMode.CreateNew;
                if (AllowOverwritingFiles)
                {
                    fm = FileMode.Create;
                }
                using (FileStream f = new FileStream(fileName, fm, FileAccess.Write))
                {
                    Result = ExportNext(f, format, imgFormat, ref exportInfo);
                }
            }
            catch (IOException)
            {
                //Don't delete the file in an io exception. It might be because allowoverwritefiles was false, and the file existed.
                throw;
            }
            catch
            {
                File.Delete(fileName);
                throw;
            }

            return(Result);
        }
Пример #9
0
        /// <summary>
        /// Exports the associated xls workbook to a stream.
        /// </summary>
        /// <param name="imgStream">Stream where the image will be exported.</param>
        /// <param name="format">Pixel depth for the created image.</param>
        /// <param name="imgFormat">Format for the saved image</param>
        /// <param name="exportInfo"> Information needed to export, cached for speed. The first time you call this method (or when you change xls.ActiveSheet), make exportInfo=null</param>
        public bool ExportNext(Stream imgStream, PixelFormat format, ImageFormat imgFormat, ref TImgExportInfo exportInfo)
        {
            bool Result = false;

            using (Bitmap bmp = CreateBitmap(Resolution, ref exportInfo, format))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    Result = ExportNext(g, ref exportInfo);
                }
                if (Result)
                {
                    bmp.Save(imgStream, imgFormat);
                }
            }
            return(Result);
        }
Пример #10
0
        /// <summary>
        /// Exports the associated xls workbook to a graphics stream. You need to provide a
        /// Graphics object with the correct dimensions. (To get the needed dimensions, use <see cref="GetRealPageSize()"/>
        /// </summary>
        /// <param name="imgData">Graphics where the image will be stored. Set it to null to skip the page.</param>
        /// <param name="exportInfo"> Information needed to export, cached for speed. The first time you call this method (or when you change xls.ActiveSheet), make exportInfo=null</param>
        public bool ExportNext(Graphics imgData, ref TImgExportInfo exportInfo)
        {
            FRenderer.CreateFontCache();
            try
            {
                Bitmap bmp = null;

                try
                {
                    if (imgData == null)
                    {
                        bmp              = BitmapConstructor.CreateBitmap(1, 1);
                        imgData          = Graphics.FromImage(bmp);
                        imgData.PageUnit = GraphicsUnit.Point;
                    }
                    IFlxGraphics aCanvas = new GdiPlusGraphics(imgData);

                    GraphicsUnit OriginalUnits = imgData.PageUnit;
                    try
                    {
                        imgData.PageUnit = GraphicsUnit.Point;
                        FRenderer.SetCanvas(aCanvas);
                        try
                        {
                            if (exportInfo == null)
                            {
                                exportInfo = GetExportInfo(aCanvas);
                            }

                            exportInfo.IncCurrentPage();
                            if (exportInfo.CurrentPage > exportInfo.TotalPages)
                            {
                                return(false);
                            }

                            int SaveActiveSheet = Workbook.ActiveSheet;
                            try
                            {
                                Workbook.ActiveSheet = exportInfo.CurrentSheet;
                                int CurrentLogicalPage = -1;
                                if (ResetPageNumberOnEachSheet)
                                {
                                    CurrentLogicalPage = exportInfo.ActiveSheet.FCurrentPage;
                                }
                                else
                                {
                                    CurrentLogicalPage = exportInfo.CurrentPage;
                                }


                                TOneImgExportInfo OneResult = exportInfo.ActiveSheet;
                                if (LastInitSheet != exportInfo.CurrentSheet)
                                {
                                    TXlsCellRange ra; int p; RectangleF[] r;
                                    FRenderer.InitializePrint(aCanvas, OneResult.PageBounds, OneResult.PageBounds, OneResult.PrintRanges, out r, out p, out ra);
                                    LastInitSheet = exportInfo.CurrentSheet;
                                }

                                if (bmp == null)
                                {
                                    OnBeforePaint(new ImgPaintEventArgs(imgData, CalcPageBounds(exportInfo.ActiveSheet.PageBounds), exportInfo.CurrentPage, exportInfo.ActiveSheet.CurrentPage, exportInfo.TotalPages));
                                }

                                FRenderer.GenericPrint(aCanvas, OneResult.PageBounds, OneResult.PrintRanges, CurrentLogicalPage,
                                                       OneResult.PaintClipRect, exportInfo.TotalLogicalPages(ResetPageNumberOnEachSheet), bmp == null,
                                                       OneResult.PagePrintRange, ref OneResult.FCurrentPrintArea);

                                aCanvas.ResetClip();

                                if (bmp == null)
                                {
                                    OnAfterPaint(new ImgPaintEventArgs(imgData, CalcPageBounds(exportInfo.ActiveSheet.PageBounds), exportInfo.CurrentPage, exportInfo.ActiveSheet.CurrentPage, exportInfo.TotalPages));
                                }
                            }
                            finally
                            {
                                Workbook.ActiveSheet = SaveActiveSheet;
                            }
                        }
                        finally
                        {
                            FRenderer.SetCanvas(null);
                        }
                    }
                    finally
                    {
                        imgData.PageUnit = OriginalUnits;
                    }
                }
                finally
                {
                    if (bmp != null)
                    {
                        bmp.Dispose();
                        imgData.Dispose();
                    }
                }
            }
            finally
            {
                FRenderer.DisposeFontCache();
            }
            return(true);
        }