PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 11:35 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Nov 01, 2011 4:17 pm 
Offline

Joined: Tue Nov 01, 2011 3:22 pm
Posts: 1
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 02, 2011 9:19 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 04, 2012 1:49 pm 
Offline

Joined: Fri Sep 12, 2008 5:34 pm
Posts: 1
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 04, 2012 2:12 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group