使用XImage.FromFile后如何删除文件?

How to delete file after using XImage.FromFile?

我正在使用 PdfSharp 基于图像生成 pdf。问题是执行此操作后,我无法删除文件并且出现以下异常:

{"The process cannot access the file because it is being used by another process."}

我发现造成这种情况的原因是我在将图像绘制到 pdf 之前使用 XImage.FromFile 函数访问图像。使用 XImage.Dispose() 也是不够的。

使用XImage.FromFile后如何删除文件?

使用 PDFsharp 1.50 beta,使用 GDI+ 构建,或修改您当前的 PDFsharp 版本(您必须使用 CacheOption = BitmapCacheOption.OnLoad 打开文件)。

更多信息:

http://forum.pdfsharp.net/viewtopic.php?p=5654#p5654