示例#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 threshold = (int)this.DynamicParameter;
                newImage = new Bitmap(image);
                newImage = Adjustments.Brightness(newImage, threshold);

                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);
        }
示例#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 threshold = (int)this.DynamicParameter;
                return(Adjustments.Brightness(image, threshold));
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }
        }
        /// <summary>
        /// Traces the edges of a given <see cref="Image"/>.
        /// TODO: Move this to another class.
        /// </summary>
        /// <param name="source">
        /// The source <see cref="Image"/>.
        /// </param>
        /// <param name="destination">
        /// The destination <see cref="Image"/>.
        /// </param>
        /// <param name="threshold">
        /// The threshold (between 0 and 255).
        /// </param>
        /// <returns>
        /// The a new instance of <see cref="Bitmap"/> traced.
        /// </returns>
        private static Bitmap Trace(Image source, Image destination, byte threshold = 0)
        {
            int width  = source.Width;
            int height = source.Height;

            // Grab the edges converting to greyscale, and invert the colors.
            ConvolutionFilter filter = new ConvolutionFilter(new SobelEdgeFilter(), true);

            using (Bitmap temp = filter.Process2DFilter(source))
            {
                destination = new InvertMatrixFilter().TransformImage(temp, destination);

                // Darken it slightly to aid detection
                destination = Adjustments.Brightness(destination, -5);
            }

            // Loop through and replace any colors more white than the threshold
            // with a transparent one.
            using (FastBitmap destinationBitmap = new FastBitmap(destination))
            {
                Parallel.For(
                    0,
                    height,
                    y =>
                {
                    for (int x = 0; x < width; x++)
                    {
                        // ReSharper disable AccessToDisposedClosure
                        Color color = destinationBitmap.GetPixel(x, y);
                        if (color.B >= threshold)
                        {
                            destinationBitmap.SetPixel(x, y, Color.Transparent);
                        }
                        // ReSharper restore AccessToDisposedClosure
                    }
                });
            }

            // Darken it again to average out the color.
            destination = Adjustments.Brightness(destination, -5);
            return((Bitmap)destination);
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="source">The current image to process</param>
        /// <param name="destination">The new Image to return</param>
        /// <returns>
        /// The processed <see cref="Bitmap"/>.
        /// </returns>
        public override Bitmap TransformImage(Image source, Image destination)
        {
            using (var graphics = Graphics.FromImage(destination))
            {
                using (var attributes = new ImageAttributes())
                {
                    attributes.SetColorMatrix(this.Matrix);

                    var rectangle = new Rectangle(0, 0, source.Width, source.Height);

                    graphics.DrawImage(source, rectangle, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);

                    // Overlay the image with some semi-transparent colors to finish the effect.
                    using (var path = new GraphicsPath())
                    {
                        path.AddRectangle(rectangle);

                        // Paint a burgundy rectangle with a transparency of ~30% over the image.
                        // Paint a blue rectangle with a transparency of 20% over the image.
                        using (var brush = new SolidBrush(Color.FromArgb(77, 38, 14, 28)))
                        {
                            Region oldClip = graphics.Clip;
                            graphics.Clip = new Region(rectangle);
                            graphics.FillRectangle(brush, rectangle);

                            // Fill the blue.
                            brush.Color = Color.FromArgb(51, 29, 32, 59);
                            graphics.FillRectangle(brush, rectangle);
                            graphics.Clip = oldClip;
                        }
                    }
                }
            }

            // Add brightness and contrast to finish the effect.
            destination = Adjustments.Brightness(destination, 5);
            destination = Adjustments.Contrast(destination, 85);

            return((Bitmap)destination);
        }