Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core unable to use Bitmap

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.

like image 570
A.Pissicat Avatar asked Jan 14 '19 11:01

A.Pissicat


2 Answers

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

like image 65
julian bechtold Avatar answered Sep 22 '22 01:09

julian bechtold


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

Install-Package System.Drawing.Common
like image 43
pejman Avatar answered Sep 19 '22 01:09

pejman