Recently I started to get warnings such as:
CA1416: This call site is reachable on all platforms. 'Image' is only supported on: 'windows'.. When I looked for more information about it I found that Microsoft broke compatibility and no longer supports System.Drawing.Image on platforms other than Windows.
Their recommendation is to switch to one of the following libraries:
- SkiaSharp
- ImageSharp (tiered license)
- Microsoft.Maui.Graphics
Some context:
- I only use
System.Drawing.Imageto Save, Load and convert to Base64 (in order to render it in HTML) - While I do only very basic stuff with it, I use it as part of an internal library that is used throughout the company on multiple projects
- It's part of the public interface of this library
- These projects have to run on both Windows, Mac and Linux
- As it's an internal project, relying on a licensed product is out of the question, so probably ImageSharp is out of the question, which leaves me with either SkiaSharp or Microsoft.Maui.Graphics.
- I only get the warning on a call to
Image.FromFile, and in unit-tests that use it with Moq asIt.IsAny<Image>()
I have two questions:
- As SkiaSharp and Maiu.Graphics have their own classes to represent an image, does it mean that I must break backwards compatibility in my library?
- I couldn't find any simple example for either of these libraries, for just saving, loading and converting an image to base64. Can you provide me with such examples?
Update: I just noticed a couple of interesting facts:
- The call to
Image.FromFileis also from a test. This doesn't matter much though, as these tests need also to run on Linux (on a GitHub Actions runner) - The problem started when I added a reference to Microsoft.Azure.Cosmos in another project in the solution. While both this project and the test project where the warnings appear, (transitively) depend on a common project that use
System.Drawing.Image, neither of these two projects depend on the other one. I find it very strange that adding a reference to one project affects a different projects that don't depend on it...