Jason Sturges Jason Sturges - 1 year ago 232
C# Question

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.

resized

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.

Answer Source

Frederik Carlier of CoreCompat recommended adding a reference to:

This copies required dependencies for macOS, and this build resolves antialias issues with libgdiplus.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download