DotNet Core image antialias using libgdiplus on Mac OS X

DotNet Core apps using libgdiplus do not appear antialiased on Mac. This occurs whether using Mono or CoreCompat System.Drawing.Image.

Not entirely sure I'm aware of the internals; however, on Windows I believe this is using GDI+, whereas on Mac libgdiplus uses Cairo.

On PC (left) the resized image is great, but aliased on Mac OS X (right) using the exact same code.

调整

Is there any insight how to address this incompatibility for macOS targets?

Code used to resize images:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace DrawingTest
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var image = Image.FromFile(@"/users/jsturges/master.png");
            var resized = ResizeImage(image, 256, 256);
            resized.Save(@"/users/jsturges/resized-mac.png");
        }

        public static Bitmap ResizeImage(Image image, int width, int height)
        {
            var ratioX = width / (float)image.Width;
            var ratioY = width / (float)image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var scaleWidth = (int)Math.Floor(image.Width * ratio);
            var scaleHeight = (int)Math.Floor(image.Height * ratio);

            var bitmap = new Bitmap(scaleWidth, scaleHeight, PixelFormat.Format32bppRgb);

            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    var rect = new Rectangle(0, 0, scaleWidth, scaleHeight);
                    graphics.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return bitmap;
        }
    }
}

Original image used to test the resize operation available here.


Frederik Carlier of CoreCompat recommended adding a reference to:

  • runtime.osx.10.10-x64.CoreCompat.System.Drawing 1.0.1-beta004
  • This copies required dependencies for macOS, including an updated build of libgdiplus that resolves antialias issues.

    Currently a workaround for CoreCompat, the System.Drawing libraries of CoreCompat will be updated once netstandard 2.0 releases.

    链接地址: http://www.djcxy.com/p/84152.html

    上一篇: 内存泄漏时使用Image.Save(流,ImageFormat)

    下一篇: DotNet Core图像抗锯齿在Mac OS X上使用libgdiplus