Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

imagemagick - Reproduce GDI+'s Composition Quality in Magick.Net

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...