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)
        {
            Image image = factory.Image;

            try
            {
                float angle = this.DynamicParameter;

                // Center of the image
                float rotateAtX = Math.Abs(image.Width / 2);
                float rotateAtY = Math.Abs(image.Height / 2);

                // Create a rotated image.
                image = this.RotateImage(image, rotateAtX, rotateAtY, angle);

                if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                {
                    // Set the width EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width);

                    // Set the height EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height);
                }

                return image;
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <param name="image">The current image to process</param>
        /// <param name="newImage">The new Image to return</param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image TransformImage(ImageFactory factory, Image image, Image newImage)
        {
            using (Graphics graphics = Graphics.FromImage(newImage))
            {
                using (ImageAttributes attributes = new ImageAttributes())
                {
                    attributes.SetColorMatrix(this.Matrix);

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

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

            // Add a vignette to finish the effect.
            factory.Update(newImage);
            Vignette vignette = new Vignette();
            newImage = (Bitmap)vignette.ProcessImage(factory);

            // Reassign the image.
            image.Dispose();
            image = newImage;

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

            try
            {
                // TODO: Optimize this one day when I can break the API.
                newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                IMatrixFilter matrix = this.DynamicParameter;
                newImage = matrix.TransformImage(image, newImage);
                
                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;
        }
Esempio n. 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)
        {
            Bitmap newImage = null;
            Bitmap image = (Bitmap)factory.Image;

            try
            {
                newImage = new Bitmap(image);
                GaussianLayer gaussianLayer = this.DynamicParameter;

                Convolution convolution = new Convolution(gaussianLayer.Sigma) { Threshold = gaussianLayer.Threshold };
                double[,] kernel = convolution.CreateGuassianSharpenFilter(gaussianLayer.Size);
                newImage = convolution.ProcessKernel(newImage, kernel);

                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;
        }
Esempio n. 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)
        {
            Bitmap newImage = null;
            Image image = factory.Image;

            try
            {
                RotateFlipType rotateFlipType = this.DynamicParameter;

                newImage = new Bitmap(image);

                // Flip
                newImage.RotateFlip(rotateFlipType);

                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;
        }
Esempio n. 6
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;
        }
Esempio n. 7
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
            {
                const int Orientation = (int)ExifPropertyTag.Orientation;
                if (!factory.PreserveExifData && factory.ExifPropertyItems.ContainsKey(Orientation))
                {
                    int rotationValue = factory.ExifPropertyItems[Orientation].Value[0];
                    switch (rotationValue)
                    {
                        case 8: // Rotated 90 right
                            // De-rotate:
                            image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                            break;

                        case 3: // Bottoms up
                            image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                            break;

                        case 6: // Rotated 90 left
                            image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                            break;
                    }
                }

                return image;
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }
        }
Esempio n. 8
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>
        /// <remarks>
        /// Based on <see href="http://math.stackexchange.com/questions/1070853/"/>
        /// </remarks>
        public Image ProcessImage(ImageFactory factory)
        {
            Image image = factory.Image;

            try
            {
                Tuple<float, bool> rotateParams = this.DynamicParameter;

                // Create a rotated image.
                image = this.RotateImage(image, rotateParams.Item1, rotateParams.Item2);

                if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                {
                    // Set the width EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width);

                    // Set the height EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height);
                }

                return image;
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }
        }
Esempio n. 9
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
            {
                float angle = this.DynamicParameter;

                // Center of the image
                float rotateAtX = Math.Abs(image.Width / 2);
                float rotateAtY = Math.Abs(image.Height / 2);

                // Create a rotated image.
                newImage = this.RotateImage(image, rotateAtX, rotateAtY, angle);

                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;
        }
Esempio n. 10
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>
        /// <remarks>
        /// Based on <see href="http://math.stackexchange.com/questions/1070853/"/>
        /// </remarks>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Image image = factory.Image;

            try
            {
                Tuple<float, bool> rotateParams = this.DynamicParameter;

                // Create a rotated image.
                newImage = this.RotateImage(image, rotateParams.Item1, rotateParams.Item2);

                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;
        }
Esempio n. 11
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
            {
                RotateFlipType rotateFlipType = this.DynamicParameter;

                // Flip
                image.RotateFlip(rotateFlipType);

                if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                {
                    // Set the width EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width);

                    // Set the height EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height);
                }
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
Esempio n. 12
0
        public void ThenResultingImageSizeShouldBeLikeCropLayer(float left, float top, float right, float bottom, CropMode mode)
        {
            // When crop mode is percentage. The right and bottom values should represent
            // the percentage amount to remove from those sides.
            const int SizeX = 200;
            const int SizeY = 200;
            int expectedWidth = 160;
            int expectedHeight = 144;

            CropLayer cl = new CropLayer(left, top, right, bottom, mode);

            // Arrange
            using (Bitmap bitmap = new Bitmap(SizeX, SizeY))
            using (MemoryStream memoryStream = new MemoryStream())
            {
                bitmap.Save(memoryStream, ImageFormat.Bmp);

                memoryStream.Position = 0;

                using (ImageFactory imageFactory = new ImageFactory())
                using (ImageFactory resultImage = imageFactory.Load(memoryStream).Crop(cl))
                {
                    // Act // Assert
                    Assert.AreEqual(expectedWidth, resultImage.Image.Width);
                    Assert.AreEqual(expectedHeight, resultImage.Image.Height);
                }
            }
        }
Esempio n. 13
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;
            Tuple<IEdgeFilter, bool> parameters = this.DynamicParameter;
            IEdgeFilter filter = parameters.Item1;
            bool greyscale = parameters.Item2;

            try
            {
                ConvolutionFilter convolutionFilter = new ConvolutionFilter(filter, greyscale);

                // Check and assign the correct method. Don't use reflection for speed.
                newImage = filter is I2DEdgeFilter
                    ? convolutionFilter.Process2DFilter((Bitmap)image)
                    : convolutionFilter.ProcessFilter((Bitmap)image);

                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;
        }
Esempio n. 14
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
            {
                Color baseColor = (Color)this.DynamicParameter;
                newImage = new Bitmap(image);

                newImage = Effects.Vignette(newImage, baseColor);

                // Reassign the image.
                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;
        }
Esempio n. 15
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.Contrast(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;
        }
Esempio n. 16
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
            {
                RoundedCornerLayer roundedCornerLayer = this.DynamicParameter;
                int radius = roundedCornerLayer.Radius;
                bool topLeft = roundedCornerLayer.TopLeft;
                bool topRight = roundedCornerLayer.TopRight;
                bool bottomLeft = roundedCornerLayer.BottomLeft;
                bool bottomRight = roundedCornerLayer.BottomRight;

                // Create a rounded image.
                newImage = this.RoundCornerImage(image, radius, topLeft, topRight, bottomLeft, bottomRight);

                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;
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="processor">
        /// The processor.
        /// </param>
        /// <param name="factory">
        /// The factory.
        /// </param>
        private static void ApplyProcessor(Func<ImageFactory, Image> processor, ImageFactory factory)
        {
            ImageInfo imageInfo = factory.Image.GetImageInfo(factory.ImageFormat);

            if (imageInfo.IsAnimated)
            {
                OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);

                // We don't dispose of the memory stream as that is disposed when a new image is created and doing so
                // beforehand will cause an exception.
                MemoryStream stream = new MemoryStream();
                using (GifEncoder encoder = new GifEncoder(stream, null, null, imageInfo.LoopCount))
                {
                    foreach (GifFrame frame in imageInfo.GifFrames)
                    {
                        factory.Update(frame.Image);
                        frame.Image = quantizer.Quantize(processor.Invoke(factory));
                        encoder.AddFrame(frame);
                    }
                }

                stream.Position = 0;
                factory.Update(Image.FromStream(stream));
            }
            else
            {
                factory.Update(processor.Invoke(factory));
            }
        }
Esempio n. 18
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;
            int width = image.Width;
            int height = image.Height;
            Bitmap newImage = null;
            Bitmap edgeBitmap = null;
            try
            {
                HalftoneFilter filter = new HalftoneFilter(5);
                newImage = new Bitmap(image);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                newImage = filter.ApplyFilter(newImage);
                bool comicMode = this.DynamicParameter;

                if (comicMode)
                {
                    // 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(newImage))
                    {
                        // Overlay the image.
                        graphics.DrawImage(edgeBitmap, 0, 0);
                        Rectangle rectangle = new Rectangle(0, 0, width, height);

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

                    edgeBitmap.Dispose();
                }

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

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

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

            return image;
        }
 public void BackgroundColorIsChanged()
 {
     ImageFactory imageFactory = new ImageFactory();
     imageFactory.Load(@"Images\text.png");
     Image original = (Image)imageFactory.Image.Clone();
     imageFactory.BackgroundColor(Color.Yellow);
     AssertionHelpers.AssertImagesAreDifferent(original, imageFactory.Image, "because the background color operation should have been applied on {0}", imageFactory.ImagePath);
 }
Esempio n. 20
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
            {
                Tuple<int, bool> parameters = this.DynamicParameter;
                int degrees = parameters.Item1;
                bool rotate = parameters.Item2;
                int width = image.Width;
                int height = image.Height;

                newImage = new Bitmap(image);

                using (FastBitmap fastBitmap = new FastBitmap(newImage))
                {
                    if (!rotate)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                HslaColor original = HslaColor.FromColor(fastBitmap.GetPixel(x, y));
                                HslaColor altered = HslaColor.FromHslaColor(degrees / 360f, original.S, original.L, original.A);
                                fastBitmap.SetPixel(x, y, altered);
                            }
                        }
                    }
                    else
                    {
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                HslaColor original = HslaColor.FromColor(fastBitmap.GetPixel(x, y));
                                HslaColor altered = HslaColor.FromHslaColor((original.H + (degrees / 360f)) % 1, original.S, original.L, original.A);
                                fastBitmap.SetPixel(x, y, altered);
                            }
                        }
                    }
                }

                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;
        }
