Skip to content

Support Full System.Drawing Functionality on .NET Core #21980

Closed

Description

@mellinoe

Update: The following is the current plan.

  • Port the .NET Framework implementation to .NET Core. WIP, See Add support for System.Drawing on Windows. corefx#20593 .
  • Move mono's libgdiplus-based managed implementation from mono to corefx
  • Ensure that there is a code-sharing story between mono and corefx. This likely means removing the source code from mono and importing code from the submodule link that already exists. In progress. Mono has begun using some portions of the Windows implementation already.
  • Port mono's tests for System.Drawing, making any modifications necessary to ensure that they pass on the Windows implementation, which is our baseline for compatibility. In progress
  • Consolidate differences between the Windows and Unix versions of the library. Document all differences in behavior and mark test cases appropriately.

System.Drawing has been identified as one of the top most-used assemblies that are missing from .NET Core. Although there are 3rd-party alternatives that provide more modern versions of the same functionality (ImageSharp, SkiaSharp, etc.), we believe there is still value in providing a binary-compatible implementation of System.Drawing itself. This issue is intended to document and track the work items involved. There are still lots of open questions; feedback and suggestions are encouraged.

Priorities:

  • Simplify migration from .NET Framework to .NET Core. We need to be binary compatible (no recompilation needed for existing assets), and we need to match the behavior as closely as possible.
  • Support System.Drawing where mono does, and facilitate code sharing between our implementations. As part of this, we will be open-sourcing the original Windows implementation from .NET Framework. This should allow us to improve mono's implementation where it diverges, and hopefully reduce the maintenance burden between us by consolidating code and effort.
  • NON-GOALS: We do not view this library as an opportunity for innovation, nor will we invest significant amounts of effort into optimizing or refactoring it. High performance, rich OS-specific functionality, and a redesigned, modern API are not areas we are interested in investing in as part of this effort. There are alternative libraries which target all of those issues and more.

Plan

System.Drawing is, fundamentally, a very thin wrapper over the GDI+ component on Windows. Worse yet, it is not available for UWP applications, so support is limited strictly to Win32 applications. This makes cross-platform implementations difficult, and the largest chunk of work involved with this effort will be figuring out how we can support the library on all of our platforms, and maintaining compatibility between them. There are several existing implementations that we can leverage, but none will support all of our platforms by themselves. We may need several distinct backends in order to get full cross-platform support, although some options could avoid that. The below chart maps implementation approaches (rows) with platform applicability (columns):

Win32 UWP macOS Linux / Others
.NET Framework Version (GDI+) ✓ X X X
Direct2D/Win2D ✓ ✓ X X
Mono/libgdiplus ✓ X ✓ ✓
Xamarin/CoreGraphics X X ✓ X
Managed ✓ ✓ ✓ ✓

.NET Framework Version (GDI+)

This is the existing implementation from the .NET Framework on Windows. It is a very thin wrapper around GDI+.

  • ✔️ Baseline implementation. Well-tested, stable.
  • ❌ Can only be used in non-UWP Windows apps (Win32).

Direct2D/Win2D

In order to get UWP support, we could implement a compatibility layer over Direct2D, or Win2D.

  • ✔️ GPU acceleration
  • ❌ Only buys us UWP support over the existing .NET Framework implementation.
  • ❌ High implementation cost
  • Can be shared between Win32 and UWP applications (Windows 8+ only).

Mono/libgdiplus

This is the implementation used in mono. It uses a complicated native shim called "libgdiplus", which relies on a large set of native binaries.

  • ✔️ libgdiplus is available in a large number of package managers across the Unix ecosystem already. This means we can treat the dependency the same way we do the other native dependencies for .NET Core.
  • ❌ Native code adds a lot of complexity for new devs.

Xamarin/CoreGraphics

