The DynamicImage evolutionary model
Starting with version 2.3 of the SP library, the DynamicImage is configurable directly to the engine; this means that the developer can plug-in his own image creation or transformation algorithms.
The configuration process is as easy as possible and it does not require any interaction with the image generation service.
It takes 2 steps:
The custom image creation or transformation needs to be custom implemented, of course.
For the image creation (for instance the image needs to be loaded from database, or maybe a set of images need a repeatable background pattern)
the IImageCreator interface needs to be implemented in a simple class. The interface brings a Get method where the image creation needs to be implemented.
For the image transformation (a great deal of possible scenarios: effects, custom text, textures, etc) the IImageTransformation needs to be implemented in
a transformation class. The IImageTransformation.Apply method represents the custom transformation implementation.
public class InvertColorsImageTransformation : IImageTransformation
{
#region IImageTransformation Members
/// <summary>
/// Applies the specified image transformation.
/// </summary>
/// <param name="image">The image to be transfored.</param>
/// <param name="parameters">The transformation params.</param>
public void Apply(ref System.Drawing.Image image, NameValueCollection parameters)
{
using (Image imageCopy = (Image)image.Clone())
using (Graphics g = Graphics.FromImage(image))
using (ImageAttributes ia = new ImageAttributes())
{
ColorMatrix cm = new ColorMatrix(new float[][] {
new float[] { -1f, 0, 0, 0, 0 },
new float[] { 0, -1f, 0, 0, 0 },
new float[] { 0, 0, -1f, 0, 0 },
new float[] { 0, 0, 0, 1f, 0 },
new float[] { 0, 0, 0, 0, 1f }
});
ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Default);
g.DrawImage(imageCopy, new Rectangle(Point.Empty, imageCopy.Size), 0, 0, imageCopy.Width,
imageCopy.Height, GraphicsUnit.Pixel, ia);
}
}
#endregion
}
The second step is to register the newly created creation or transformation class. The registration for both of them takes just 2 parameters: the type of the custom
creation or transformation passed as the generic type and the second one is a list of parameters which can be helpfull in the image creation or transformation.
It's easy to notice that just a custom creator can be registered but theoreticaly as many custom transformations.
protected override void OnLoad(EventArgs e)
{
this.DynamicImage2.RegisterCustomTransformationType<InvertColorsImageTransformation>(null);
}
That's all the dynamic image requires to run custom implementation.
There are 2 small observations because of the fact that the custom parameters are passed to the image generation service by the query string.
1. Try to avoid 3 chars long param keys in respect to the built-in ones, but try not to make them too long; anyway any key collision will trigger cause exception.
2. Try not to indirectly overload the query string by registering too many transformations.