PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Nov 14, 2014 4:55 pm 
Offline

Joined: Fri Nov 14, 2014 12:38 pm
Posts: 2
Good afternoon

I noticed a handled exception today in my WPF project which was slowing down my batch PDF creation process. The exception occurs in PdfSharp.Drawing.XImage.InitializeGdiHelper when the XImage is initialised with a BitmapSource created from a MemoryStream. As there is no file on disk the line new System.Drawing.Bitmap(GetImageFilename(wpfImage)) throws an ArgumentException, this is handled silently but incurs a performance cost. The below illustrates the current workaround I have implemented to avoid this exception, I expect you'll prefer to determine the appropriate values for the isJpeg and isCmyk vars rather than skip this block of code completely but I'll include the workaround in case it is of any use

Code:
    private void InitializeGdiHelper()
    {
      if (!isCmyk.HasValue)
      {
        try
        {
            string imageFileName = GetImageFilename(wpfImage);
            if (!String.IsNullOrEmpty(imageFileName) && new System.IO.FileInfo(imageFileName).Exists)
            {
                using (System.Drawing.Image image = new System.Drawing.Bitmap(imageFileName))
                {
                    string guid = image.RawFormat.Guid.ToString("B").ToUpper();
                    isJpeg = guid == "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
                    isCmyk = (image.Flags & ((int)System.Drawing.Imaging.ImageFlags.ColorSpaceCmyk | (int)System.Drawing.Imaging.ImageFlags.ColorSpaceYcck)) != 0;
                    if (isJpeg.Value)
                    {
                        memory = new MemoryStream();
                        image.Save(memory, System.Drawing.Imaging.ImageFormat.Jpeg);
                        if ((int)memory.Length == 0)
                        {
                            memory = null;
                        }
                    }
                }
            }
        }
        catch { }
      }
    }



Kind Regards, and thanks very much for all the work you've put into this excellent project
Nick


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 17, 2014 3:47 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi, Nick,

Thanks for the feedback.

Is there any advantage in using "new System.IO.FileInfo(imageFileName).Exists" instead of "File.Exists(imageFilename)"?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 24, 2014 12:03 pm 
Offline

Joined: Fri Nov 14, 2014 12:38 pm
Posts: 2
Hi Thomas

None whatsoever, that's the result of me making several edits to the code looking for a better fix and not reviewing it before submitting this item, File.Exists is more sensible


Nick


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 35 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