简体   繁体   中英

C# dotnet6 Azure Function App System.InvalidCastException when referencing a System.Drawing.Bitmap

I have the following issue with a do.net 6 Azure Function App when using the v6 System.Drawing.Common package.

I'm getting the following error when referencing a bitmap, just wondering if anyone knows any work arounds for this.

2022-03-23T20:14:29.255 [Error] System.InvalidCastException: 
[A]System.Drawing.Bitmap cannot be cast to [B]System.Drawing.Bitmap. 
Type A originates from 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 
in the context 'Default' 
at location 'D:\Program Files (x86)\SiteExtensions\Functions\4.1.3\32bit\System.Drawing.Common.dll'. 
Type B originates from 
'System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' in the context 'Default' at location
'D:\\home\\site\\wwwroot\\bin\\runtimes/win/lib/net6.0/System.Drawing.Common.dll

Obviously some issues with versioning going on here, but this is happening on an Azure Function App deployed with the .NET 6 runtime. I wonder why it's trying to cast it down to the v4.0.2 DLL when I never even reference that DLL anywhere in the project.

Anyone any ideas or potential work arounds for this?

I finally resolved this issue.

I had an embedded Bitmap image in a resource file, when I tried to use that image from the resource file, it was returned back as a Version=4.0.2.0 bitmap.

I'm in a .NET 6 Windows Function App, so it was referencing the v6 version of System.Drawing.Common, Version=6.0.0.0.

I changed the embedded image to be a byte steam, and could create a V6 Bitmap directly from that.

So it would appear that Azure Functions under the hood is using different libraries for embedded resources than the main environment would expect.

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