22

According to the docs, there's supposed to be a class for thumbnail handling in the namespace System.Drawing. I want to create a model that has an image as a property and then save it to a database. However, when I try the code below, I get an error that the class isn't found in that namespace, regardless of the docs stating otherwise.

using System.Drawing;
public class Donkey
{
  public Image Image { get;set; }
}

What am I missing?!

I've googled it but only found old posts and examples that seem not to work.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

2 Answers2

62

Try installing the System.Drawing.Common NuGet package. This contains Image and other related types like Bitmap.

PM> Install-Package System.Drawing.Common

You can also install using the NuGet Package Manager UI accessible by right-clicking the solution in Solution Explorer and choosing Manage NuGet Packages...

System.Drawing.Common

Adding a clarification comment from @KonradViltersten:

Traditionally, System.Drawing was included in the full .NET Framework, but not .NET Core because System.Drawing was based on Windows drawing methods. .NET core was designed to be platform independent and thus did not include anything that was platform specific (like drawing). Because this functionality is so requested, MS released a separate assembly that could be installed to provide the functionality

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • 3
    It seem rather random that *System.Drawing.Printing* is there by default but *System.Drawing.Image* needs to be nugotten (newgot? gotten by nuget? nugetgot?)... Also a clearer documentation of that would be of help. – Konrad Viltersten Jan 23 '19 at 21:28
  • Also, a quick validation of my assumption - that's the right class to manage images, right? I can store that junk using EF directly to the DB. Correct? – Konrad Viltersten Jan 23 '19 at 21:29
  • 1
    It's not that weird. System.Drawing.Common, like the description says, gives access to GDI+ graphics. This had to be rewritten from classic .net to .net core, and is not part of the .net core build but rather an extension since .net core is meant to be minimum necessary components. I can't speak for the .Printing namespace but this is absolutely correct. – Zakk Diaz Jan 23 '19 at 21:31
  • @ErikFunkenbusch That was very well-worded. May I suggest that you copy in your comment as a paragraph of the accepted answer? It would make it even more great. And then we can remove the comments. – Konrad Viltersten Jan 23 '19 at 21:51
  • A link to the original Microsoft announcement will make it clear https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/ – Lex Li Jan 23 '19 at 21:56
1

Since .NET 6 System.Drawing.Common only supported on Windows. Check out the Recommended action section of the doc for alternatives:

To use these APIs for cross-platform apps, migrate to one of the following libraries:

Guru Stron
  • 102,774
  • 10
  • 95
  • 132