4

I am trying to create new instance of Image class with SixLabors.Imagesharp but I got error. The system I work with used the old ImageSharp and We want to renew nuget packages and use SixLabors.ImageSharp instead.

The code used to be like this: 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:

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?

2 Answers2

9

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

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

Documentation

James South
  • 10,147
  • 4
  • 59
  • 115
0

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

Sathish Guru V
  • 1,417
  • 2
  • 15
  • 39
  • I tried but it doesnt work. Got this error: Severity Code Description Project File Line Suppression State Error CS0144 Cannot create an instance of the abstract class or interface 'Image' ..NET Framework 4.6.1 – Parham Gitijah May 26 '20 at 08:54
  • 1
    The constructor you've linked to is protected, so no that can't be used (even if the non-generic Image wasn't abstract). – Rup May 26 '20 at 11:52
  • Always good to point at the docs. Correct link is [here](https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image-1.html#SixLabors_ImageSharp_Image_1__ctor_System_Int32_System_Int32_) – James South May 26 '20 at 15:34
  • 1
    Image is an abstract class so this doesn't work. https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html – Richard Fuller Jul 17 '20 at 04:42