// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Resizes an image to the given dimensions.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging;
///
/// Resizes an image to the given dimensions.
///
public class Resize : IGraphicsProcessor
{
///
/// Initializes a new instance of the class.
///
public Resize()
{
this.RestrictedSizes = new List();
this.Settings = new Dictionary();
}
///
/// Gets or sets DynamicParameter.
///
public dynamic DynamicParameter
{
get;
set;
}
///
/// Gets or sets any additional settings required by the processor.
///
public Dictionary Settings
{
get;
set;
}
///
/// Gets or sets the list of sizes to restrict resizing methods to.
///
public List RestrictedSizes { get; set; }
///
/// Processes the image.
///
///
/// The current instance of the class containing
/// the image to process.
///
///
/// The processed image from the current instance of the class.
///
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 = maxHeight;
maxSize.Height = maxHeight;
resizeLayer.MaxSize = maxSize;
Resizer resizer = new Resizer(resizeLayer);
newImage = (Bitmap)resizer.ResizeImage(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;
}
}
}