This is a specialized implementation used by Xamarin, in support of macOS and iOS. Unlike the "libgdiplus" mono version, it only uses CoreGraphics (a system component).

  • ✔️ No extra native dependencies; only uses system libraries.
  • ❌ macOS-specific. Could be used for iOS (not supported by .NET Core yet).

Managed (ImageSharp or custom)

One option is to implement the library in managed code. We can call into an existing library, like ImageSharp, for image-processing and graphics functionality. This is very desirable for a lot of reasons, but it does have a higher implementation cost.

  • ✔️ Can be used everywhere.
  • ✔️ Can achieve uniform behavior everywhere (at the expense of conformity with .NET Framework).
  • ✔️ Lower maintenance costs in the long-term.
  • ❌ High implementation cost.

Windows-Specific

One option is to treat the library as a Windows-specific component, similar to Registry. Attempting to install and load the library on other platforms would throw PlatformNotSupportedException's, as Registry does.

Tests

We do not have a very good existing test suite in the .NET Framework. The library is mainly covered indirectly, through other components like WinForms. Mono has a good set of conformance tests for their implementation, which we should be able to re-use. Where appropriate, we should also consider test cases which directly compare output from the .NET Framework implementation and other implementations (assuming we choose to support such things).

Location

An open question is where this library should live. If we take third-party dependencies to support some platforms, we may not want this code to live in corefx.

Printing Functionality

One thing skimmed over above is the fact that System.Drawing exposes a lot of functionality for printing images and documents. This poses a different problem from image-processing and graphics, which can be implemented portably. We may decide to exclude all printing-related portions of the library.

Future investments

Once the library is supported on all platforms, we will accept small, targeted improvements to the library, keeping in mind that legacy compatibility is the only important metric for the library. We will not want to merge any code that refactors, optimizes, or augments the Windows version until we have a fully-working cross-platform implementation. This matches our strategy with other compatibility libraries, like System.IO.SerialPort.

@marek-safar @JimBobSquarePants @ViktorHofer @qmfrederik @karelz

