C# net6.0平台使用System.Drawing.Common包,运行报错。 -- 解决方案

背景

在windows平台下,创建net6.0的C#程序,引用NuGet包:System.Drawing.Common,运行代码Image image = new Bitmap(2000, 1024)会报错:“System.Drawing.Common is not supported on this platform.”。如下截图:
在这里插入图片描述

网上或者官方搜索到的答案:

  • 网上搜索到:引入本地System.Drawing.Common的dll。虽然问题解决了,但根本原因没有找到。再说了,写C#程序非必要情况,应该都会直接在NuGet上下载,不会去找一个路径,再去添加引用。
  • 官方解释的是:在非windows平台下使用System.Drawing.Common的解决方案。但我这里是windows平台。
    这个问题,目前在网上搜了一圈,没有对应的解决方案,deepseek也不行。相信大家遇到这个问题,在网上应该也没有查到相应的解决方案吧。

在此,我把解决这个问题的解决方案分享出来,供大家参考:

  • 方案一(不建议使用):降平台版本,net6.0降到framework 4.8版本。
    但这种方案需要重新建一个framework 4.8版本的工程,而且framework平台下CommunityToolkit.Mvvm的Fody功能用不了,太难受了。
  • 方案二:NuGet下载System.Drawing.Common的版本不能高于9.0.0。这也是各种踩坑各种验证找到的。
    在这里插入图片描述
    我对比了下9.x.x版本与8.x.x版本的区别,发现8.x.x版本多了个传递包:Microsoft.Win32.SystemEvents。
    在这里插入图片描述
运行获取系统配置信息的应用程序时报错: 应用程序中发生了未经处理的异常 未将对象引用设置到对象的实例 有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾。 ************** 异常文本 ************** System.NullReferenceException: 未将对象引用设置到对象的实例。 在 SystemInfo.SystemInfo.GetBIOSInfoEx() 在 SystemInfo.SystemInfo.SystemInfo_Load(Object sender, EventArgs e) 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ContainerControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** 已加载的程序集 ************** mscorlib 程序集版本:4.0.0.0 Win32 版本:4.8.9290.0 built by: NET481REL1LAST_C 基本代码:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll ---------------------------------------- SystemInfo 程序集版本:1.0.0.0 Win32 版本:1.0.0.0 基本代码:file:///C:/SMT_V1.7_250426/SystemInformation/SystemInfo.exe ---------------------------------------- System.Windows.Forms 程序集版本:4.0.0.0 Win32 版本:4.8.9256.0 built by: NET481REL1LAST_B 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System 程序集版本:4.0.0.0 Win32 版本:4.8.9282.0 built by: NET481REL1LAST_C 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- System.Drawing 程序集版本:4.0.0.0 Win32 版本:4.8.9032.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System.Configuration 程序集版本:4.0.0.0 Win32 版本:4.8.9032.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------- System.Xml 程序集版本:4.0.0.0 Win32 版本:4.8.9032.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll ---------------------------------------- Accessibility 程序集版本:4.0.0.0 Win32 版本:4.8.9032.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll ---------------------------------------- Common 程序集版本:1.0.0.0 Win32 版本:1.0.0.0 基本代码:file:///C:/SMT_V1.7_250426/SystemInformation/Common.DLL ---------------------------------------- mscorlib.resources 程序集版本:4.0.0.0 Win32 版本:4.8.9032.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll ---------------------------------------- System.Windows.Forms.resources 程序集版本:4.0.0.0 Win32 版本:4.8.9032.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll ---------------------------------------- ************** JIT 调试 ************** 要启用实时(JIT)调试, 该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置 jitDebugging 值。 编译应用程序时还必须启用 调试。 例如: <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 启用 JIT 调试后,任何未经处理的异常 都将被发送到在此计算机上注册的 JIT 调试程序, 而不是由此对话框处理。
最新发布
05-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值