I am using System.Drawing.Common (https://www.nuget.org/packages/System.Drawing.Common/) package in .NET Core application to create bitmap image using GDI+. When I use the same package in Xamarin.Forms application which throws PlatformNotSupported exception.
Is there any plan to support Xamarin.Forms in future?
Is there a different package for Mono's implementation (which our Unix impl came from)
@marek-safar can you please answer / move to the right place for Xamarin?
I don't think this is Xamarin issue. From the issue, I cannot tell on what platform this crashes. The Xamarin.Forms package is like any other nuget which does not explicitly target .NET Core but uses netstandard2 TFM.
@safern @maryamariyan is it expected behavior for System.Drawing.Common?
The System.Drawing.Common package should be taking the inbox xamarin implementation when targeting that framework: https://github.com/dotnet/corefx/blob/master/src/System.Drawing.Common/pkg/System.Drawing.Common.pkgproj#L10
@prasanthelvr would you mind sharing a repro project if you still hitting this issue just to make sure you're targeting the right stuff and if so, to fix it?
@safern I have shared the simple issue reproducing sample Image.zip.
Thanks, @prasanthelvr -- I took a look at the project, installed all the xamarin tools, setup Image.Android as start up project and run it. When I click in the Create Image button, which calls the netstandard library (Image) to create the image, I get:
Unhandled Exception:
System.TypeLoadException: Could not resolve type with token 01000016 (from typeref, class/assembly System.Drawing.Bitmap, System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
From looking at Image.Android project.assets.json file it seems like it is correctly looking for System.Drawing.Common implementation inbox inside the Mono.Android framework:
"MonoAndroid,Version=v8.1": {
"System.Drawing.Common/4.5.0": {
"type": "package",
"compile": {
"ref/MonoAndroid10/_._": {}
},
"runtime": {
"lib/MonoAndroid10/_._": {}
}
},
I looked at the mono gac folder and net4.7.1/facades folder and it doesn't seem to have the System.Drawing.Common facade in it, unless I'm missing something, it seems that the issue is coming from the Mono runtime. Note that the netstandard implementation of System.Drawing.Common will throw PlatformNotSupportedException in all APIs. So the right way to use this package is the way that you're using it, where you have a NETStandard library that uses this package, but don't publish that library standalone, instead you reference it from a project with a runtime (in this case Mono.Android) where it is supported.
@marek-safar would you mind taking a look and confirm it is not Xamarin's issue? Because from the package perspective and the project.assets.json file everything seems fine from our side.
I think the reporter is asking about .NET Core support as it throws PlatformNotSupported for him which you just confirmed. I have no idea when .NET Core is going to support this API.
.NET Core supports it, if you look at the package we support all this frameworks: https://github.com/dotnet/corefx/blob/master/src/System.Drawing.Common/pkg/System.Drawing.Common.pkgproj#L6
However this package is runtime specific, so for .NET Standard we throw PlatformNotSupported. So the way to use it if you have a .NETStandard library, people shouldn't publish that library as a standalone library and then reference the assembly directly, if they do that, they will get the .NET Standard runtime specific assembly which throws PlatformNotSupported. Instead the right way to reference this library from a project targeting a supported runtime (.NET Core 2.0 or greater, .NET 4.6.1, Xamarin) is using ProjectReference or PackageReference (if a package for the .NET Standard library is built). That way our tools will restore and publish the runtime specific assembly and will not get the NotSupported assembly.
In this case as I mentioned in my previous comment, @prasanthelvr is doing the right thing, the project.assets.json is pointing to the right asset for MonoAndroid (to the inbox implementation), and I was not able to repro the PlatformNotSupported exception when running the Android project directly, but instead I got:
Unhandled Exception:
System.TypeLoadException: Could not resolve type with token 01000016 (from typeref, class/assembly System.Drawing.Bitmap, System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
And then looked at the Mono gac and the System.Drawing.Common is not there.
Seeing a variation of this issue as well, when taking my netstandard2.0 assembly to Xamarin/Android.
Looking for a workaround to get my Android project running.
not resolve type with token 01000016 (from typeref, class/assembly System.Drawing.Bitmap, System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
I鈥檓 getting the same error with Xamarin forms project. From the shared code - which is .net standard not PCL it throws the message above.
FYI I ended up abstracting my GDI drawing primitives, and using SkiaSharp to implement draw layer for Android.
What I was specifically interested in was Path and Region fills and algebra. I get good performance from Skia compared to GDI, except Skia did not scale well for polygons with many (>30) points, in my specific test case.
Skia requires a C++ layer, you can get the NuGet package or build the source code yourself (which I did). My challenge is for a netstandard AnyCpu build to pick the right C++ library to interop to, as it's not known at compile time if you will run in 32 or 64 bit mode (same problem on Windows and Android).
Most helpful comment
Seeing a variation of this issue as well, when taking my netstandard2.0 assembly to Xamarin/Android.
Looking for a workaround to get my Android project running.