/// <summary>
        /// Performs the processing of an image.
        /// </summary>
        /// <param name="inputImage">The image to perform the processing on.</param>
        /// <returns>
        /// The image after it has been processed by this filter.
        /// </returns>
        public override Image Process(Image inputImage)
        {
            using (Graphics g = Graphics.FromImage(inputImage))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode  = InterpolationMode.HighQualityBicubic;

                Font font = new Font(this.FontName, this.FontSize, this.FontStyle, GraphicsUnit.Pixel);

                // Position the text according to the size of the string and the specified anchor location.
                Size size = g.MeasureString(this.Text, font).ToSize();

                Rectangle imageArea     = new Rectangle(Point.Empty, inputImage.Size);
                Rectangle watermarkArea = new Rectangle(Point.Empty, size);

                RectangleUtil.PositionRectangle(this.AnchorLocation, imageArea, ref watermarkArea);

                watermarkArea.Offset(this.Offset);

                // Mix the specified colour with our opacity value
                Color textColour = Color.FromArgb((int)(this.Opacity * 255.0f), this.TextColour);

                // Draw the text
                g.DrawString(this.Text, font, new SolidBrush(textColour), new PointF((float)watermarkArea.X, (float)watermarkArea.Y));
            }

            return(inputImage);
        }
        /// <summary>
        /// Performs the processing of an image.
        /// </summary>
        /// <param name="inputImage">The image to perform the processing on.</param>
        /// <returns>
        /// The image after it has been processed by this filter.
        /// </returns>
        public Image Process(Image inputImage)
        {
            Size      imageSize  = inputImage.Size;
            Rectangle sourceRect = Rectangle.Empty;

            if (_cropMode == CropMode.Area)
            {
                sourceRect = this.CropArea;

                int x2 = sourceRect.X + sourceRect.Width;
                if (x2 > inputImage.Width)
                {
                    sourceRect.Width -= (x2 - inputImage.Width);
                }

                int y2 = sourceRect.Y + sourceRect.Height;
                if (y2 > inputImage.Height)
                {
                    sourceRect.Height -= (y2 - inputImage.Height);
                }
            }
            else if (_cropMode == CropMode.ShrinkFactor)
            {
                Rectangle cropped = new Rectangle(0, 0, (int)((double)imageSize.Width * ShrinkFactor), (int)((double)imageSize.Height * ShrinkFactor));

                // Position this cropped image within the main image bounds, respecting the chosen anchor
                RectangleUtil.PositionRectangle(this.Anchor, new Rectangle(Point.Empty, imageSize), ref cropped);

                sourceRect = cropped;
            }

            // Write the final image
            Bitmap outputBitmap = new Bitmap(sourceRect.Width, sourceRect.Height);

            using (Graphics g = Graphics.FromImage(outputBitmap))
            {
                Rectangle destRect = new Rectangle(0, 0, outputBitmap.Width, outputBitmap.Height);
                g.DrawImage(inputImage, destRect, sourceRect, GraphicsUnit.Pixel);
            }

            return(outputBitmap);
        }
Exemple #3
0
        /// <summary>
        /// Performs the processing of an image.
        /// </summary>
        /// <param name="inputImage">The image to perform the processing on.</param>
        /// <returns>
        /// The image after it has been processed by this filter.
        /// </returns>
        public override Image Process(Image inputImage)
        {
            using (Graphics g = Graphics.FromImage(inputImage))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode  = InterpolationMode.HighQualityBicubic;

                // Figure out the position based on the anchor location
                Rectangle imageRect     = new Rectangle(Point.Empty, inputImage.Size);
                Rectangle watermarkRect = new Rectangle(Point.Empty, WatermarkImage.Size);

                RectangleUtil.PositionRectangle(this.AnchorLocation, imageRect, ref watermarkRect);

                watermarkRect.Offset(this.Offset);

                ImageAttributes imageAttr = BuildImageAttributes();

                g.DrawImage(this.WatermarkImage, watermarkRect, 0, 0, WatermarkImage.Width, WatermarkImage.Height, GraphicsUnit.Pixel, imageAttr);
            }
            return(inputImage);
        }
        // Define other methods and classes here
        /// <summary>
        /// Finds the largest rectangle which can fit inside sourceRect with the desired aspect ratio
        /// </summary>
        /// <param name="sourceRect">The container rectangle</param>
        /// <param name="desiredAspect">The desired aspect ratio</param>
        /// <param name="anchorLocation">The anchor location.</param>
        /// <returns>A Rectangle which represents the largest possible area that should be taken from the source image given the input parameters.</returns>
        public virtual Rectangle GetLargestInset(Rectangle sourceRect, float desiredAspect, AnchorLocation anchorLocation)
        {
            Rectangle destRect = default(Rectangle);

            float sourceAspect = (float)sourceRect.Width / (float)sourceRect.Height;
            float ratioScale   = desiredAspect / sourceAspect;

            if (sourceAspect > desiredAspect)
            {
                destRect.Width  = (int)((float)sourceRect.Width * ratioScale);
                destRect.Height = sourceRect.Height;
            }
            else
            {
                destRect.Width  = sourceRect.Width;
                destRect.Height = (int)((float)sourceRect.Height / ratioScale);
            }

            RectangleUtil.PositionRectangle(anchorLocation, sourceRect, ref destRect);

            return(destRect);
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="inputImage">The input image to process</param>
        /// <returns>The resulting image after processing</returns>
        public virtual Image Process(Image inputImage)
        {
            Size bitmapSize = Size.Empty;
            Size outputSize = GetNewSize(inputImage, this.TargetSize, this.ResizeMethod, out bitmapSize);

            Bitmap outputBmp = new Bitmap(bitmapSize.Width, bitmapSize.Height);

            outputBmp.SetResolution(this.Resolution.X, this.Resolution.Y);

            Graphics g = Graphics.FromImage(outputBmp);

            g.CompositingQuality = this.CompositingQuality;
            g.InterpolationMode  = this.InterpoliationMode;

            Rectangle destRect   = new Rectangle(new Point(0, 0), outputSize);
            Rectangle sourceRect = new Rectangle(0, 0, inputImage.Width, inputImage.Height);

            float outputAspect = (float)outputSize.Width / (float)outputSize.Height;

            if (this.ResizeMethod == ResizeMethod.Crop)
            {
                sourceRect = GetLargestInset(sourceRect, outputAspect, this.AnchorLocation);
            }
            else if (this.ResizeMethod == Filters.ResizeMethod.AspectRatioFill)
            {
                Rectangle bitmapRectangle = new Rectangle(Point.Empty, bitmapSize);

                // Draw a background colour.
                g.FillRectangle(new SolidBrush(this.BackgroundColour), bitmapRectangle);

                // Position the image inside according to the anchor location
                RectangleUtil.PositionRectangle(this.AnchorLocation, bitmapRectangle, ref destRect);
            }

            g.DrawImage(inputImage, destRect, sourceRect, GraphicsUnit.Pixel);

            return(outputBmp);
        }