.Net Core 无法使用位图

A.P*_*cat 3 c# .net-core .net-core-2.1

我正在使用 .Net Core 2.1 开发 Web 服务。

我有一个包含所有像素值(灰度)、宽度、高度的字节数组。我想从这些参数创建一个位图。

有我的代码(来自一个有效的 .Net Framework 4 项目):

public FileResult PostSealCryptItem(Byte[] matrix, int width, int height)
{
    Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
            bmp.SetPixel(x, y, Color.FromArgb(ToArgb(matrix, x, y)));

    Byte[] data = BitmapToByteArray(bmp);
    FileContentResult result = new FileContentResult(data , "image/png");

    return result;
}
Run Code Online (Sandbox Code Playgroud)

但是在 .NET Core 中,我遇到了错误:

找不到类型或命名空间名称“位图”(您是否缺少 using 指令或程序集引用?)

我尝试添加对 System.Drawing 的引用,但没有奏效。

jul*_*old 9

安装 Nuget 包 System.Drawing.Common

它不再包含在 .net core 中,必须手动添加。

  • 对于那些稍后会来到这里的人请注意 - 对于 net 6,`System.Drawing.Common` 软件包被认为仅适用于 Windows,不能再在 Mac 或 Linux 上使用 (https://learn.microsoft.com /en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only) (14认同)
  • 如何让它也支持linux? (5认同)