Esempio n. 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;
            Bitmap mask           = null;
            Bitmap maskCropped    = null;
            Bitmap maskPositioned = null;
            Image  image          = factory.Image;

            try
            {
                int width  = image.Width;
                int height = image.Height;
                Tuple <Image, Point?> parameters = this.DynamicParameter;
                mask = new Bitmap(parameters.Item1);
                Point?position = parameters.Item2.HasValue ? parameters.Item2 : null;

                if (mask.Size != image.Size)
                {
                    Rectangle parent = new Rectangle(0, 0, width, height);
                    Rectangle child  = ImageMaths.GetFilteredBoundingRectangle(mask, 0, RgbaComponent.A);
                    maskCropped = new Bitmap(child.Width, child.Height);
                    maskCropped.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                    // First crop any bounding transparency.
                    using (Graphics graphics = Graphics.FromImage(maskCropped))
                    {
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(
                            mask,
                            new Rectangle(0, 0, child.Width, child.Height),
                            child.X,
                            child.Y,
                            child.Width,
                            child.Height,
                            GraphicsUnit.Pixel);
                    }

                    // Now position the mask in an image of the same dimensions as the original.
                    maskPositioned = new Bitmap(width, height);
                    maskPositioned.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    using (Graphics graphics = Graphics.FromImage(maskPositioned))
                    {
                        graphics.Clear(Color.Transparent);

                        if (position != null)
                        {
                            // Apply the mask at the given position.
                            graphics.DrawImage(maskCropped, position.Value);
                        }
                        else
                        {
                            // Center it instead
                            RectangleF centered = ImageMaths.CenteredRectangle(parent, child);
                            graphics.DrawImage(maskCropped, new PointF(centered.X, centered.Y));
                        }
                    }

                    newImage = Effects.ApplyMask(image, maskPositioned);
                    maskCropped.Dispose();
                    maskPositioned.Dispose();
                }
                else
                {
                    newImage = Effects.ApplyMask(image, mask);
                    mask.Dispose();
                }

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

                if (maskCropped != null)
                {
                    maskCropped.Dispose();
                }

                if (maskPositioned != null)
                {
                    maskPositioned.Dispose();
                }

                if (newImage != null)
                {
                    newImage.Dispose();
                }

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

            return(image);
        }
