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

PdfSharpWPF: XImage.Dispose() -don't release the source file
http://forum.pdfsharp.com/viewtopic.php?f=3&t=1832
Page 1 of 1

Author:  Tinia [ Tue Nov 01, 2011 4:17 pm ]
Post subject:  PdfSharpWPF: XImage.Dispose() -don't release the source file

PdfSharp WPF - version 1.31.1789.0

When using the XImage object. The loaded file is not released by Dispose() or Close() method. It's not released for garbage collection.
I tried adding a System.Threading.Thread.Sleep(300); to give the garbage an extra chance, but no improvement. The file is not released before I terminate the program.

Code:
//C# CODE
PdfSharp.Drawing.XImage _xImg = PdfSharp.Drawing.XImage.FromFile(@"C:\myTestImg.jpg");
_xImg.Memory.Close();
_xImg.Memory.Dispose();
_xImg.Dispose();

File.Delete(@"C:\myTestImg.jpg"); // this is where the exception accoures: IOException "The process cannot access the file 'C:\myTestImg.jpg' because it is being used by another process."


Any way to work around this?

/Kind Regards
Jeanette T.

Author:  Thomas Hoevel [ Wed Nov 02, 2011 9:19 am ]
Post subject:  Re: PdfSharpWPF: XImage.Dispose() -don't release the source file

Tinia wrote:
Any way to work around this?
Use the GDI+ build.
Or read the file into a memory stream and use XImage.FromStream().

We use Windows routines to open the files - and as far as I can tell Windows keeps the files locked.

You don't need "Sleep()", you can force a garbage collection:
Code:
GC.Collect();
GC.WaitForPendingFinalizers();


Our code calls:
Code:
wpfImage = new BitmapImage(new Uri(path));

I don't see a "Close()" or "Dispose()" or anything similar with class BitmapImage.

Author:  ZedGuy [ Wed Apr 04, 2012 1:49 pm ]
Post subject:  Re: PdfSharpWPF: XImage.Dispose() -don't release the source file

I had a similar problem and was able to fix it by making a change in the constructor of XImage

Current code
Code:
this.wpfImage = new BitmapImage(new Uri(path));  // AGHACK


Replacement code
Code:
 BitmapImage temp;
 temp = new BitmapImage();
 temp.BeginInit();
 temp.CacheOption = BitmapCacheOption.OnLoad;
 temp.UriSource = new Uri(path);
 temp.EndInit();
 this.wpfImage = temp;


I found this fix for BitmapImage on stackoverflow (http://stackoverflow.com/questions/6430 ... -lock-file)

Also, I'm not sure if this is a belt and suspenders type thing, but when I create an XImage I do it in a using block

Code:
Using image As XImage = XImage.FromFile("C:\temp\locked_image.jpg")
  imageHeight = pageWidth / image.PixelWidth * image.PixelHeight
  gfx.DrawImage(image, marginLeft, marginTop, pageWidth, imageHeight)
End Using

Author:  Thomas Hoevel [ Wed Apr 04, 2012 2:12 pm ]
Post subject:  Re: PdfSharpWPF: XImage.Dispose() -don't release the source file

Changing the code from
Code:
this.wpfImage = new BitmapImage(new Uri(path));


to
Code:
this.wpfImage = new BitmapImage(new Uri(path), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);


should have the same effect.

Anyway, "BitmapCacheOption.OnLoad" should be the solution to the locking problem.
ZedGuy, thank you very much for your feedback.

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