21

I am working on a Web Service using .Net Core 2.1.

I have a byte array containing all pixels values (in grey scale), a width, a height. I want to create a bitmap from theses parameters.

There is my code (from a working .Net Framework 4 project) :

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;
}

But in .NET Core, I've the error :

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

I've try to add reference to System.Drawing but it did not work.

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • There is no built in image manipulation in .NET Core, you need to use a third party tool, but there are some good ones. For example I'm currently using [SkiaSharp](https://www.nuget.org/packages/skiasharp). – DavidG Jan 14 '19 at 11:10
  • Possible duplicate of [How to use bitmap resources in ASP.NET Core app?](https://stackoverflow.com/questions/53341354/how-to-use-bitmap-resources-in-asp-net-core-app) – Sebastian Hofmann Jan 14 '19 at 11:11
  • 2
    There's [System.Drawing.Common](https://www.nuget.org/packages/System.Drawing.Common) – stuartd Jan 14 '19 at 11:12
  • @stuartd Downside of that is that you are limited to running on Windows I believe? – DavidG Jan 14 '19 at 11:12
  • @DavidG looks like it, now I see it has `Microsoft.Win32.SystemEvents` as a dependency – stuartd Jan 14 '19 at 11:17
  • you could use the windows compatibility pack. Guess you have to stick with windows then... https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/?WT.mc_id=-blog-scottha – Mat Jan 14 '19 at 11:18
  • https://www.hanselman.com/blog/HowDoYouUseSystemDrawingInNETCore.aspx – Mat Jan 14 '19 at 11:18
  • @stuartd Yeah, that's why I wouldn't recommend using it. Especially considering `SkiaSharp` and `ImageSharp` both do everything (and more). Skia also supports webp which can be useful. – DavidG Jan 14 '19 at 11:36

5 Answers5

37

Bitmap is part of System.Drawing which was included in .Net Framework.

It is no longer included with .net core and must be added manually.

Install the Nuget package System.Drawing.Common by right-clicking on your project in visual studio and choosing Manage Nuget Packages

In the Nuget Package manager, click on the Browse Tab, search for System.Drawing.Common in the top and it should be the first Package, official by Microsoft. It will work just as in .Net Framework: enter image description here

julian bechtold
  • 1,875
  • 2
  • 19
  • 49
  • 12
    Just a note for those who will come here later - as for net 6 the `System.Drawing.Common` package considered to be Windows-only and can not be used on Mac or Linux anymore (https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only) – Serg Jun 07 '22 at 06:53
  • 3
    How to make it supported for linux as well? – Sana Ahmed Jun 09 '22 at 06:30
5

There is a NuGet package instead of System.Drawing, you can use it

Install-Package System.Drawing.Common
pejman
  • 740
  • 7
  • 13
3

You can use ImageSharp instead of System.Drawing

https://github.com/SixLabors/ImageSharp

Nuget console

Install-Package SixLabors.ImageSharp -Version 1.0.0-beta0005
Mat
  • 1,960
  • 5
  • 25
  • 38
1

Aspose.Drawing is a drop-in cross-platform replacement for System.Drawing. The library is fully managed and supports Web Services using .Net Core 2.1. (I'm one of the developers.)

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
0

This one worked for me dotnet add package System.Drawing.Common --version 5.0.3

Shunya
  • 2,344
  • 4
  • 16
  • 28