Azure Function gives error: System.Drawing is not supported on this platform
Asked Answered
O

2

20

(If this question is poorly worded, could someone please help me clear it up?)

I have an Azure Function (2.0) which relies on some System.Drawing code. I've added a NuGet reference to System.Drawing.Common (4.5.0).

After publishing the app, however, when the function is called, it produces the error:

System.Private.CoreLib: Exception while executing function: [MyFunctionName]. System.Drawing.Common: System.Drawing is not supported on this platform.

As far as I'm aware, System.Drawing.Common is supported on .NET Core now, which I believe is the environment in which my Azure Function is running. The actual project is a .NET Standard 2.0 project, though.

I am confused as to how to resolve this. I've tried converting the project to a .NET Core 2.1 project but that led to bizarre errors related to "Metadata generation failed" and an inability to find System.Runtime.

My project references Microsoft.Azure.WebJobs.Extensions.EventGrid (2.0.0-beta2) if that's relevant.

Overhand answered 17/8, 2018 at 23:59 Comment(0)
T
26

It's not about the CLR, it's about the sandbox.

System.Drawing relies heavily on GDI/GDI+ to do its thing. Because of the somewhat risky nature of those APIs (large attack surface) they are restricted in the App Service sandbox.

Win32k.sys (User32/GDI32) Restrictions

For the sake of radical attack surface area reduction, the sandbox prevents almost all of the Win32k.sys APIs from being called, which practically means that most of User32/GDI32 system calls are blocked. For most applications this is not an issue since most Azure Web Apps do not require access to Windows UI functionality (they are web applications after all).

Try to find an different library that doesn't rely on System.Drawing/GDI, like ImageSharp.

Torhert answered 18/8, 2018 at 9:7 Comment(1)
I had some fundamental misunderstandings about what goes on behind the scenes. As far as I was aware, if you could reference a NuGet package, you could use whatever APIs it offers (and they would "just work"). I've now come to understand that the System.Drawing.Common NuGet package allows these drawing APIs to work provided that you're on a platform that supports it, but will fail with a runtime exception if you're not. I was also confused because I knew my Azure Function was running on Windows, but as you pointed out, the sandbox environment limits GDI+ usage. Thank you!Overhand
S
6

A little update can help a lot of people. Now you can switch your Azure Function to v3: in Function app settings panel -> Runtime version ~3

With this setup, your code run in a sandbox that support Core 3, (you don't need to rebuild your dll to Core3, i run my .net core 2.1 dll without errors), and surprise... you don't get this exception anymore:

System.Drawing.Common: System.Drawing is not supported on this platform.

Because the CLI can found GDI/GDI+ that need in the subsystem. Now i can convert html to pdf without go crazy.

Slovakia answered 20/3, 2020 at 8:2 Comment(3)
Using System.Drawing.Common, how to convert html to pdf? One can do it with something like puppeteer, but then one does not need System.Drawing.Common as well.Vinic
You're a lifesaver mate!Restful
I am using Runtime Version 4.0 and I get the error with .NET6. Can I fix it by using an .NET6 Isolated. Or do I need to downgrade?Gibbon

© 2022 - 2024 — McMap. All rights reserved.