Esempio n. 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)
        {
            Bitmap newImage       = null;
            Bitmap mask           = null;
            Bitmap maskCropped    = null;
            Bitmap maskPositioned = null;
            var    image          = factory.Image;

            try
            {
                var        width      = image.Width;
                var        height     = image.Height;
                ImageLayer parameters = DynamicParameter;
                mask = new Bitmap(parameters.Image);
                mask.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                var position = parameters.Position;

                if (mask.Size != image.Size)
                {
                    var parent = new Rectangle(0, 0, width, height);
                    var child  = ImageMaths.GetFilteredBoundingRectangle(mask, 0, RgbaComponent.A);
                    maskCropped = new Bitmap(child.Width, child.Height, PixelFormat.Format32bppPArgb);
                    maskCropped.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                    // First crop any bounding transparency.
                    using (var graphics = Graphics.FromImage(maskCropped))
                    {
                        GraphicsHelper.SetGraphicsOptions(graphics);

                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(
                            mask,
                            new Rectangle(0, 0, child.Width, child.Height),
                            child.X,
                            child.Y,
                            child.Width,
                            child.Height,
                            GraphicsUnit.Pixel);
                    }

                    // Now position the mask in an image of the same dimensions as the original.
                    maskPositioned = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
                    maskPositioned.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    using (var graphics = Graphics.FromImage(maskPositioned))
                    {
                        GraphicsHelper.SetGraphicsOptions(graphics, true);
                        graphics.Clear(Color.Transparent);

                        if (position != null)
                        {
                            // Apply the mask at the given position.
                            graphics.DrawImage(maskCropped, position.Value);
                        }
                        else
                        {
                            // Center it instead
                            var centered = ImageMaths.CenteredRectangle(parent, child);
                            graphics.DrawImage(maskCropped, new PointF(centered.X, centered.Y));
                        }
                    }

                    newImage = Effects.ApplyMask(image, maskPositioned);
                    maskCropped.Dispose();
                    maskPositioned.Dispose();
                }
                else
                {
                    newImage = Effects.ApplyMask(image, mask);
                    mask.Dispose();
                }

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

                maskCropped?.Dispose();

                maskPositioned?.Dispose();

                newImage?.Dispose();

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

            return(image);
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="image">The current image to process</param>
        /// <param name="newImage">The new Image to return</param>
        /// <returns>
        /// The processed <see cref="System.Drawing.Bitmap"/>.
        /// </returns>
        public override Bitmap TransformImage(Image image, Image newImage)
        {
            // Bitmaps for comic pattern
            Bitmap highBitmap    = null;
            Bitmap lowBitmap     = null;
            Bitmap patternBitmap = null;
            Bitmap edgeBitmap    = null;
            int    width         = image.Width;
            int    height        = image.Height;

            try
            {
                using (ImageAttributes attributes = new ImageAttributes())
                {
                    Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);

                    attributes.SetColorMatrix(ColorMatrixes.ComicHigh);

                    // Draw the image with the high comic colormatrix.
                    highBitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb);
                    highBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                    // Apply a oil painting filter to the image.
                    highBitmap = new OilPaintingFilter(3, 5).ApplyFilter((Bitmap)image);

                    // Draw the edges.
                    edgeBitmap = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
                    edgeBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    edgeBitmap = Effects.Trace(image, edgeBitmap, 120);

                    using (Graphics graphics = Graphics.FromImage(highBitmap))
                    {
                        graphics.DrawImage(highBitmap, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                    }

                    // Create a bitmap for overlaying.
                    lowBitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb);
                    lowBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                    // Set the color matrix
                    attributes.SetColorMatrix(this.Matrix);

                    // Draw the image with the losatch colormatrix.
                    using (Graphics graphics = Graphics.FromImage(lowBitmap))
                    {
                        graphics.DrawImage(highBitmap, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                    }

                    // We need to create a new image now with a pattern mask to paint it
                    // onto the other image with.
                    patternBitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb);
                    patternBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                    // Create the pattern mask.
                    using (Graphics graphics = Graphics.FromImage(patternBitmap))
                    {
                        graphics.Clear(Color.Transparent);

                        for (int y = 0; y < height; y += 8)
                        {
                            for (int x = 0; x < width; x += 4)
                            {
                                graphics.FillEllipse(Brushes.White, x, y, 3, 3);
                                graphics.FillEllipse(Brushes.White, x + 2, y + 4, 3, 3);
                            }
                        }
                    }

                    // Transfer the alpha channel from the mask to the low saturation image.
                    lowBitmap = Effects.ApplyMask(lowBitmap, patternBitmap);

                    using (Graphics graphics = Graphics.FromImage(newImage))
                    {
                        graphics.Clear(Color.Transparent);

                        // Overlay the image.
                        graphics.DrawImage(highBitmap, 0, 0);
                        graphics.DrawImage(lowBitmap, 0, 0);
                        graphics.DrawImage(edgeBitmap, 0, 0);

                        // Draw an edge around the image.
                        using (Pen blackPen = new Pen(Color.Black))
                        {
                            blackPen.Width = 4;
                            graphics.DrawRectangle(blackPen, rectangle);
                        }

                        // Dispose of the other images
                        highBitmap.Dispose();
                        lowBitmap.Dispose();
                        patternBitmap.Dispose();
                        edgeBitmap.Dispose();
                    }
                }

                // Reassign the image.
                image.Dispose();
                image = newImage;
            }
            catch
            {
                if (newImage != null)
                {
                    newImage.Dispose();
                }

                if (highBitmap != null)
                {
                    highBitmap.Dispose();
                }

                if (lowBitmap != null)
                {
                    lowBitmap.Dispose();
                }

                if (patternBitmap != null)
                {
                    patternBitmap.Dispose();
                }

                if (edgeBitmap != null)
                {
                    edgeBitmap.Dispose();
                }
            }

            return((Bitmap)image);
        }