System.Drawing aspnetVnext 核心 CLR 中缺少命名空间

System.Drawing namespace missing in aspnetVnext core CLR

System.drawing 命名空间在 coreCLR 中不可用。我想调整服务器中图像的大小并 return 存储它。

为了提供更多上下文,我的应用程序使用 markdown 进行内容编辑。图片上传后,我想调整大小并将其应用到 HTML 中。我正在制作一个程序集以与不在桌面上的 coreclr 一起工作。

简短的回答(目前也是唯一的回答)是肯定的,您完全正确。 System.Drawing 不是,也永远不会是 Core 的好选择,尤其是因为它是跨平台的。我们正在寻找其他解决方案,例如此处讨论的托管 libgd 包装器 https://github.com/imazen/Graphics-vNext

Alexander Köplinger 开始将 System.Drawing(和 WinForms)的 Mono 实现移植到 .NET Core。您可以在 github.com/akoeplinger/mono-winforms-netcore.

找到他的代码

不过作为公平的警告,他确实是这样说的:

Mono's WinForms implementation didn't get much love in the recent years, so not sure if it has much use outside of experimental/hobby-stuff apps.

如果您使用 windows,您可以尝试 Magick.Net(包含 ImageMagick) https://magick.codeplex.com/

进行图片转换和操作的一种非常简单的方法是使用 Magick.NET(用于说明的示例代码):

using( MagickImage ThisPicture = new MagickImage( JpegPicture ) )
{
    ThisPicture.Scale( XDimensions, YDimensions );
    ThisPicture.ColorType = ColorType.Grayscale;
    ThisPicture.Extent( XDimensions, YDimensions, Gravity.Center, MagickColors.Black );

    using( PixelCollection ThisPixels = ThisPicture.GetPixels() )
    {
        byte Intensity = ThisPixels[ IX, IY ].GetChannel( 0 );
    }
}