View previous topic :: View next topic |
Author |
Message |
cmosses
Joined: 05 Oct 2007 Posts: 6
|
Posted: Wed Nov 21, 2007 8:34 am Post subject: XImage compatible formats? DrawImage makes a black mess... |
|
|
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 |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Wed Nov 21, 2007 9:15 am Post subject: |
|
|
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 |
|
|
cmosses
Joined: 05 Oct 2007 Posts: 6
|
Posted: Wed Nov 21, 2007 9:29 am Post subject: |
|
|
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 |
|
|
edmicman
Joined: 04 Mar 2008 Posts: 8
|
Posted: Wed Mar 05, 2008 8:41 pm Post subject: |
|
|
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 |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Thu Mar 06, 2008 8:09 am Post subject: |
|
|
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".
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 |
|
|
edmicman
Joined: 04 Mar 2008 Posts: 8
|
Posted: Thu Mar 06, 2008 2:37 pm Post subject: |
|
|
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
Great job on the library! |
|
Back to top |
|
|
|