Activity

  1. qmfrederik commented on May 25, 2017

    @qmfrederik
    Contributor

    @mellinoe Thanks for the additional information!

    I actually have a good experience with using Mono/libgdiplus via CoreCompat.System.Drawing. Here's why:

    • Mono/libgdiplus actually does run on .NET Core on Windows. When on Windows, Mono's System.Drawing just uses GDI+ from Windows. So I think you can add a checkbox to 'Win32' for Mono's System.Drawing
    • I don't think the native dependencies need to be a problem. libgdiplus ships as a native component for most operating systems .NET Core supports. All Linux distros which .NET Core supports include libgdiplus in their mainline repositories: Ubuntu, Debian, Fedora, OpenSuse - even Alpine has it. Plus, libgidplus was recently added to Homebrew, too.
      So, I would suggest treating libgdiplus as a the single native dependency which would be acquired from the Linux distro or Homebrew - using the same acquisition mechanism for other .NET Core dependencies, such as curl or OpenSSL.
    • Mono's System.Drawing does include the Printing components, so you would get those for free, too.
    • Mono's System.Drawing runs natively on netstandard2.0 (and all unit tests pass!) and is very close to running on netstandard1.6.
    • Could you elaborate on "Native code adds a lot of complexity for new devs"?
    • Regarding "Behavior and performance issues", I'm not really aware of them, so it would be good to have them listed, too.
    • Since it's part of Mono, Mono's System.Drawing is still being maintained.

    Another option would be to use the System.Drawing code from NetFX and have it consume libgdiplus instead of GDI+ on Linux. There's been some talk of open sourcing System.Drawing before, not sure if that's still an option? It would be fairly easy to implement, too.

    Regarding UWP, do you currently have demand for System.Drawing on UWP?

    /cc @akoeplinger

  2. mellinoe commented on May 25, 2017

    @mellinoe
    ContributorAuthor

    All Linux distros which .NET Core supports include libgdiplus in their mainline repositories: Ubuntu, Debian, Fedora, OpenSuse - even Alpine has it. Plus, libgidplus was recently added to Homebrew, too.

    That is good to know; thanks for the info. If we are able to treat it as a fully external dependency, like the other native libs we use, then I would have less hesitation about using it. I'm not an expert on libgdiplus, so I would certainly like to understand more about it. The folks that I have talked with have been hesitant to readily suggest that we go ahead and use it, but it may be okay in the face of less-promising alternatives. In general, though, I think we should be really cautious about any new native dependencies we adopt in .NET Core. They make a lot of things more complicated for us and for customers.

    Could you elaborate on "Native code adds a lot of complexity for new devs"?

    I was trying to express the idea that folks trying to submit fixes for System.Drawing will have a harder time contributing to an external C library than a managed C# library here. Just a different skill-set, is all.

    There's been some talk of open sourcing System.Drawing before, not sure if that's still an option? It would be fairly easy to implement, too.

    At the very least, we will be using the .NET Framework version for Windows, so we will certainly open-source it. We can attempt to use that same implementation against libgdiplus on other platforms, but I fear that would involve reworking tons of the native code. Then again, that is just a guess on my part.

    Regarding UWP, do you currently have demand for System.Drawing on UWP?

    We view System.Drawing as one of the main libraries blocking old customers (and their code) from migrating to our new platforms, including UWP. That's not exactly the question you asked, but the end result is: we'd like to support System.Drawing everywhere, because it has proven to be an important dependency for existing libraries.

  3. qmfrederik commented on May 26, 2017

    @qmfrederik
    Contributor

    At the very least, we will be using the .NET Framework version for Windows, so we will certainly open-source it. We can attempt to use that same implementation against libgdiplus on other platforms, but I fear that would involve reworking tons of the native code. Then again, that is just a guess on my part.

    My guess is that the .NET Framework version will work well with libgdiplus. It's an assumption based on my experience with libgdiplus and how it tries to be binary compatible with GDI+.
    The proof of the pudding is in the eating, though. If you can make the .NET Framework System.Drawing source code available under an open source license, I'd be happy to give it a spin, and run Mono's unit tests for System.Drawing on .NET Framework's System.Drawing backed by libgdiplus running on .NET Core on, say, Linux -- that would give the definitive answer.

    I was trying to express the idea that folks trying to submit fixes for System.Drawing will have a harder time contributing to an external C library than a managed C# library here. Just a different skill-set, is all.

    That's a fair point, although on the other hand the C libraries that libgdiplus uses - such as libpng, libjpeg,... have matured for a long time so there's some value to that, too. Graphics code requires a special skillset anyway, perhaps the bulk of people with that skillset are knowledgeable in C?

    Perhaps another option is to start with libgdiplus and move code from unmanaged to managed. It could help kick start the System.Drawing implementation by reusing libgdiplus and move towards the goal of cross-platform managed code step by step.

  4. JimBobSquarePants commented on May 26, 2017

    @JimBobSquarePants

    Perhaps another option is to start with libgdiplus and move code from unmanaged to managed. It could help kick start the System.Drawing implementation by reusing libgdiplus and move towards the goal of cross-platform managed code step by step.

    If I was to build a managed 2D graphics library (I am) I wouldn't, by choice, re-implement the System.Drawing API.

    That said @mellinoe I'm happy to offer and assist in migration if the decision was made to use ImageSharp as an underlying managed library. In Jamestopia though we wouldn't bother and would actively push developers to migrate to better API's. Seems a lot cheaper and easier for MS.

    There are some fundamental differences in how the two libraries currently work which would have to be discussed and workarounds considered. We're still unfinished so we have great flexibility to alter the API to improve flexibility and fill gaps which would help a lot.

    Pixel Formats
    ImageSharp expands all image formats into one of 20 different pixel formats we bundle. System.Drawing stores images in Format8bppIndexed format and similar in their indexed form. There would have to be a workaround developed to do the same.

    Image Formats

    • Gif - Our output for gif is different to System.Drawing. We support animated gif plus transparency, we also quantize the output using Octrees by default and dither the result. This is all configurable but we would have to limit our encoder to use the default System.Drawing palette.
    • Png - We use all the available filters to encode pngs to reduce file size plus we support indexed png. We can add a switch to not filter the output scanlines to make the types compatible.
    • Jpeg - Our output is very slightly different to System.Drawing (I think due to rounding errors on decode) and our decoder performance is not as good just now. We need help on that to make it up to scratch.
    • Bmp - Should be no issues there. We support more output types but that can be limited.
    • Tiff - We have a tiff format in the works. Great progress is being made but it is unfinished.

    MetaData
    System.Drawing bundles everything together into the PropertyItem class from EXIF metadata to gif loop count and frame delay and makes no effort to decode that data. We've split out metadata into generic, EXIF, and ICC components and offer full reading/writing for those data types. You would have to create Property items to match that data and store the raw bytes.

    Affine Transforms - Resize, Crop etc
    We would need to do some work to ensure that we can produce the same output as System.Drawing. I'm not sure what sampler it uses for the different quality enumerations. We also don't support matrix operations within resize operations so there would have to be work done there.

    Drawing
    We offer drawing with a very similar API to System.Drawing. There may be a few gaps but I imagine. I know off-hand we haven't implemented all the Hatch bushes @tocsoft is our resident expert there so he can fill you in on what is/isn't there.

    I'm sure there's more...

    Regarding "Behavior and performance issues", I'm not really aware of them, so it would be good to have them listed, too.

    @qmfrederik I had a very, very quick scan of the libgdi codebase and immediately found unsupported areas 1, 2. I've no idea what is out there. I remember having issues with Image.FromStream(stream, useembeddedColorManagement) in the past but I don't know if that is still an issue.

    Graphics code requires a special skillset anyway, perhaps the bulk of people with that skillset are knowledgeable in C?

    Btw I couldn't read/write C if you held a gun to my head plus I'm teaching myself graphics programming as I write ImagSharp. Shocking 😝

  5. migueldeicaza commented on May 26, 2017

    @migueldeicaza
    Contributor

    It is worth updating the original issue with the new information provided in the comments.

    Mono's System.Drawing implementation was used for Mono's Windows.Forms implementation and we spent a few years tuning it to work with various third party component vendors. We delegated the hard graphic problems to Cairo.

    It was also built so that on Windows, it would use the system provided GDI+ library, and on other platforms, it uses Mono's libgdiplus, it achieves this by having libgdiplus surface the same API as the Windows libgdiplus.

    This means a couple of things:

    • That Mono's System.Drawing is actually a very thin layer on top of libgdiplus.
    • that on Windows, you get to use the real GDI+, which is quite nice.

    While the implementation works, the font handling capabilities are complicated on Unix, so we used FreeType to get this information. While this works great on Linux systems, it is very slow on first use on a Mac that typically lacks FreeType, so if I was going to do the work in this space, I would start by improving libgdiplus to have a swappable font backend and use Apple's CoreText on OSX to avoid the initial font cache creation problems.

    Cairo and the underlying libraries are faster than any C# code you can write.

    That said, if I was going to take on the long-term maintenance of System.Drawing, today I would take a different approach.

    I would use SkiaSharp as the backing engine as it is actively maintained, actively developed, actively fine tuned and optimized for both Chrome and Android. It also addresses some of the design limitations of Cairo, both the bottlenecks of the API as well as issues with the coordinate space that happen due to loss of precision with huge matrix transformations (Mozilla went through this, and made essentially this switch as well).

    I would start with the sysdrawing-coregraphics which is a project that moved most of the logic from libgdiplus from C to C# and replaced the calls to Cairo with calls to CoreGraphics.

    And then I would replace the calls to CoreGraphics with calls to the Skia APIs.

    Last year I did some work along those lines on my spare time, where I mostly butchered the codebase so it would compile without CoreGraphics and I was planning on adding the Skia support. The time to do that never materialized. If you are interested in that fork, you can find it here.

  6. JimBobSquarePants commented on May 26, 2017

    @JimBobSquarePants

    Just had a thought. What about server support. What are the plans there? That should factor into any decision making.

  7. TheBlueSky commented on May 26, 2017

    @TheBlueSky
    Contributor

    So what is the goal here? Only API compatibility? This is what I understood from what @mellinoe said.

    If this is the case, @migueldeicaza comparison between native code and C# isn't very relevant, as long as C# performance is decent:

    Cairo and the underlying libraries are faster than any C# code you can write.

    I mean if building the API in managed code is an option, let's go for it.

    On the other hand, if we settled on "let's just create something" option, let's not go with Win32-only option; this makes it a bit confusing.

  8. vibeeshan025 commented on May 26, 2017

    @vibeeshan025

    Great initiative!! Its better to have System.Drawing implemented on top of set of interfaces and let the community develop each implementation.
    Example we can use DirectX on windows and OpenGL on linux and mac OS. but the System.Drawing will internally call the interfaces which is platform agnostic.
    If we are going for windows only option then its not going to make any difference to existing .net frameworks.

  9. JimBobSquarePants commented on May 26, 2017

    @JimBobSquarePants

    You need good APIs for that that to be a viable idea. This is a stopgap not a solution.

  10. bitapparat commented on May 26, 2017

    @bitapparat

    I'd personally prefer a more forward-looking approach with an officially supported modern cross-platform graphics API for all .Net platforms, and a generic and optional System.Drawing implementation on top of it. But i'm aware that's a rather extensive and expensive solution and, thus, probably unrealistic.

  11. gulbanana commented on May 26, 2017

    @gulbanana

    I would love to see something like an implementation on top of ImageSharp. This hypothetical System.Drawing port isn't for high performance or high quality use - it's just to remove a blocker for porting legacy code in which image handling might be one small function. It would be fine if it was slower than the original netfx System.Drawing or didn't produce bit-identical output in all ops- it might even be able to get away with throwing NotSupported for some rarely-used apis.

    Building on top of a community library with a modern API would provide a path forward for people to move their code to make direct use of that implentation later on.

  12. stefanolson commented on May 26, 2017

    @stefanolson

    Whilst I'm aware that a lot of people use System.Drawing, the WPF System.Windows.Media api is a lot more capable in many areas, and more similar to the UWP API so personally I would rather see a better, more flexible/modern API than System.Drawing available, although of course there may be valuable things in System.Drawing that may be worth implementing

  13. Rekeyea commented on May 26, 2017

    @Rekeyea

    IMHO System.Drawing should be left as Windows only. It is already being used in .Net Framework projects using ASP.Net Core, but those will still be using Windows since those are enterprise applications and I don't see them moving their whole infrastructure "because now they can".

  14. ViktorHofer commented on May 26, 2017

    @ViktorHofer
    Member

    IMHO System.Drawing should be left as Windows only. It is already being used in .Net Framework projects using ASP.Net Core, but those will still be using Windows since those are enterprise applications and I don't see them moving their whole infrastructure "because now they can".

    I disagree. ASP.NET Core is being used by more and more open source projects and startups because of cross-platform support and its speed improvements in comparison to nodeJS. I think it's viable to support System.Drawing on all environments where .NET Core and ASP.NET Core is running.

    I'm a little worried about binary compatibility here. I think that we definitely need a System.Drawing implementation which is compatible with .NET Framework but I also think that we should build something (together with the community) which is modern, scalable and fast. It might be a good idea to 1. port the existing lib to core (managed) and 2. build a successor of System.Drawing which has all the innovations and improvements which aren't possible with the current API spec.

  15. Rekeyea commented on May 26, 2017

    @Rekeyea

    @ViktorHofer it was said that there are other more modern alternatives like ImageSharp or SkiaSharp. That's why I think it's pointless since those open source projects and startups should use those other alternatives.

  16. 77 remaining items

  17. mqudsi commented on Dec 26, 2017

    @mqudsi

    Thank you! This is my second forray into the github page for CoreFX (first was my PR from earlier today) and presumed the issue being here meant it was part of “core” CoreFx.

    Thanks for clarifying!

  18. JimBobSquarePants commented on Dec 27, 2017

    @JimBobSquarePants

    To answer your last question, the point of .NET Core is provide portability and extensibility to different platforms, if developers have a .NET Framework app that use System.Drawing but they don’t want to port it because they don’t gain much because it will be Windows-Only supported (the same as they have in their current app), what would be the pint of this effort vs if we support it in Unix also, they gain the portability of their app for free and is a good incentive to move to .NET Core.

    I would argue that offering System.Drawing support on Unix would offer zero incentive to move to .NET Core

  19. qmfrederik commented on Dec 27, 2017

    @qmfrederik
    Contributor

    I would argue that offering System.Drawing support on Unix would offer zero incentive to move to .NET Core

    There seems to be demand for System.Drawing on .NET Core; CoreCompat.System.Drawing, CoreCompat.System.Drawing.v2 and System.Drawing.Common combined have over 300,000 downloads on NuGet.

    To put the "giant effort to support it across all platforms" in some context: Mono has ported System.Drawing to Linux and macOS more than 10 years ago. They've also made libgdiplus compatible with ARM-based OSes such as Android and iOS. What CoreFX is doing is consolidating the Windows, Linux and macOS implementations of System.Drawing.

    At the end of the day, it's another NuGet package which is available to you, and you're free to use any imaging library which meets your requirements 😄.

  20. clrjunkie commented on Dec 27, 2017

    @clrjunkie

    There seems to be demand for System.Drawing on .NET Core; CoreCompat.System.Drawing, CoreCompat.System.Drawing.v2 and System.Drawing.Common combined have over 300,000 downloads on NuGet.

    Now that System.Drawing is out of the BCL (a good thing) I think it's important we have new basic (but quality) image manipulation support as part of the BCL, specifically for image resizing and rotation which does not mandate any external dependency.

  21. ddobric commented on Jan 22, 2018

    @ddobric
  22. danmoseley commented on Feb 2, 2018

    @danmoseley
    Contributor

    @safern is this issue still useful? We already have an issue to track marking the compat pack stable. If we want to track consolidating the Windows and Unix code further it would probably make sense to have a new more focused issue.

  23. safern commented on Feb 2, 2018

    @safern
    Member

    Agree. We can close this issue.

  24. voltcode commented on Nov 5, 2018

    @voltcode

    I was researching a way to migrate our apps toward net core with Platform Extensions and noticed that System.Drawing.ImageConverter is not present in Platform Extensions. Is it going to be suppported? Is this the best place to report such thing, or should I raise a separate issue or report it somewhere else ?

  25. benaadams commented on Nov 5, 2018

    @benaadams
    Member

    @voltcode probably should open a new issue; for tracking, if its not already in the Windows Compatibility Pack

  26. danmoseley commented on Nov 5, 2018

    @danmoseley
    Contributor

    @voltcode this would be a new issue, but actually it is already being added in dotnet/corefx#33092 which you can track.

    After that .NET Core should have everything that was in System.Drawing.dll in the System.Drawing namespace - I just checked.

  27. transferred this issue fromdotnet/corefxon Jan 31, 2020
  28. added this to the 2.1.0 milestone on Jan 31, 2020
  29. hannespreishuber commented on Jun 11, 2020

    @hannespreishuber

    doenst work with blazor Webassembly

  30. ghost locked as resolved and limited conversation to collaborators on Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Drawingdesign-discussionenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions