Example #1
0
        /// <summary>
        /// Returns information needed for exporting multiple pages on one sheet. You normally
        /// don't need to use this method, but you can use it to speed up multiple displays.
        /// </summary>
        /// <returns></returns>
        public TImgExportInfo GetFirstPageExportInfo()
        {
            FRenderer.CreateFontCache();
            try
            {
                using (Bitmap bmp = BitmapConstructor.CreateBitmap(1, 1))
                {
                    using (Graphics imgData = Graphics.FromImage(bmp))
                    {
                        IFlxGraphics aCanvas = new GdiPlusGraphics(imgData);

                        imgData.PageUnit = GraphicsUnit.Point;
                        FRenderer.SetCanvas(aCanvas);
                        try
                        {
                            return(GetExportInfo(aCanvas));
                        }
                        finally
                        {
                            FRenderer.SetCanvas(null);
                        }
                    }
                }
            }
            finally
            {
                FRenderer.DisposeFontCache();
            }
        }
Example #2
0
        public static Bitmap ConvertTo256Colors(Image Source)
        {
            Bitmap Result = BitmapConstructor.CreateBitmap(Source.Width, Source.Height, PixelFormat.Format8bppIndexed);

            Result.SetResolution(Source.HorizontalResolution, Source.VerticalResolution);
            ConvertTo256Colors(Source, Result);
            return(Result);
        }
Example #3
0
        public static Bitmap ConvertToBlackAndWhite(Bitmap Source)
        {
            Bitmap Result = BitmapConstructor.CreateBitmap(Source.Width, Source.Height, PixelFormat.Format1bppIndexed);

            Result.SetResolution(Source.HorizontalResolution, Source.VerticalResolution);
            ConvertToBlackAndWhite(Source, Result);
            return(Result);
        }
Example #4
0
 internal TGraphicCanvas()
 {
     FontCache        = new TFontCache();
     bmp              = BitmapConstructor.CreateBitmap(1, 1);
     imgData          = Graphics.FromImage(bmp);
     imgData.PageUnit = GraphicsUnit.Point;
     Canvas           = new GdiPlusGraphics(imgData);
     Canvas.CreateSFormat();
 }
Example #5
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);
        }
Example #6
0
        internal Bitmap GetImage(int index)
        {
            TImgExportInfo ei = null;

            if (Count > 0)
            {
                ei = TImgExportInfo.Clone(this[Count - 1].ExportInfo);
            }
            else
            {
                ei = TImgExportInfo.Clone(Owner.FirstPageExportInfo);
            }

            for (int i = Count; i <= index; i++)
            {
                GetNextImage(ref ei, null);
                Add(new TPageInfo(TImgExportInfo.Clone(ei), null));
            }

            TPageInfo Pi = this[index];

            if (Pi.Bmp == null)
            {
                RectangleF Page = Pi.ExportInfo.ActiveSheet.PageBounds;
                Pi.Bmp = BitmapConstructor.CreateBitmap((int)(Page.Width * Owner.Zoom), (int)(Page.Height * Owner.Zoom));
                Pi.Bmp.SetResolution(96 * Owner.Zoom, 96 * Owner.Zoom);
                TImgExportInfo ExportInfo = null;
                if (index > 0)
                {
                    ExportInfo = TImgExportInfo.Clone(this[index - 1].ExportInfo);
                }
                else
                {
                    ExportInfo = TImgExportInfo.Clone(Owner.FirstPageExportInfo);
                }

                GetNextImage(ref ExportInfo, Pi.Bmp);
                CacheCount++;
            }

            //Bring the item to last position on the cache.
            if (Pi.Next != null)
            {
                Pi.Next.Prev = Pi.Prev;
                if (Pi.Prev != null)
                {
                    Pi.Prev.Next = Pi.Next;
                }
                else
                {
                    CacheFirst = Pi.Next;
                }
            }
            if (CacheFirst == null)
            {
                CacheFirst = Pi;
            }

            if (CacheLast != Pi)
            {
                Pi.Prev = CacheLast;
                if (CacheLast != null)
                {
                    CacheLast.Next = Pi;
                }
                CacheLast = Pi;
                Pi.Next   = null;
            }

            //If we have more bitmaps on the cache that what is allowed, delete the one at the first position.
            if (CacheFirst != null && CacheCount > 1 + Owner.CacheSize / (Owner.Zoom * Owner.Zoom))
            {
                if (CacheFirst.Next != null)
                {
                    CacheFirst.Bmp.Dispose();
                    CacheFirst.Bmp = null;
                    CacheCount--;

                    TPageInfo Next = CacheFirst.Next;
                    Next.Prev       = null;
                    CacheFirst.Next = null;
                    CacheFirst      = Next;
                }
            }

            Debug.Assert(CacheFirst == null || CacheFirst.Prev == null, "Error in cache");
            Debug.Assert(CacheLast == null || CacheLast.Next == null, "Error in cache");
            return(Pi.Bmp);
        }
Example #7
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);
        }
Example #8
0
        public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, long transparentColor, int brightness, int contrast, int gamma, Color shadowColor, Stream imgData)
        {
            bool ChangedParams = brightness != FlxConsts.DefaultBrightness ||
                                 contrast != FlxConsts.DefaultContrast ||
                                 gamma != FlxConsts.DefaultGamma ||
                                 shadowColor != ColorUtil.Empty;

            ChangedParams = ChangedParams || (!image.RawFormat.Equals(ImageFormat.Jpeg) && transparentColor != FlxConsts.NoTransparentColor);
            if (!ChangedParams && srcRect.Top == 0 && srcRect.Left == 0 && srcRect.Width == image.Width && srcRect.Height == image.Height)
            {
                Canvas.DrawImage(image, destRect, imgData, transparentColor, image.RawFormat.Equals(ImageFormat.Jpeg));
            }     //Optimizes the most common case.
            else
            {
                using (Bitmap bm = BitmapConstructor.CreateBitmap(Convert.ToInt32(srcRect.Width), Convert.ToInt32(srcRect.Height), image.PixelFormat))
                {
                    bm.MakeTransparent();
                    ImageAttributes imgAtt = null;
                    try
                    {
                        if (transparentColor != FlxConsts.NoTransparentColor)
                        {
                            long  cl  = transparentColor;
                            Color tcl = ColorUtil.FromArgb((int)(cl & 0xFF), (int)((cl & 0xFF00) >> 8), (int)((cl & 0xFF0000) >> 16));
                            imgAtt = new ImageAttributes();
                            imgAtt.SetColorKey(tcl, tcl);
                        }

                        if (gamma != FlxConsts.DefaultGamma)
                        {
                            if (imgAtt == null)
                            {
                                imgAtt = new ImageAttributes();
                            }
                            imgAtt.SetGamma((real)((UInt32)gamma) / 65536f);
                        }

                        using (Graphics Gr = Graphics.FromImage(bm))
                        {
                            Rectangle r2 = new Rectangle(Convert.ToInt32(srcRect.Left), Convert.ToInt32(srcRect.Top), Convert.ToInt32(srcRect.Width), Convert.ToInt32(srcRect.Height));
                            int       l  = Math.Max(0, -r2.Left);
                            int       t  = Math.Max(0, -r2.Top);
                            Rectangle r  = new Rectangle(l, t, Math.Min(Convert.ToInt32(image.Width), r2.Width + l), Math.Min(Convert.ToInt32(image.Height), r2.Height + t));

                            if (ChangedParams)
                            {
                                if (shadowColor != ColorUtil.Empty)
                                {
                                    FlgConsts.MakeImageGray(ref imgAtt, shadowColor);
                                }
                                else
                                {
                                    FlgConsts.AdjustImage(ref imgAtt, brightness, contrast);
                                }
                            }

                            Image FinalImage = image;
                            try
                            {
                                if (image.RawFormat.Equals(ImageFormat.Wmf) || image.RawFormat.Equals(ImageFormat.Emf))
                                {
                                    FinalImage = FlgConsts.RasterizeWMF(image);                                     //metafiles do not like cropping or changing attributes.
                                }


                                Gr.DrawImage(FinalImage, r,
                                             Math.Max(0, r2.Left), Math.Max(0, r2.Top), Math.Min(Convert.ToInt32(image.Width), r2.Width + l), Math.Min(Convert.ToInt32(image.Height), r2.Height + t), GraphicsUnit.Pixel, imgAtt);
                            }
                            finally
                            {
                                if (FinalImage != image)
                                {
                                    FinalImage.Dispose();
                                }
                            }


                            bool DefaultToJpeg = image.RawFormat.Equals(ImageFormat.Jpeg);
                            Canvas.DrawImage(bm, destRect, null, FlxConsts.NoTransparentColor, DefaultToJpeg);
                        }
                    }
                    finally
                    {
                        if (imgAtt != null)
                        {
                            imgAtt.Dispose();
                        }
                    }
                }
            }
        }