Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Drawing.Common is not supported on non-Windows platforms #80148

Closed
qcjxberin opened this issue Aug 14, 2021 · 25 comments
Closed

System.Drawing.Common is not supported on non-Windows platforms #80148

qcjxberin opened this issue Aug 14, 2021 · 25 comments
Labels
area-System.Drawing documentation Documentation bug or enhancement, does not impact product or test code

Comments

@qcjxberin
Copy link

System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
at System.Drawing.LibraryResolver.EnsureRegistered() in System.Drawing.Common.dll:token 0x600058c+0x11
at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize() in System.Drawing.Common.dll:token 0x6000b00+0x0
at System.Drawing.SafeNativeMethods.Gdip..cctor() in System.Drawing.Common.dll:token 0x6000af8+0x2b

https://aka.ms/systemdrawingnonwindows can't be open,How to solve this problem

@svick
Copy link
Contributor

svick commented Aug 14, 2021

The exception message is correct: System.Drawing.Common is no longer supported on .Net 6 on Linux and macOS. For more information, see this document and dotnet/docs#25257.

As that document states, if you want to continue using it on .Net 6, you can provide a runtime config switch named System.Drawing.EnableUnixSupport set to true. But even this will not work starting with .Net 7.

As for the aka.ms link, I'm not sure. Maybe it will be set up once this breaking change is properly documented (i.e. when dotnet/docs#25367 is merged).

@mairaw
Copy link
Contributor

mairaw commented Aug 14, 2021

It seems @svick has answered your question, so I’m closing this issue. Let me know if I missed anything.

@mairaw mairaw closed this as completed Aug 14, 2021
@mairaw mairaw reopened this Aug 14, 2021
@mairaw
Copy link
Contributor

mairaw commented Aug 14, 2021

Reopening since the link goes to our internal tool somehow.
Tagging @ericstj @danmoseley @terrajobst @safern who seem to own that link.

@safern
Copy link
Member

safern commented Aug 14, 2021

Tagging @ericstj @danmoseley @terrajobst @safern who seem to own that link.

I'll work with @gewarren to post the doc ASAP on Monday and point the url to the doc.

@gewarren
Copy link
Contributor

@safern Here is the PR; it's ready for review: dotnet/docs#25367

@safern
Copy link
Member

safern commented Aug 16, 2021

https://aka.ms/systemdrawingnonwindows should now resolve to the published docs. Thanks for the report.

@safern safern closed this as completed Aug 16, 2021
@mairaw mairaw added the documentation Documentation bug or enhancement, does not impact product or test code label Aug 16, 2021
@janseris
Copy link

janseris commented Mar 16, 2022

What is the solution for this for developers?
I have a web server application running on Linux and I need to know what replaces System.Drawing for .NET 6 and later. I read in the documentation that

recommended alternatives include ImageSharp, SkiaSharp, Windows Imaging Components, and Microsoft.Maui.Graphics.

So now developers will use multiple libraries and .NET will have no standard image manipulation API at all?
Which one of these is the right one? For which one Microsoft can guarantee support for the next years? What are we supposed to do when these random package authors change license or drop support?
So now when people will want to learn .NET image manipulation, there will be package-specific tutorials?
MAUI image API support might not be dropped but instead, it is a UI library so depending on MAUI on an ASP.NET server application which does not relate to MAUI in any way is ridiculous.
What is the 1 and only suggested solution which will be supported by Microsoft just as .NET 6 is (LTS) etc.?

What is the suggested solution for Blazor WebAssembly projects needing to resize image before uploading to server? MAUI graphics API?

Thanks

@danmoseley
Copy link
Member

@mattleibow can you confirm that SkiaSharp is fully supported by Microsoft?

Also, I have generally thought of SkiaSharp as client focused, and something like ImageSharp better for servers. Is it reasonable to use SkiaSharp on the server side? I know that unlike ImageSharp, it has some native dependencies.

@siddjain
Copy link

I am using System.Drawing.Common on Mac OS but with target framework 6.0 and have set

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    },
"configProperties": {
      "System.Drawing.EnableUnixSupport": true
   }
  }
}

but still seeing this exception:

Unhandled exception. System.PlatformNotSupportedException: System.Drawing.Common is not supported on this platform.
   at System.Drawing.SystemFonts.get_DefaultFont()

can anyone help please?

@gewarren
Copy link
Contributor

@siddjain Which file are those settings from?

@siddjain
Copy link

the settings are from myapplication.runtimeconfig.json under bin/Debug/net6.0-windows of the project root. I am trying to run the application from the project root using dotnet run. Thanks for your help.

@gewarren
Copy link
Contributor

What does your project file look like? I'm surprised the output folder is net6.0-windows on macOS.

