Exemplo n.º 1
0
        public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, long transparentColor, int brightness, int contrast, int gamma, Color shadowColor, Stream imgData)
        {
            if (PointOutside.Check(ref srcRect))
            {
                return;
            }
            if (PointOutside.Check(ref destRect))
            {
                return;
            }
            if (image.Height <= 0 || image.Width <= 0)
            {
                return;
            }
            bool ChangedParams = brightness != FlxConsts.DefaultBrightness ||
                                 contrast != FlxConsts.DefaultContrast ||
                                 gamma != FlxConsts.DefaultGamma ||
                                 shadowColor != Colors.Transparent;

            ImageAttributes imgAtt = null;

            try
            {
                if (transparentColor != FlxConsts.NoTransparentColor)
                {
                    long  cl  = transparentColor;
                    Color tcl = ColorUtil.FromArgb(0xFF, (byte)(cl & 0xFF), (byte)((cl & 0xFF00) >> 8), (byte)((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);
                }

                if (!ChangedParams && srcRect.Top == 0 && srcRect.Left == 0 && srcRect.Width == image.Width && srcRect.Height == image.Height && imgAtt == null)
                {
                    FCanvas.DrawImage(image, destRect);
                }
                else
                {
                    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.
                        }

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

                        PointF[] ImageRect = new PointF[] { new PointF(destRect.Left, destRect.Top), new PointF(destRect.Right, destRect.Top), new PointF(destRect.Left, destRect.Bottom) };
                        FCanvas.DrawImage(FinalImage,
                                          ImageRect,
                                          srcRect, GraphicsUnit.Pixel, imgAtt);
                    }
                    finally
                    {
                        if (FinalImage != image)
                        {
                            FinalImage.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (imgAtt != null)
                {
                    imgAtt.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, long transparentColor, int brightness, int contrast, int gamma, Color shadowColor, Stream imgData)
        {
            if (PointOutside.Check(ref srcRect))
            {
                return;
            }
            if (PointOutside.Check(ref destRect))
            {
                return;
            }
            if (image.Height <= 0 || image.Width <= 0)
            {
                return;
            }
            bool ChangedParams = brightness != FlxConsts.DefaultBrightness ||
                                 contrast != FlxConsts.DefaultContrast ||
                                 gamma != FlxConsts.DefaultGamma ||
                                 shadowColor != ColorUtil.Empty;

            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((float)((UInt32)gamma) / 65536f);
                }

                if (!ChangedParams && srcRect.Top == 0 && srcRect.Left == 0 && srcRect.Width == image.Width && srcRect.Height == image.Height && imgAtt == null)
                {
                    bool Retry = false;
                    try
                    {
                        Canvas.DrawImage(image, destRect);                          //Optimizes the most common case, but there is also an error when cropping metafiles. At least we make sure here this won't have any error on 99% of the cases.
                    }
                    catch (System.Runtime.InteropServices.ExternalException ex)
                    {
                        if (FlexCelTrace.HasListeners)
                        {
                            FlexCelTrace.Write(new TRenderMetafileError(ex.Message));
                        }

                        Retry = true;                         //metafiles can raise nasty errors here.
                    }
                    if (Retry)
                    {
                        using (Image bmp = FlgConsts.RasterizeWMF(image))
                        {
                            Canvas.DrawImage(bmp, destRect);
                        }
                    }
                }
                else
                {
                    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.
                        }

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

                        PointF[] ImageRect = new PointF[] { new PointF(destRect.Left, destRect.Top), new PointF(destRect.Right, destRect.Top), new PointF(destRect.Left, destRect.Bottom) };
                        Canvas.DrawImage(FinalImage,
                                         ImageRect,
                                         srcRect, GraphicsUnit.Pixel, imgAtt);
                    }
                    finally
                    {
                        if (FinalImage != image)
                        {
                            FinalImage.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (imgAtt != null)
                {
                    imgAtt.Dispose();
                }
            }
        }
Exemplo n.º 3
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();
                        }
                    }
                }
            }
        }