NuGet System.Drawing.Common .NET 6 CA1416 此调用站点可在所有平台上访问。 'Image.FromStream(Stream)' 仅支持:'windows'

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

将 NuGet System.Drawing.Common 升级到 6.0.0 会导致以下错误:

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/

受影响的代码如下:

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

我们使用库访问方法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;
}

我们只在 Azure 上托管应用程序,因此定位 Windows 没问题,但替换 GetThumbnailImage 也是可以接受的。

更新:

定位 Windows 工作正常,直到我们的一位开发人员尝试使用 Visual Studio 2022 for Mac Preview 1 在他的 Apple 计算机上启动解决方案。

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

阅读 .NET 6 Breaking changes Microsoft 有一节关于 System.Drawing.Common

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

他们的建议如下:

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

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.

Note

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.

尽管 Microsoft.Maui.Graphics 处于预览阶段并且被认为是一个实验库,但我还是尝试使用它,因为 Microsoft 将该库作为推荐的操作库。

起初看起来很有希望,但后来我在他们的 IImage Downsize 方法中遇到了一个错误。

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

在解决这个问题之前,我的临时解决方案是使用 Target framework .NET 6,Target OS (none),然后使用 Exclude specific warnings as errors,因为我们已经启用 Treat warnings as errors.

我还在我们的 Web 项目根目录中创建了一个具有以下值的 runtimeconfig.template.json

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

原文:

阅读 Microsoft Docs 上的重大更改后,由于我们只针对 Windows 平台,我决定暂时采取最快的胜利途径并将目标 OS 设置为 Windows.

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

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

这是一个长期开放issue

I have got around it 通过使用

编辑我的共享集会信息(不是自动生成的)
#if NET6_0
[assembly: System.Runtime.Versioning.SupportedOSPlatform("windows7.0")]
#endif