@siddjain
Copy link

its because I have set it so. setting it to net6.0-macos gave me another set of issues. here is the csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <RootNamespace>MyApp</RootNamespace>
    <AssemblyName>MyApp</AssemblyName>    
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CommandLineParser" Version="2.9.1" />
    <PackageReference Include="System.Drawing.Common" Version="7.0.0" />
  </ItemGroup>
</Project>

@gewarren
Copy link
Contributor

Looks like you're using the 7.0 version of System.Drawing.Common. Does it work if you set that to 6.0.0?

@siddjain
Copy link

no it still doesn't work

Unhandled exception. System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
 ---> System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
   at System.Drawing.LibraryResolver.EnsureRegistered()
   at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
   at System.Drawing.SafeNativeMethods.Gdip..cctor()
   --- End of inner exception stack trace ---
   at System.Drawing.SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(IntPtr& fontfamily)
   at System.Drawing.FontFamily.GetGdipGenericSansSerif()
   at System.Drawing.FontFamily.get_GenericSansSerif()
   at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)
   at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
   at System.Drawing.Font..ctor(String familyName, Single emSize, String systemName)
   at System.Drawing.SystemFonts.get_DefaultFont()

@siddjain
Copy link

made some changes and that error is fixed but I get a new one now:

Unhandled exception. System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'libgdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(liblibgdiplus, 0x0001): tried: 'liblibgdiplus' (no such file), '/System/Volumes/Preboot/Cryptexes/OSliblibgdiplus' (no such file), '/usr/lib/liblibgdiplus' (no such file, not in dyld cache), 'liblibgdiplus' (no such file), '/usr/local/lib/liblibgdiplus' (no such file), '/usr/lib/liblibgdiplus' (no such file, not in dyld cache)
   at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)
   at System.Drawing.SafeNativeMethods.Gdip..cctor()

how do I fix this one?

@safern
Copy link
Member

safern commented Jan 3, 2023

@siddjain for that specific error you need to make sure that libgdiplus is installed in your OS.

https://www.mono-project.com/docs/gui/libgdiplus/

In Unix systems you can do it by running sudo apt-get install libgdiplus or whatever is the package manager for that OS.

on mac systems you need to run brew install mono-libgddiplus

Note that using 6.0.0 means that you'd be out of support so the recommended guidelines from the .NET team is to move out of depending on System.Drawing.Common for non windows.

@danmoseley
Copy link
Member

@mattleibow just checking you saw my question above as it would be good to know the answer.

@danmoseley danmoseley transferred this issue from dotnet/core Jan 3, 2023
@danmoseley
Copy link
Member

@siddjain do you have to use System.Drawing? As mentioned, it is out of support now.

@siddjain
Copy link

siddjain commented Jan 4, 2023

Thanks all for the help. re: do you have to use System.Drawing? to answer literally, its a loaded question. One can always port the code to another library. In fact, I already did it for one of my GDI+ apps as I wanted to learn Swift but to my surprise the C# code runs much faster than Swift!

Also what I understand when someone says the library is out of support is that no one is fixing bugs and issues but anyone is welcome to use it as-is and it will continue to be available and downloadable from the internet. is that correct?

@danmoseley
Copy link
Member

Also what I understand when someone says the library is out of support is that no one is fixing bugs and issues but anyone is welcome to use it as-is and it will continue to be available and downloadable from the internet. is that correct?

System.Drawing is not going anywhere, but the code inside it that uses libgdiplus has been removed, which means that it will only work on Windows now.

.NET 6 will go out of support in due course, although you'll still be able to download it if you don't mind being out of support.

In short, I recommend you don't write code against System.Drawing unless it's only intended to run on Windows. The only exception might be if it's just a temporary thing, in which case you can use .NET 6. But generally you should pick another library to do this stuff unless you're Windows only.

@siddjain
Copy link

siddjain commented Jan 4, 2023

Thanks Dan for the clarification!

@janseris
Copy link

janseris commented Jan 4, 2023

@siddjain

one can always port the code to another library

This part is not magically for free you know especially with large codebase with little automatic tests

@siddjain
Copy link

siddjain commented Jan 4, 2023 via email

@Rombersoft
Copy link

Reading it all I came to the conclusion that I learned not the right programming language. It was necessary to learn Java 10 years ago when C# finished with such valuable problems. Microsoft is a great company but it is not able to make support graphics basics for three OS. I will keep silent about cross-platformed desktop GUI, they are missing. Miguel De Icaza come back. If you advise migrating to MAUI or Skia, maybe will you write a wrapper for it inside Linux System.Drawing implementation?

@ghost ghost locked as resolved and limited conversation to collaborators Feb 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Drawing documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

No branches or pull requests

9 participants