Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

NuGet System.Drawing.Common .NET 6 CA1416 This call site is reachable on all platforms. 'Image.FromStream(Stream)' is only supported on: 'windows'

Ask Question

Upgrading NuGet System.Drawing.Common to 6.0.0 causes the following error:

CA1416 This call site is reachable on all platforms. 'Image.FromStream(Stream)' is only supported on: 'windows'.

https://www.nuget.org/packages/System.Drawing.Common/

The affected code is the following:

var drawingImage = System.Drawing.Image.FromStream(memstr);

We use the library to access the method GetThumbnailImage.

public byte[] GetThumbnailBytes(byte[] imageBytes)
    var thumbnailBytes = Array.Empty<byte>();
    using (MemoryStream memstr = new MemoryStream(imageBytes))
        var drawingImage = System.Drawing.Image.FromStream(memstr);
        var thumbnailSize = GetThumbnailSize(drawingImage);
        var thumbnail = drawingImage.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
        var ms = thumbnail.ToStream(drawingImage.RawFormat);
        thumbnailBytes = ms.ReadFully();
    return thumbnailBytes;

We only host the application on Azure so targeting Windows is fine but replacing GetThumbnailImage is acceptable as well.

Update:

Targeting Windows worked fine until one of our developers tried to start the solution on his Apple computer using Visual Studio 2022 for Mac Preview 1.

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1416

Reading .NET 6 Breaking changes Microsoft has a section about System.Drawing.Common.

https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only

Their recommendations are the following:

To use these APIs for cross-platform apps, migrate to one of the following libraries:

  • ImageSharp
  • SkiaSharp
  • Microsoft.Maui.Graphics
  • Alternatively, you can enable support for non-Windows platforms by setting the System.Drawing.EnableUnixSupport runtime configuration switch to true in the runtimeconfig.json file:

    "runtimeOptions": { "configProperties": { "System.Drawing.EnableUnixSupport": true

    This configuration switch was added to give cross-platform apps that depend heavily on this package time to migrate to more modern libraries. However, non-Windows bugs will not be fixed. In addition, we may completely remove support for non-Windows platforms in a future release, even if you enable it using the runtime configuration switch.

    Despite the name of the runtime switch, System.Drawing.EnableUnixSupport, it applies to various non-Windows platforms, such as macOS and Android, which can generally be considered flavors of Unix.

    Even though Microsoft.Maui.Graphics is in preview and is considered an experimental library I tried to use it given that Microsoft has the library as a recommended action library.

    It seemed really promising at first but then I encountered a bug in their IImage Downsize method.

    https://github.com/dotnet/Microsoft.Maui.Graphics/issues/247

    Until that is fixed my temporary solution is using Target framework .NET 6, Target OS (none) and then use Exclude specific warnings as errors given that we have enabled Treat warnings as errors.

    I have also created a runtimeconfig.template.json in our web project root with the following values:

    "runtimeOptions": { "configProperties": { "System.Drawing.EnableUnixSupport": true

    Original:

    After reading about the breaking change on Microsoft Docs and since we only target the Windows platform I decided to do the quickest path to victory for the moment and set Target OS to Windows.

    But this code won't warn if the project targets Windows

    https://learn.microsoft.com/en-us/dotnet/core/compatibility/code-analysis/5.0/ca1416-platform-compatibility-analyzer

    the only fix i needed was the json file. But the syntax was a little wrong for me. It needed to be as described below by Mehmet stackoverflow.com/a/74286035/3093731 { "configProperties": { "System.Drawing.EnableUnixSupport": true } } – Andrew Jan 27 at 21:49 Tried Microsoft.Maui.Graphics for reading width and height from an uploaded image and they were wrong (or anyway not in pixel, but width was a negative value so don't really know), SkiaSharp looks working great! – Majico Jun 2 at 0:39

    .NET 6 Breaking changes Microsoft has a section about System.Drawing.Common

  • add your project runtimeconfig.template.json
  • "configProperties": { "System.Drawing.EnableUnixSupport": true
  • add your DockerFile this or install your linux container
  • FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    RUN apt-get update \
        && apt-get install -y --allow-unauthenticated \
            libc6-dev \
            libgdiplus \
            libx11-dev \
         && rm -rf /var/lib/apt/lists/*
                    some functions will work with docker file. if you take an exception like gdi+ error. you should update dockerfile
    – Mehmet Erdoğdu
                    Feb 15 at 8:28
    

    This has been an long open issue

    I have got around it by editing my shared assembly info (not autogenerated) with

    #if NET6_0
    [assembly: System.Runtime.Versioning.SupportedOSPlatform("windows7.0")]
    #endif
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.