.NET Core 6 Linux环境下Aspose.Slides生成缩略图失败求助
问题:Linux下.NET Core 6运行Aspose.Slides出现平台不支持异常
异常信息
System.TypeInitializationException : 'Gdip'的类型初始值设定项引发异常。
---- System.PlatformNotSupportedException : System.Drawing.Common在非Windows平台不受支持。
堆栈跟踪:
at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, IntPtr scan0, IntPtr& bitmap)
测试代码
var pres = new Presentation(fn); for (var page = 0; page < pres.Slides.Count; page++) { ISlide slide = pres.Slides[page]; slide.GetThumbnail(); // 修正原代码语法错误 }
已引用的NuGet包
<PackageReference Include="System.Drawing.Common" Version="6.0.0" /> <PackageReference Include="Aspose.Cells" Version="22.8.0" /> <PackageReference Include="Aspose.Email" Version="22.7.0" /> <PackageReference Include="Aspose.Imaging" Version="22.8.0" /> <PackageReference Include="Aspose.PDF" Version="22.7.0" /> <PackageReference Include="Aspose.Slides.NET" Version="22.7.0" /> <PackageReference Include="Aspose.Words" Version="22.8.0" /> <PackageReference Include="BitMiracle.LibTiff.NET" Version="2.4.649" />
解决方案
方案1:启用System.Drawing.Common的Linux支持(.NET 6+需手动配置)
.NET 6开始默认禁用System.Drawing.Common在非Windows平台的支持,需通过运行时开关启用:
- 方式一:修改runtimeconfig.json
在项目输出目录的[项目名].runtimeconfig.json中添加配置:{ "runtimeOptions": { "configProperties": { "System.Drawing.EnableUnixSupport": true } } } - 方式二:在.csproj中配置
直接在项目文件里添加运行时配置:<PropertyGroup> <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" /> </PropertyGroup> - 同时安装Linux系统依赖库(以Debian/Ubuntu为例):
sudo apt-get update && sudo apt-get install -y libgdiplus libc6-dev
方案2:使用Aspose.Slides的SkiaSharp跨平台后端(推荐)
Aspose.Slides 21.9及以上版本支持基于SkiaSharp的无依赖渲染,完全避开System.Drawing.Common:
- 安装NuGet包:
Aspose.Slides.SkiaSharp - 修改代码使用SkiaSharp渲染选项:
using Aspose.Slides; using Aspose.Slides.Export; var pres = new Presentation(fn); var renderingOptions = new RenderingOptions(); renderingOptions.SkiaSharpOptions = new SkiaSharpOptions(); for (var page = 0; page < pres.Slides.Count; page++) { ISlide slide = pres.Slides[page]; // 生成缩略图,返回SkiaSharp的SKBitmap var skBitmap = slide.GetThumbnail(renderingOptions); // 直接用SkiaSharp保存文件 skBitmap.Save($"{_currentDir}/output/{origfn}_{page + 1}.tiff", SkiaSharp.SKEncodedImageFormat.Tiff, 100); }
方案3:升级Aspose.Slides到最新版本
旧版本Aspose.Slides对Linux平台适配不完善,升级到最新版可能直接解决问题,同时新版本默认优先使用跨平台API。
内容的提问来源于stack exchange,提问作者maxfridbe
相关产品推荐
相关产品推荐

