简体   繁体   English

使用 SixLabors.ImageSharp 在 C# 中创建新的 Image 实例

[英]Create new instance of Image in C# using SixLabors.ImageSharp

I am trying to create new instance of Image class with SixLabors.Imagesharp but I got error.我正在尝试使用 SixLabors.Imagesharp 创建 Image class 的新实例,但出现错误。 The system I work with used the old ImageSharp and We want to renew nuget packages and use SixLabors.ImageSharp instead.我使用的系统使用了旧的 ImageSharp,我们想更新 nuget 包并改用 SixLabors.ImageSharp。

The code used to be like this: OLD Code with ImageSharp:以前的代码是这样的: OLD Code with ImageSharp:

var resultImage = new Image<Rgba32>(outputImageWidth, outputImageHeight);

The new code I try to write with SixLabors.ImageSharp is exactly the same but this time I got the massage:我尝试用 SixLabors.ImageSharp 编写的新代码完全相同,但这次我得到了按摩:

Severity Code Description   Project File Linem Suppression State
Error CS0315 The type 'SixLabors.ImageSharp.PixelFormats.Rgba32' cannot be used as type parameter 'TPixel' in the generic type or method 'Image<TPixel>'. There is no boxing conversion from 'SixLabors.ImageSharp.PixelFormats.Rgba32' to '?'.

I tried a lot of other ways to create a new Image but I failed.我尝试了很多其他方法来创建新图像,但都失败了。 Do you have any idea how I can create new Image using SixLabors.Imagesharp?您知道如何使用 SixLabors.Imagesharp 创建新图像吗?

The namespace for the pixel format has changed so your code is missing an import.像素格式的命名空间已更改,因此您的代码缺少导入。

using SixLabors.ImageSharp.PixelFormats;

The following code compiles and runs for 1.0.0-rc0001以下代码针对1.0.0-rc0001编译并运行

using (var image = new Image<Rgba32>(1000, 1000))
{
    // Do something
}

Documentation 文档

Per the Documentation , the constructor does not take the Generic so modify as below and try.根据文档,构造函数不采用泛型,因此如下修改并尝试。

var resultImage = new Image(outputImageWidth, outputImageHeight);

EDIT: Corrected the Documentation Link.编辑:更正了文档链接。 Thanks @James谢谢@詹姆斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM