Devesprit.ImageProcessor & Devesprit.ImageServer
Image Processors

IWebGraphicsProcessor

All the individual image processors follow an interface IWebGraphicsprocessor which allows developers to extend the library (Also see IGraphicsProcessor).

IWebGraphicsProcessor.cs
Copy Code
using System.Collections.Generic;
using System.Drawing;
namespace Devesprit.ImageProcessor.Processors
{
    /// <summary>
    /// Defines properties and methods for ImageProcessor Plugins.
    /// </summary>
    public interface IGraphicsProcessor
    {
        /// <summary>
        /// Gets or sets the DynamicParameter.
        /// </summary>
        dynamic DynamicParameter { get; set; }
        /// <summary>
        /// Gets or sets any additional settings required by the processor.
        /// </summary>
        Dictionary<string, string> Settings { get; set; }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:Devesprit.ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:Devesprit.ImageProcessor.ImageFactory"/> class.
        /// </returns>
        Image ProcessImage(ImageFactory factory);
    }
}

Example

AdjustGamma.cs
Copy Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using Devesprit.ImageProcessor.Internal;
using Devesprit.ImageProcessor.Plugins.AdvancedEffects.Internals;
using Devesprit.ImageProcessor.Processors;
using Devesprit.ImageServer.Processors;
namespace Devesprit.ImageProcessor.Plugins.AdvancedEffects
{
    #region Effect For ImageProcessor
    public class AdjustGamma : IGraphicsProcessor
    {
        public AdjustGamma()
        {
            Settings = new Dictionary<string, string>();
            ProvidersInit.InitSystem();
        }
        public dynamic DynamicParameter { get; set; }
        public Dictionary<string, string> Settings { get; set; }
        public Image ProcessImage(ImageFactory factory)
        {
            try
            {
                var settings = (AdjustGammaSettings)DynamicParameter;
                var image = EffectProcessor.ApplyEffectToImage(factory.Image, settings);
                if (factory.Image != image)
                {
                    factory.Image.Dispose();
                    factory.Image = image;
                }
                return image;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
    #endregion


    #region Effect For WebServer
    public class AdjustGammaWeb : IWebGraphicsProcessor
    {
        public AdjustGammaWeb()
        {
            Processor = new AdjustGamma();
        }
        private static readonly Regex QueryRegex = new Regex(@"ag\.[^&]+=[^&]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        public Regex RegexPattern => QueryRegex;
        public int SortOrder { get; private set; }
        public IGraphicsProcessor Processor { get; }
        public int MatchRegexIndex(string queryString)
        {
            this.SortOrder = int.MaxValue;
            var match = this.RegexPattern.Match(queryString);
            if (match.Success)
            {
                SortOrder = match.Index;
                Processor.DynamicParameter = QueryStringDeserializer.Deserialize<AdjustGammaSettings>(queryString, "ag");
            }
            return this.SortOrder;
        }
    }
    #endregion


    #region Extension Method for ImageFactory Class
    public static class AdjustGammaExtension
    {
        public static ImageFactory AdjustGamma(this ImageFactory imageFactory, AdjustGammaSettings settings)
        {
            if (imageFactory.ShouldProcess)
            {
                var effect = new AdjustGamma { DynamicParameter = settings };
                imageFactory.CurrentImageFormat.ApplyProcessor(effect.ProcessImage, imageFactory);
            }
            return imageFactory;
        }
    }
    #endregion


    #region Settings
    public sealed class AdjustGammaSettings
    {
        [Description("Set the Gamma correction value. Values range from -100 to 100.")]
        public int Gamma { get; set; }
    }
    #endregion
}

How to install

To install your processor please add a section in Processing.Config and put your assembly in Bin directory.

See Also

Extending Devesprit.ImageProcessor

Configuration