Esempio n. 21
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)
        {
            const float InchInCm = 0.3937007874015748f;
            Image image = factory.Image;

            try
            {
                Tuple<int, int, PropertyTagResolutionUnit> resolution = this.DynamicParameter;

                // Set the bitmap resolution data.
                // Ensure that the resolution is recalculated for bitmap since it only
                // supports inches.
                if (resolution.Item3 == PropertyTagResolutionUnit.Cm)
                {
                    float h = resolution.Item1 / InchInCm;
                    float v = resolution.Item2 / InchInCm;
                    ((Bitmap)image).SetResolution(h, v);
                }
                else
                {
                    ((Bitmap)image).SetResolution(resolution.Item1, resolution.Item2);
                }

                if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                {
                    // Set the horizontal EXIF data.
                    int horizontalKey = (int)ExifPropertyTag.XResolution;
                    PropertyItem horizontal = this.GetResolutionItem(horizontalKey, resolution.Item1);

                    // Set the vertical EXIF data.
                    int verticalKey = (int)ExifPropertyTag.YResolution;
                    PropertyItem vertical = this.GetResolutionItem(verticalKey, resolution.Item1);

                    factory.ExifPropertyItems[horizontalKey] = horizontal;
                    factory.ExifPropertyItems[verticalKey] = vertical;

                    // Set the unit data
                    int unitKey = (int)ExifPropertyTag.ResolutionUnit;
                    PropertyItem unit = FormatUtilities.CreatePropertyItem();
                    unit.Id = unitKey;
                    unit.Len = 2;
                    unit.Type = (short)ExifPropertyTagType.Int16;
                    Int32Converter measure = (int)resolution.Item3;
                    unit.Value = new[] { measure.Byte1, measure.Byte2 };

                    // TODO: Create nice streamline getter/setters for EXIF.
                    factory.ExifPropertyItems[unitKey] = unit;
                }
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
Esempio n. 22
0
    public static ImageFactory GetInstance()
    {
        if (instance == null)
        {
            GameObject go = new GameObject();
            instance = go.AddComponent<ImageFactory>();
        }

        return instance;
    }
Esempio n. 23
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 grey = null;
            Image image = factory.Image;
            byte threshold = this.DynamicParameter;

            try
            {
                // Detect the edges then strip out middle shades.
                grey = new ConvolutionFilter(new SobelEdgeFilter(), true).Process2DFilter(image);
                grey = new BinaryThreshold(threshold).ProcessFilter(grey);

                // Search for the first white pixels
                Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(grey, 0);
                grey.Dispose();

                newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                using (Graphics graphics = Graphics.FromImage(newImage))
                {
                    graphics.DrawImage(
                                     image,
                                     new Rectangle(0, 0, rectangle.Width, rectangle.Height),
                                     rectangle.X,
                                     rectangle.Y,
                                     rectangle.Width,
                                     rectangle.Height,
                                     GraphicsUnit.Pixel);
                }

                // Reassign the image.
                image.Dispose();
                image = newImage;

                if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                {
                    // Set the width EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width);

                    // Set the height EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height);
                }
            }
            catch (Exception ex)
            {
                grey?.Dispose();

                newImage?.Dispose();

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

            return image;
        }
Esempio n. 24
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
            {
                ResizeLayer resizeLayer = this.DynamicParameter;

                // Augment the layer with the extra information.
                resizeLayer.RestrictedSizes = this.RestrictedSizes;
                Size maxSize = new Size();

                int maxWidth;
                int maxHeight;
                int.TryParse(this.Settings["MaxWidth"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxWidth);
                int.TryParse(this.Settings["MaxHeight"], NumberStyles.Any, CultureInfo.InvariantCulture, out maxHeight);

                maxSize.Width = maxWidth;
                maxSize.Height = maxHeight;

                resizeLayer.MaxSize = maxSize;

                Resizer resizer = new Resizer(resizeLayer) { ImageFormat = factory.CurrentImageFormat, AnimationProcessMode = factory.AnimationProcessMode };
                newImage = resizer.ResizeImage(image, factory.FixGamma);

                // Check that the original image has not been returned.
                if (newImage != image)
                {
                    // Reassign the image.
                    image.Dispose();
                    image = newImage;

                    if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                    {
                        // Set the width EXIF data.
                        factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width);

                        // Set the height EXIF data.
                        factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height);
                    }
                }
            }
            catch (Exception ex)
            {
                if (newImage != null)
                {
                    newImage.Dispose();
                }

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

            return image;
        }
Esempio n. 25
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 grey = null;
            Image image = factory.Image;

            try
            {
                HaarCascade cascade = this.DynamicParameter;
                grey = new Bitmap(image.Width, image.Height);
                grey.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                grey = MatrixFilters.GreyScale.TransformImage(image, grey);

                HaarObjectDetector detector = new HaarObjectDetector(cascade)
                {
                    SearchMode = ObjectDetectorSearchMode.NoOverlap,
                    ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller,
                    ScalingFactor = 1.5f
                };

                // Process frame to detect objects
                Rectangle[] rectangles = detector.ProcessFrame(grey);
                grey.Dispose();

                newImage = new Bitmap(image);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                using (Graphics graphics = Graphics.FromImage(newImage))
                {
                    using (Pen blackPen = new Pen(Color.White))
                    {
                        blackPen.Width = 4;
                        graphics.DrawRectangles(blackPen, rectangles);
                    }
                }

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

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

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

            return image;
        }
 public void TestLoadImageFromFile(string fileName, string expectedMime)
 {
     var testPhoto = Path.Combine(this.localPath, string.Format("Images/{0}", fileName));
     using (ImageFactory imageFactory = new ImageFactory())
     {
         imageFactory.Load(testPhoto);
         Assert.AreEqual(testPhoto, imageFactory.ImagePath);
         Assert.AreEqual(expectedMime, imageFactory.MimeType);
         Assert.IsNotNull(imageFactory.Image);
     }
 }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <param name="image">The current image to process</param>
        /// <param name="newImage">The new Image to return</param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image TransformImage(ImageFactory factory, Image image, Image newImage)
        {
            using (Graphics graphics = Graphics.FromImage(newImage))
            {
                using (ImageAttributes attributes = new ImageAttributes())
                {
                    attributes.SetColorMatrix(this.Matrix);

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

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

                    // Add a glow to the image.
                    using (GraphicsPath path = new GraphicsPath())
                    {
                        path.AddEllipse(rectangle);
                        using (PathGradientBrush brush = new PathGradientBrush(path))
                        {
                            // Fill a rectangle with an elliptical gradient brush that goes from orange to transparent.
                            // This has the effect of painting the far corners transparent and fading in to orange on the
                            // way in to the centre.
                            brush.WrapMode = WrapMode.Tile;
                            brush.CenterColor = Color.FromArgb(70, 255, 153, 102);
                            brush.SurroundColors = new Color[] { Color.FromArgb(0, 0, 0, 0) };

                            Blend blend = new Blend
                            {
                                Positions = new float[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F },
                                Factors = new float[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f }
                            };

                            brush.Blend = blend;

                            Region oldClip = graphics.Clip;
                            graphics.Clip = new Region(rectangle);
                            graphics.FillRectangle(brush, rectangle);
                            graphics.Clip = oldClip;
                        }
                    }
                }
            }

            // Add a vignette to finish the effect.
            factory.Update(newImage);
            Vignette vignette = new Vignette();
            newImage = (Bitmap)vignette.ProcessImage(factory);

            // Reassign the image.
            image.Dispose();
            image = newImage;

            return image;
        }
Esempio n. 28
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 grey = null;
            Image image = factory.Image;
            byte threshold = this.DynamicParameter;

            try
            {
                // Detect the edges then strip out middle shades.
                grey = new ConvolutionFilter(new SobelEdgeFilter(), true).Process2DFilter(image);
                grey = new BinaryThreshold(threshold).ProcessFilter(grey);

                // Search for the first white pixels
                Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(grey, 0);
                grey.Dispose();

                newImage = new Bitmap(rectangle.Width, rectangle.Height);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                using (Graphics graphics = Graphics.FromImage(newImage))
                {
                    graphics.DrawImage(
                                     image,
                                     new Rectangle(0, 0, rectangle.Width, rectangle.Height),
                                     rectangle.X,
                                     rectangle.Y,
                                     rectangle.Width,
                                     rectangle.Height,
                                     GraphicsUnit.Pixel);
                }

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

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

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

            return image;
        }
Esempio n. 29
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)
        {
            try
            {
                factory.PreserveExifData = this.DynamicParameter;
            }
            catch (Exception ex)
            {
                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return factory.Image;
        }
        public void ImageIsLoadedFromFile()
        {
            foreach (FileInfo file in this.ListInputFiles())
            {
                using (ImageFactory imageFactory = new ImageFactory())
                {
                    imageFactory.Load(file.FullName);

                    imageFactory.ImagePath.Should().Be(file.FullName, "because the path should have been memorized");
                    imageFactory.Image.Should().NotBeNull("because the image should have been loaded");
                }
            }
        }
Esempio n. 31
0
 public void TestAddHsl()
 {
     ImageFactory.GenerateHsl(50, 50).Add(default(HSL));
 }
Esempio n. 32
0
 public void TestModifyHsl()
 {
     TestModify(ImageFactory.GenerateHsl(BunnyPath));
 }
Esempio n. 33
0
 public void TestAddCmyk()
 {
     ImageFactory.GenerateCmyk(50, 50).Add(default(CMYK));
 }
Esempio n. 34
0
 public void TestClearDouble()
 {
     ImageFactory.Generate(50, 50).Clear();
 }
Esempio n. 35
0
 public void TestAddBgra()
 {
     ImageFactory.GenerateBgra(50, 50).Add(default(BGRA));
 }
Esempio n. 36
0
 public void TestAddDouble()
 {
     ImageFactory.Generate(50, 50).Add(default(double));
 }
Esempio n. 37
0
 public void TestClearComplex()
 {
     ImageFactory.GenerateComplex(50, 50).Clear();
 }
Esempio n. 38
0
 public void TestIsReadOnlyBgra()
 {
     Assert.IsTrue(ImageFactory.GenerateBgra(50, 50).IsReadOnly);
 }
Esempio n. 39
0
 public void TestRemoveHsl()
 {
     ImageFactory.GenerateHsl(50, 50).Remove(default(HSL));
 }
Esempio n. 40
0
 public void TestRemoveDouble()
 {
     ImageFactory.Generate(50, 50).Remove(default(double));
 }
Esempio n. 41
0
 public ActionResult Edit(Commander input, string pwd, HttpPostedFileBase file)
 {
     using (var session = DB.Instance.GetSession())
     {
         var commander = session.Load <Commander>(CommanderId);
         commander.PlayerName = input.PlayerName;
         commander.Story      = input.Story;
         //Password
         if (!String.IsNullOrEmpty(pwd))
         {
             if (pwd.Length < 6)
             {
                 throw new Exception("Password must be at least 6 characters long");
             }
             commander.Salt     = Password.GenerateSalt();
             commander.Password = Password.HashPassword(pwd, commander.Salt);
         }
         //Country
         foreach (var ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
         {
             var info = new RegionInfo(ci.LCID);
             if (info.TwoLetterISORegionName == input.Country.Code)
             {
                 commander.Country = new Country
                 {
                     Code = info.TwoLetterISORegionName,
                     Name = info.DisplayName
                 };
                 break;
             }
         }
         //Image
         if (IsImage(file))
         {
             var ext       = Path.GetExtension(file.FileName);
             var imagePath = "/App_Data/upload/commander_" + CommanderId + ".jpg";
             using (file.InputStream)
             {
                 using (FileStream fs = new FileStream(Server.MapPath(imagePath), FileMode.Create))
                 {
                     using (ImageFactory imageFactory = new ImageFactory())
                     {
                         imageFactory.Load(file.InputStream)
                         .Quality(60)
                         .Resize(new ImageProcessor.Imaging.ResizeLayer(
                                     anchorPosition: ImageProcessor.Imaging.AnchorPosition.Center,
                                     resizeMode: ImageProcessor.Imaging.ResizeMode.Crop,
                                     upscale: false,
                                     size: new System.Drawing.Size
                         {
                             Width  = 360,
                             Height = 480
                         }
                                     ))
                         .Format(new ImageProcessor.Imaging.Formats.JpegFormat())
                         .Save(fs);
                     }
                 }
             }
         }
         session.SaveChanges();
     }
     return(RedirectToAction("View", new { id = CommanderId }));
 }
Esempio n. 42
0
 public void TestClearBgra()
 {
     ImageFactory.GenerateBgra(50, 50).Clear();
 }
Esempio n. 43
0
 public void TestClearCmyk()
 {
     ImageFactory.GenerateCmyk(50, 50).Clear();
 }
Esempio n. 44
0
 public void TestClearHsl()
 {
     ImageFactory.GenerateHsl(50, 50).Clear();
 }
Esempio n. 45
0
 public void TestAddComplex()
 {
     ImageFactory.GenerateComplex(50, 50).Add(default(Complex));
 }
Esempio n. 46
0
 public void TestModifyRgb()
 {
     TestModify(ImageFactory.GenerateRgb(BunnyPath));
 }
Esempio n. 47
0
 public void TestAddRgb()
 {
     ImageFactory.GenerateRgb(50, 50).Add(default(RGB));
 }
Esempio n. 48
0
 public void TestRemoveBgra()
 {
     ImageFactory.GenerateBgra(50, 50).Remove(default(BGRA));
 }
Esempio n. 49
0
 public void TestModifyComplex()
 {
     TestModify(ImageFactory.Generate(BunnyPath).ToComplexImage());
 }
Esempio n. 50
0
 public void TestIsSynchronizedComplex()
 {
     Assert.IsFalse(ImageFactory.GenerateComplex(50, 50).IsSynchronized);
 }
Esempio n. 51
0
 public void TestModifyCmyk()
 {
     TestModify(ImageFactory.GenerateCmyk(BunnyPath));
 }
Esempio n. 52
0
 public void TestModifyDouble()
 {
     TestModify(ImageFactory.Generate(BunnyPath));
 }
Esempio n. 53
0
 public void TestRemoveCmyk()
 {
     ImageFactory.GenerateCmyk(50, 50).Remove(default(CMYK));
 }
Esempio n. 54
0
 public void TestModifyBgra()
 {
     TestModify(ImageFactory.GenerateBgra(BunnyPath));
 }
Esempio n. 55
0
 public void TestRemoveComplex()
 {
     ImageFactory.GenerateComplex(50, 50).Remove(default(Complex));
 }
Esempio n. 56
0
 public void TestIsReadOnlyCmyk()
 {
     Assert.IsTrue(ImageFactory.GenerateCmyk(50, 50).IsReadOnly);
 }
Esempio n. 57
0
 public void TestRemoveRgb()
 {
     ImageFactory.GenerateRgb(50, 50).Remove(default(RGB));
 }
Esempio n. 58
0
 public void TestIsReadOnlyDouble()
 {
     Assert.IsTrue(ImageFactory.Generate(50, 50).IsReadOnly);
 }
Esempio n. 59
0
 public void TestClearRgb()
 {
     ImageFactory.GenerateRgb(50, 50).Clear();
 }
Esempio n. 60
0
 public void TestIsSynchronizedBgra()
 {
     Assert.IsFalse(ImageFactory.GenerateBgra(50, 50).IsSynchronized);
 }