I am trying to replace a function which uses GDI+ to add a watermark to an image. With an equivilant function which uses Magick.Net.
The GDI+ function is as follows:
public void DrawImage(Image image, Rectangle sourceRect, Rectangle destRect, double opacity, bool highQuality)
{
using (var g = Graphics.FromImage(Control))
{
using (var imgAttr = new ImageAttributes())
{
if (opacity != 1.0)
{
var matrix = new float[][] {
new float[] { 1, 0, 0, 0, 0},
new float[] { 0, 1, 0, 0, 0},
new float[] { 0, 0, 1, 0, 0},
new float[] { 0, 0, 0, (float)opacity, 0},
new float[] { 0, 0, 0, 0, 0}
};
imgAttr.SetColorMatrix(new ColorMatrix(matrix));
}
imgAttr.SetWrapMode(WrapMode.TileFlipXY);
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = highQuality ? CompositingQuality.HighQuality : CompositingQuality.Default;
g.PixelOffsetMode = highQuality ? PixelOffsetMode.HighQuality : PixelOffsetMode.None;
g.SmoothingMode = highQuality ? SmoothingMode.HighQuality : SmoothingMode.None;
g.InterpolationMode = highQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Bilinear;
g.DrawImage(image, destRect, sourceRect.X, sourceRect.Y, sourceRect.Width, sourceRect.Height, GraphicsUnit.Pixel, imgAttr);
}
}
}
So far using Magick.Net I have a function like so:
public void DrawImage(MagickImage image, MagickGeometry sourceRect, MagickGeometry destRect, double opacity, bool highQuality = false)
{
var source = image.Clone(sourceRect.X, sourceRect.Y, sourceRect.Width, sourceRect.Height);
destRect.IgnoreAspectRatio = true;
source.Resize(destRect);
source.Alpha(AlphaOption.Set);
source.Evaluate(Channels.Alpha, EvaluateOperator.Multiply, opacity);
_magick.Composite(source, destRect.X, destRect.Y, CompositeOperator.SrcOver);
}
When the highQuality flag is false the two methods produce identical results. However when the flag is true the GDI+ produces a much more opaque watermark.
Does anyone know how to reproduce the CompositingQuality.HighQuality effect in ImageMagick?