/// <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="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); }
/// <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); }
/// <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); }
/// <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); }