Hi there,
my company uses PDFSharp to render PDFs with images. The images are added to the PDF while creating a table by using the MigraDoc classes from the PDFSharp library. Also these images are created on the fly in a temporary folder which needs to be cleared afterwards. The issue here is that initiating the render process results in calling the following code which is responsible for the file lock:
Code:
namespace PdfSharp.Drawing
{
...
XImage(string path)
{
...
#if WPF && !SILVERLIGHT
//BitmapSource.Create()
// BUG: BitmapImage locks the file
this.wpfImage = new BitmapImage(new Uri(path)); // AGHACK
#endif
The file lock can be prevented by changing this code section to
Code:
namespace PdfSharp.Drawing
{
...
XImage(string path)
{
...
#if WPF && !SILVERLIGHT
// read image into stream to avoid locking the file
// see http://tedshelloworld.blogspot.de/2010/11/bitmaps-in-wpf-disposing-of-bitmaps-in.html
BitmapImage image = new BitmapImage();
using (FileStream stream = File.OpenRead(path))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
this.wpfImage = image;
#endif
I've tested PDFSharp with the code change and the file lock is gone. Credits go to the author behind the link mentioned in the comments.
Please include this bug fix in the next release.
So lonG