无法在Linux上从磁盘文件创建位图

时间:2018-02-14 10:55:43

标签: c# .net .net-core

我引用了来自.NET Core项目的CoreCompat.System.Drawing.v2以及runtime.linux-x64.CoreCompat.System.Drawing并执行了以下操作:

Bitmap frame = new Bitmap($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\images\\image_0.bmp")

我已经检查过运行时路径是否正确。在Windows上这很好用,但在Debian上我得到了:

Unable to update the static FcBlanks: 0x0600
Unable to update the static FcBlanks: 0x0601
Unable to update the static FcBlanks: 0x0602
Unable to update the static FcBlanks: 0x0603
Unable to update the static FcBlanks: 0x06dd
Unable to update the static FcBlanks: 0x070f
Unable to update the static FcBlanks: 0x2028
Unable to update the static FcBlanks: 0x2029
Unable to update the static FcBlanks: 0xfff9
Unable to update the static FcBlanks: 0xfffa
Unable to update the static FcBlanks: 0xfffb

Unhandled Exception: System.ArgumentException: A null reference or invalid value was found [GDI+ status: InvalidParameter]
at System.Drawing.GDIPlus.CheckStatus(Status status)
at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
at TestVideoConvert.TestVideoConvert.Create() in /home/osboxes/Downloads/Test2/TestVideoConvert/TestVideoConvert.cs:line 44
at ImageResizeNetCore.Program.Main(String[] args) in /home/osboxes/Downloads/Test2/ImageResizeNetCore/Program.cs:line 15

我已安装libgdiplus,但没有运气。

使用System.Drawing.Common会出现以下错误:

Unhandled Exception: System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
at TestVideoConvert.TestVideoConvert.Create() in /home/osboxes/Downloads/Test2/TestVideoConvert/TestVideoConvert.cs:line 44
at ImageResizeNetCore.Program.Main(String[] args) in /home/osboxes/Downloads/Test2/ImageResizeNetCore/Program.cs:line 15

我在某处读过这可能是由于图像尺寸太大,但在我的情况下,这是一个1920x1080 bmp,所以并非不同寻常。

1 个答案:

答案 0 :(得分:2)

您需要为当前环境使用正确的路径分隔符

在您的情况下,您尝试使用Windows分隔符(反斜杠)连接路径。但是,Linux使用正斜杠作为其路径分隔符。

因此在Windows上,您的路径如下所示:.. \ images \ image_0.bmp

但是在Linux上,你的路径实际上就是这样:../ images / image_0.bmp

为了让您的代码在所有受支持的环境中运行,请使用Path.DirectorySeparatorChar值作为分隔符。

在这种情况下,您的代码将变为以下内容:

var delim = Path.DirectorySeparatorChar;
Bitmap frame = new Bitmap($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}{delim}images{delim}image_0.bmp");