[英]System.Drawing.Common not working on Windows
I'm trying to run a simple C# Application on windows 11. I'm using the System.Drawing to edit a png file, but this error is occuring.我正在尝试在 windows 11 上运行一个简单的 C# 应用程序。我正在使用 System.Drawing 编辑 png 文件,但出现了此错误。
System.PlatformNotSupportedException: 'System.Drawing.Common is not supported on this platform.' System.PlatformNotSupportedException:“此平台不支持 System.Drawing.Common。”
After a bit of research, turns out System.Drawing is only supported on windows.. but I am using windows.. Perhaps it's because I'm using windows 11?经过一番研究,原来 System.Drawing 仅在 windows 上受支持。但我使用的是 windows。可能是因为我使用的是 windows 11?
Furthermore, adding此外,添加
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
Didn't seem to fix this issue either似乎也没有解决这个问题
The error is thrown on this line of code:在这行代码中抛出错误:
Bitmap bmp = new Bitmap(Bitmap.FromFile(file));
You must build your project for windows. You do this by adding -windows
to your current Target Framework Moniker (TFM) in the project file:您必须为 windows 构建项目。您可以通过将-windows
添加到项目文件中当前的目标框架名字对象 (TFM)来执行此操作:
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
...
</PropertyGroup>
This tells the compiler that you will be running your application exclusively on windows.这告诉编译器您将在 windows 上独占运行您的应用程序。
If you are using winforms, also add如果您使用的是 winforms,还要添加
<UseWindowsForms>true</UseWindowsForms>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.