Example #1
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Image  image    = factory.Image;

            try
            {
                int percentage = this.DynamicParameter;

                newImage = new Bitmap(image);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                newImage = Adjustments.Alpha(newImage, percentage);

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                if (newImage != null)
                {
                    newImage.Dispose();
                }

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return(image);
        }
Example #2
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Image image = factory.Image;

            try
            {
                int percentage = this.DynamicParameter;
                return(Adjustments.Alpha(image, percentage));
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }
        }
Example #3
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Image image = factory.Image;

            try
            {
                ImageLayer imageLayer = this.DynamicParameter;
                Bitmap     overlay    = new Bitmap(imageLayer.Image);

                // Set the resolution of the overlay and the image to match.
                overlay.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                Size size          = imageLayer.Size;
                int  width         = image.Width;
                int  height        = image.Height;
                int  overlayWidth  = size != Size.Empty ? Math.Min(width, size.Width) : Math.Min(width, overlay.Width);
                int  overlayHeight = size != Size.Empty ? Math.Min(height, size.Height) : Math.Min(height, overlay.Height);

                Point?position = imageLayer.Position;
                int   opacity  = imageLayer.Opacity;

                if (image.Size != overlay.Size || image.Size != new Size(overlayWidth, overlayHeight))
                {
                    // Find the maximum possible dimensions and resize the image.
                    ResizeLayer layer   = new ResizeLayer(new Size(overlayWidth, overlayHeight), ResizeMode.Max);
                    Resizer     resizer = new Resizer(layer)
                    {
                        AnimationProcessMode = factory.AnimationProcessMode
                    };
                    overlay       = resizer.ResizeImage(overlay, factory.FixGamma);
                    overlayWidth  = overlay.Width;
                    overlayHeight = overlay.Height;
                }

                // Figure out bounds.
                Rectangle parent = new Rectangle(0, 0, width, height);
                Rectangle child  = new Rectangle(0, 0, overlayWidth, overlayHeight);

                // Apply opacity.
                if (opacity < 100)
                {
                    overlay = Adjustments.Alpha(overlay, opacity);
                }

                using (Graphics graphics = Graphics.FromImage(image))
                {
                    graphics.SmoothingMode      = SmoothingMode.AntiAlias;
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;

                    if (position != null)
                    {
                        // Draw the image in position catering for overflow.
                        graphics.DrawImage(overlay, new Point(Math.Min(position.Value.X, width - overlayWidth), Math.Min(position.Value.Y, height - overlayHeight)));
                    }
                    else
                    {
                        RectangleF centered = ImageMaths.CenteredRectangle(parent, child);
                        graphics.DrawImage(overlay, new PointF(centered.X, centered.Y));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return(image);
        }
Example #4
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            var    image      = factory.Image;
            Bitmap background = null;
            Bitmap newImage   = null;

            try
            {
                ImageLayer imageLayer = DynamicParameter;
                background = new Bitmap(imageLayer.Image);

                // Set the resolution of the background and the image to match.
                background.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                var size             = imageLayer.Size;
                var width            = image.Width;
                var height           = image.Height;
                var backgroundWidth  = size != Size.Empty ? Math.Min(width, size.Width) : Math.Min(width, background.Width);
                var backgroundHeight = size != Size.Empty ? Math.Min(height, size.Height) : Math.Min(height, background.Height);

                var position = imageLayer.Position;
                var opacity  = imageLayer.Opacity;

                if (image.Size != background.Size || image.Size != new Size(backgroundWidth, backgroundHeight))
                {
                    // Find the maximum possible dimensions and resize the image.
                    var layer   = new ResizeLayer(new Size(backgroundWidth, backgroundHeight), ResizeMode.Max);
                    var resizer = new Resizer(layer)
                    {
                        AnimationProcessMode = factory.AnimationProcessMode
                    };
                    background       = resizer.ResizeImage(background, factory.FixGamma);
                    backgroundWidth  = background.Width;
                    backgroundHeight = background.Height;
                }

                // Figure out bounds.
                var parent = new Rectangle(0, 0, width, height);
                var child  = new Rectangle(0, 0, backgroundWidth, backgroundHeight);

                // Apply opacity.
                if (opacity < 100)
                {
                    background = Adjustments.Alpha(background, opacity);
                }

                newImage = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                using (var graphics = Graphics.FromImage(newImage))
                {
                    GraphicsHelper.SetGraphicsOptions(graphics, true);

                    if (position != null)
                    {
                        // Draw the image in position catering for overflow.
                        graphics.DrawImage(background, new Point(Math.Min(position.Value.X, width - backgroundWidth), Math.Min(position.Value.Y, height - backgroundHeight)));
                    }
                    else
                    {
                        var centered = ImageMaths.CenteredRectangle(parent, child);
                        graphics.DrawImage(background, new PointF(centered.X, centered.Y));
                    }

                    // Draw passed in image onto graphics object.
                    graphics.DrawImage(image, 0, 0, width, height);
                }

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + GetType().Name, ex);
            }
            finally
            {
                background?.Dispose();
            }

            return(image);
        }
Example #5
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            var image = factory.Image;

            Bitmap overlay = null;

            try
            {
                ImageLayer imageLayer = DynamicParameter;
                overlay = new Bitmap(imageLayer.Image);

                // Set the resolution of the overlay and the image to match.
                overlay.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                var size          = imageLayer.Size;
                var width         = image.Width;
                var height        = image.Height;
                var overlayWidth  = size != Size.Empty ? Math.Min(width, size.Width) : Math.Min(width, overlay.Width);
                var overlayHeight = size != Size.Empty ? Math.Min(height, size.Height) : Math.Min(height, overlay.Height);

                var position = imageLayer.Position;
                var opacity  = imageLayer.Opacity;

                if (image.Size != overlay.Size || image.Size != new Size(overlayWidth, overlayHeight))
                {
                    // Find the maximum possible dimensions and resize the image.
                    var layer   = new ResizeLayer(new Size(overlayWidth, overlayHeight), ResizeMode.Max);
                    var resizer = new Resizer(layer)
                    {
                        AnimationProcessMode = factory.AnimationProcessMode
                    };
                    overlay       = resizer.ResizeImage(overlay, factory.FixGamma);
                    overlayWidth  = overlay.Width;
                    overlayHeight = overlay.Height;
                }

                // Figure out bounds.
                var parent = new Rectangle(0, 0, width, height);
                var child  = new Rectangle(0, 0, overlayWidth, overlayHeight);

                // Apply opacity.
                if (opacity < 100)
                {
                    overlay = Adjustments.Alpha(overlay, opacity);
                }

                using (var graphics = Graphics.FromImage(image))
                {
                    GraphicsHelper.SetGraphicsOptions(graphics, true);

                    if (position != null)
                    {
                        // Draw the image in position catering for overflow.
                        graphics.DrawImage(overlay, new Point(Math.Min(position.Value.X, width - overlayWidth), Math.Min(position.Value.Y, height - overlayHeight)));
                    }
                    else
                    {
                        var centered = ImageMaths.CenteredRectangle(parent, child);
                        graphics.DrawImage(overlay, new PointF(centered.X, centered.Y));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + GetType().Name, ex);
            }
            finally
            {
                overlay?.Dispose();
            }

            return(image);
        }