28

I am trying to reference System.Drawing in a .NET Core console application targeting .NET 4.6, but the assembly is not there.

According to Microsoft, if you use .NET Core, System.Drawing is not available. But if you reference the full .NET framework, you should be able to use it.

This is my project.json file:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

    "dependencies": {

    },

  "frameworks": {
    "net46": {
    }
  }
}

What is the problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pantonis
  • 5,601
  • 12
  • 58
  • 115
  • 1
    Did you read your own question in the first place? Your first sentence says __I am trying to reference System.Drawing in a .net core console app targeting net46__, which tells me you don't understand difference between ASP.NET Core and .NET Core. I just explained it for you, to help you avoid confusion in future or to **fix** your question. Because for people reading the question its not clear if you want to run it on .NET Core, .NET framework. It's contradicting, because .NET Core and .NET Framework are mutually exclusive. ASP.NET Core runs on both, but ASP.NET Core is a webstack, not runtime – Tseng Dec 09 '16 at 09:39
  • 3
    @Tseng who mentioned asp.net? You mentioned it man. – pantonis Dec 09 '16 at 11:05
  • I totally agree with you. I meant the VS template. But I did not mention ASP.NET – pantonis Dec 09 '16 at 11:58

6 Answers6

28

I know this is old, but there is now a port of the library called System.Drawing.Common. Try installing that one with NuGet.

To make this possible, there is a metapackage called Windows Compatibility Pack. However, this metapackage includes many, many Windows related APIs (distributed as packages).

Source: https://developers.de/2018/01/22/how-to-use-system-drawing-in-net-core/

infl3x
  • 1,492
  • 19
  • 26
  • 1
    Also important to consider: As the name suggest: It's only working on windows. This might or might not be a problem. – Christian Gollhardt Jul 19 '18 at 00:34
  • 2
    System.Drawing.Common works on Windows, Linux and macOS. On Linux and macOS, you need to install its native dependency, ligdiplus. You can do `apt-get install -y libgdiplus` or `brew install mono-libgdiplus`. – Frederik Carlier Nov 15 '18 at 08:34
  • 2
    It will no longer be supported on other platforms in net core 7 just FYI – Sana Ahmed Jun 09 '22 at 11:22
27

Add the NuGet reference Microsoft.Windows.Compatibility.

Notice: mark "Include prerelease"

Of course, it works only if prerelease packages are OK for you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikolay Pakudin
  • 630
  • 6
  • 12
20

System.Drawing is not a good option with ASP.NET.

If you want graphics manipulation, I suggest to use ImageSharp on .NET Core or ImageProcessor / ImageResizer on .NET Framework.

Also, if you really need to use System.Drawing, change frameworks in your project.json file to netstandard1.6 and add in dependencies "NETStandard.Library": "1.6.1".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
J. Doe
  • 2,651
  • 1
  • 13
  • 31
  • Thanks. Although it is still in Alpha version and cannot find any documentation. Hope that no bugs exist because I will run it in production env. very soon. – pantonis Nov 30 '16 at 18:40
  • I just realized I also target full .NET Framework in my project and I have a reference to `"NETStandard.Library": "1.6.1"`, but how come? .NET Standard is a kind of "contract" exposing some APIs that all Frameworks should implement. `System.Drawing` [is quite empty in .NET Core](https://github.com/dotnet/corefx/tree/master/src/System.Drawing.Primitives/src/System/Drawing) for the moment and I've been told on Twitter that even with .NET Standard 2.0 coming soon there will be no System.Drawing implementation similar to what is existing on full .NET Framework. I don't understand – Jérôme MEVEL Dec 01 '16 at 01:24
  • Unfortunately the image format of imagesharp is very limited, with no webp support at all. Pmuch all libraries that do support webp and others rely on system.drawing. – Douglas Gaskell Jun 29 '20 at 04:43
  • I removed the link because the domain has been dropped and now links to spam. – Luke Mar 05 '22 at 23:23
4

When you want to use ASP.NET Core on the .NET Full Framework only, you can reference the old class libraries like this:

{
  "version": "1.0.0-*",

  "frameworks": {
    "net452": {
      "dependencies": {
      },
      "frameworkAssemblies": {
        "System.Drawing": "4.0.0.0",
      }
    }
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
guest
  • 41
  • 1
0

I was able to replace System.Drawing with a multi-platform library called SkiaSharp.

If you're running on Linux, you can also install this NuGet package, so that you don't have to install the dependencies manually.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kurdemol94
  • 358
  • 5
  • 12
-1

For .NET 6 Core:

install Aspose.Drawing.Common
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hesham Yemen
  • 437
  • 5
  • 6
  • That is a bit terse. Can you [elaborate](https://stackoverflow.com/posts/75188531/edit)? Where does it execute (presumably some kind of command line environment)? Which environment was it tested in (incl. versions)? You could also include references to documentation. (But *** *** *** *** *** *** ***without*** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 18 '23 at 09:56
  • 1
    At first I didn't understand comments like these n'either, but it's executed in the Terminal (or Developer PowerShell?) in Visual Studio. It's the same as installing it in the NuGet Package Manager. – Jannick Breunis Apr 18 '23 at 15:19
  • Aspose's licensing might not be palatable to many though. It's at least $1197 for 1 developer to develop software that can be used in unlimited locations, $1995 for 10 developer to develop software that can be used in a max of 10 locations, and $5586 for 10 developers to develop software that can be used in unlimited locations. You can't make an SDK without paying a monthly fee. I feel like this answer should address 1. Why you need a third party library now. 2. That this is paid software with licence fees, and no free option even for non-commercial use. – ProgrammingLlama Apr 20 '23 at 00:42