public Stream Resize(Stream imageStream, int maxWidthOrHeight) { Size size = new Size(maxWidthOrHeight, maxWidthOrHeight); ResizeLayer layer = new ResizeLayer(size, ResizeMode.Max); Resizer resizer = new Resizer(layer); Bitmap bmp = resizer.ResizeImage(Image.FromStream(imageStream)); return ToStream(bmp, ImageFormat.Jpeg); }
/// <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) { 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) { 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) { 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); newImage = resizer.ResizeImage(image); // Check that the original image has not been returned. if (newImage != image) { // 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; }