.Net Core 无法使用位图

.Net Core unable to use Bitmap

我正在使用 .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;
}

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

The type or namespace name 'Bitmap' could not be found (are you missing a using directive or an assembly reference?)

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

您可以使用 ImageSharp 而不是 System.Drawing

https://github.com/SixLabors/ImageSharp

Nuget 控制台

Install-Package SixLabors.ImageSharp -Version 1.0.0-beta0005

Bitmap 是包含在 .Net Framework 中的 System.Drawing 的一部分。

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

通过 right-clicking 在 visual studio 中的项目上安装 Nuget 包 System.Drawing.Common 并选择 Manage Nuget Packages

在Nuget包管理器中,点击Browse Tab,在顶部搜索System.Drawing.Common,应该是微软官方的第一个包。它将像在 .Net Framework 中一样工作:

Aspose.Drawing 是 drop-in cross-platform 替代 System.Drawing。该库是完全托管的,并支持使用 .Net Core 2.1 的 Web 服务。 (我是开发者之一。)

有一个NuGet包代替了System.Drawing,你可以使用它

Install-Package System.Drawing.Common

这个对我有用 dotnet add package System.Drawing.Common --version 5.0.3