简体   繁体   中英

Passing System.Drawing.Common.Bitmap (.NET Standard 2.0) to class expecting System.Dwawing.Bitmap (.NET 4.7.2)

I am wokring on converting some class libraries from .NET 4.7.2 over to .NET Standard (so that other Standard/Core assemblies can better use them), and I'm running into a problem with a particular third party library. This library performs image processing, and many of it's classes' methods take Image or Bitmap objects from the classic System.Drawing namespace.

I have found the .NET Standard package System.Drawing.Common that has the compatibility classes in it, so that my own uses of those classes in my source code is working fine. But when I try to pass one of these classes into this library, I get an error:

Reference to type 'Bitmap' claims it is defined in 'System.Drawing', but it could not be found

I think I understand what's going on here (the types are defined in assemblies with different names, so they are different types). But is there any way to make these work as-is? Or will it require the vendor of the third-party library to make changes on their side to support .NET Standard?

System.Drawing is not part of .NET Standard so the types are not compatible.

Additionally, I wouldn't use System.Drawing.Common as it's just a wrapper around GDI+ which doesn't work well in multi-threaded environments like server side code.

I would recommend moving to ImageSharp or SkiaSharp , both of which should work in all of your projects.

I had the same problem and I was able to call the methods using the dark magic of reflection. I didn't believe it would work, but it did (at least it seems to):

var bmp = new Bitmap(1, 1);
var assembly = Assembly.GetAssembly(typeof(ImageElement));
var elem = (ImageElement)assembly.CreateInstance("Winnovative.ImageElement", true, BindingFlags.Default, null, new object[] { 0f, 0f, 1f, 1f, bmp }, null, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM