PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Image file lock after rendering a document with MigraDoc
http://forum.pdfsharp.com/viewtopic.php?f=3&t=2995
Page 1 of 1

Author:  ViRuSTriNiTy [ Fri Nov 28, 2014 1:59 pm ]
Post subject:  Image file lock after rendering a document with MigraDoc

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

Author:  Thomas Hoevel [ Mon Dec 01, 2014 9:24 am ]
Post subject:  Re: Image file lock after rendering a document with MigraDoc

This has been changed in current internal builds of PDFsharp.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/