PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

XImage compatible formats? DrawImage makes a black mess...

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
cmosses



Joined: 05 Oct 2007
Posts: 6

PostPosted: Wed Nov 21, 2007 8:34 am    Post subject: XImage compatible formats? DrawImage makes a black mess... Reply with quote

I am having trouble drawing images to a PDF. The images are uploaded by users of a website, so I can't just use a format that I know works.

I retrieve the images as bytes from a database and place them in Image objects. I can save these images to disk and open the files with a viewer, and everything works fine when I do this.

I then create an XImage using XImage.FromGdiPlusImage and use XGraphics.DrawImage to draw it to the PDF. When I open the PDF, sometimes I get a black image with noise where the image should have been. This happens with JPG files. I have tried to convert such a file to TIFF, and in this case the image appears fine. I have tried using Photoshop to save the JPG in a very basic and compatible way, but that didn't help.

Has anyone else experienced this and found a solution? Is there documentation which specifies which formats are acceptable to XImage? If not, any ideas for a workaround?

Thanks in advance Smile
Back to top
View user's profile Send private message
Thomas Hoevel



Joined: 16 Oct 2006
Posts: 387
Location: Cologne, Germany

PostPosted: Wed Nov 21, 2007 9:15 am    Post subject: Reply with quote

JPEG files are simply embedded in PDF - PDFsharp only sets some flags.
There's a known bug with CMYK JPEG and greyscale JPEG images in PDFsharp 1.00 - PDFsharp 1.00 sets incorrect flags for those.

In PdfImage.cs, method "InitializeJpeg()" you need this code:
Code:
      Elements[Keys.Length] = new PdfInteger(streamLength);
      Elements[Keys.Filter] = new PdfName("/DCTDecode");
      Elements[Keys.Width] = new PdfInteger(image.Width);
      Elements[Keys.Height] = new PdfInteger(image.Height);
      Elements[Keys.BitsPerComponent] = new PdfInteger(8);
      if ((image.gdiImage.Flags & ((int)ImageFlags.ColorSpaceCmyk | (int)ImageFlags.ColorSpaceYcck)) != 0)
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");
        if ((image.gdiImage.Flags & (int)ImageFlags.ColorSpaceYcck) != 0)
          Elements["/Decode"] = new PdfLiteral("[1 0 1 0 1 0 1 0]");  // Invert colors? Why??
      }
      else if ((image.gdiImage.Flags & (int)ImageFlags.ColorSpaceGray) != 0)
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceGray");
      }
      else
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB");
      }

If the problem persists, please send us sample files.
_________________
Regards
Thomas Hoevel
PDFsharp Team
Back to top
View user's profile Send private message Visit poster's website
cmosses



Joined: 05 Oct 2007
Posts: 6

PostPosted: Wed Nov 21, 2007 9:29 am    Post subject: Reply with quote

Thanks for that, very helpful.

If someone reading this is not interested in making changes to the PDFsharp code, then a possible workaround is to convert all images to TIFF on the fly, before inserting them in the PDF. The NET Image class will let you do this with the Save method, specifying TIFF as the output format and reading the XImage from that file. Of course that means disk IO which is not always possible, but there you have it.

Cheers
Back to top
View user's profile Send private message
edmicman



Joined: 04 Mar 2008
Posts: 8

PostPosted: Wed Mar 05, 2008 8:41 pm    Post subject: Reply with quote

Ahhh, I'm having this same problem - grayscale scanned JPGs are showing up distorted and messed up. Thanks for the tip - I'll try and decide if I want to mess with the IO of converting (can you do that in memory??), or whether it'd just be easier to modify the PDFSharp code.

If the source can be changed, is there any chance of a fix or patch that handles any image better?
Back to top
View user's profile Send private message
Thomas Hoevel



Joined: 16 Oct 2006
Posts: 387
Location: Cologne, Germany

PostPosted: Thu Mar 06, 2008 8:09 am    Post subject: Reply with quote

edmicman wrote:
If the source can be changed, is there any chance of a fix or patch that handles any image better?

With the code changes shown above JPEG images will be handled correctly.
There won't be a fix that can handle JPEG images better than "correctly". Laughing

If you find non-JPEG images that are not handled correctly, send us a sample and we'll try to fix it. Standard formats are already implemented.
_________________
Regards
Thomas Hoevel
PDFsharp Team
Back to top
View user's profile Send private message Visit poster's website
edmicman



Joined: 04 Mar 2008
Posts: 8

PostPosted: Thu Mar 06, 2008 2:37 pm    Post subject: Reply with quote

Thank you! I did end up getting it working....my images are displaying, and I can get them to display in the size I want. Now I just have to work on getting a better source image so it doesn't lose as much quality when I increase the size Smile

Great job on the library!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com