为什么位图的路径参数在 Ubuntu Server 20.04 上抛出 "ArgumentException: Parameter is not valid"?

Why does the path parameter for Bitmap throw "ArgumentException: Parameter is not valid" on Ubuntu Server 20.04?

我决定让我的 Discord 机器人 运行 在我的新 Ubuntu 20.04 家庭服务器上。

机器人在服务器上执行 运行,但是当我尝试从路径创建 Bitmap 时出现以下错误:

System.ArgumentException: Parameter is not valid.
   at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)
   at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
   at Aurelia.ImageProcessor.ImageBuilder(String filename, String idolname, String group, String rarity, Int32 rar, String id, String pathDefiner) in /home/aurelia/discordBots/Aurelia/AureliaBot/Aurelia/ImageProcessor.cs:line 21

如果我在家里 Windows PC 上的 VS 中调试它,我不会收到此错误并且工作正常。

我检查了每个参数,每个参数都应该没问题。

Bitmap cardTemp = new Bitmap($"assets\groups\{group}\{idolname}\{filename}");
Bitmap card = new Bitmap(cardTemp, 800, 1200);
Bitmap rareFrameTemp = new Bitmap($"assets\frames\{rarity}Frame.png");

只有第一行报错,是的,文件存在。 谢谢!

问题是您的路径虽然适用于 Windows,但不适用于基于 Unix 的系统,因为它们使用正斜杠 - / - 而不是反斜杠 \目录分隔符。

使用 Path.Combine 作为跨平台解决方案。

它将使用 Path.DirectorySeparatorChar 来提供特定于平台的字符:

var cardTempPath = Path.Combine("assets", "groups", group, idolname, filename);
var rareFrameTemp = Path.Combine("assets", "frames", $"{rarity}Frame.png");

Bitmap cardTemp = new Bitmap(cardTempPath);
Bitmap card = new Bitmap(cardTemp, 800, 1200);
Bitmap rareFrameTemp = new Bitmap(rareFrameTemp);