Resolving 'System.Drawing.Image' Errors After Upgrading to .NET 9
Microsoft recently released .NET 9, the latest version of its popular development platform. This release brings several enhancements, including improved performance, enhanced developer productivity tools, and updated runtime features. With a focus on streamlining application development and introducing new capabilities, .NET 9 continues to solidify its position as a powerful framework for modern software development.
Being excited about the new .NET 9 release, I decided to upgrade one of the projects I was working on from .NET Core 8 to .NET 9. However, to my surprise, I encountered the following error while building the application:
The type name 'Image' could not be found in the namespace 'System.Drawing'. This type has been forwarded to assembly 'System.Drawing.Common, Version=0.0.0.0 Consider adding a reference to that assembly.
See the screenshot of the error shown in the Visual Studio.
System.Drawing.Image img = System.Drawing.Image.FromStream(memoryStream);
At first, I didn’t have a clear idea about what was causing the issue. Searching online didn’t yield much helpful information at the time. After some trial and error, I decided to check the NuGet Package Manager to see if there was a package named System.Drawing.Common, as hinted in the error message.
I found the package in the NuGet Package Manager and installed it. To my relief, this resolved the error, and the application started building successfully. The best part? There was no need to modify any existing namespaces or references in my code. Simply adding the System.Drawing.Common package fixed the issue seamlessly.
Summary
The error was caused because .NET 9 does not include the System.Drawing.Common library by default. In previous versions like .NET 8, this library was automatically referenced in certain project templates, allowing developers to use classes like System.Drawing.Image seamlessly. With .NET 9, this behavior has changed, requiring developers to explicitly include the System.Drawing.Common NuGet package when these features are needed.