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

Img .JPG on Windows 7
http://forum.pdfsharp.com/viewtopic.php?f=2&t=2784
Page 1 of 1

Author:  zoranm80 [ Mon Apr 07, 2014 10:20 am ]
Post subject:  Img .JPG on Windows 7

I searched in the forum, but did not find any solution for the problem with the creation of PDF, using a JPG as background on Windows 7 64bit.
My image is .JPG, 32-bit CMYK.
The PdfSharp dll version that we use is ā€œ1.32.2608.0ā€

When my software creates the PDF, the background image loses color and repeats the image several times.
On XP this does not happen.

This is my code
Code:
try{
   PdfDocument document = new PdfDocument();
   PdfPage page = document.AddPage();
   document.Options.NoCompression = true;
   document.Options.ColorMode = PdfColorMode.Cmyk;
   document.Version = 14;
   page.Size = PdfSharp.PageSize.A4;
   XImage xImagePosterBackground = XImage.FromFile("c:\\img_test.jpg");
   XGraphics graphics = XGraphics.FromPdfPage(page);
   graphics.DrawImage(xImagePosterBackground, new XPoint(0, 0));
   string fileName = "test_jpg.pdf";
   document.Save("c:\\" + fileName);
}
catch (System.Exception ex)
{
   string s = ex.ToString();
   System.Diagnostics.Debug.Assert(false, "Error!");
}


Do you have any suggestions?

Attachments:
File comment: left side is an PDF created by Win7, right side is an PDF crated by XP
errore.png
errore.png [ 16.9 KiB | Viewed 11982 times ]

Author:  Thomas Hoevel [ Mon Apr 07, 2014 11:26 am ]
Post subject:  Re: Img .JPG on Windows 7

You can try the code modification suggested by SiliconMind here:
viewtopic.php?p=6963#p6963

Author:  zoranm80 [ Mon Apr 07, 2014 12:44 pm ]
Post subject:  Re: Img .JPG on Windows 7

Iā€™m sincere, I did not understand what part of the code I should look
The problem is the DLL or the operating system?
By 2011, the problem has not been fixed?
How can I change posted code to get something that work?

Author:  Thomas Hoevel [ Mon Apr 07, 2014 12:53 pm ]
Post subject:  Re: Img .JPG on Windows 7

File PdfImage.cs, class PdfImage, method "void InitializeJpeg()"

Look for "Save(memory, ImageFormat.Jpeg);" and replace with the suggested code.

Author:  zoranm80 [ Mon Apr 07, 2014 2:09 pm ]
Post subject:  Re: Img .JPG on Windows 7

so,
I've download the entire solution, BuildAll-PDFsharp-VS2010.sln
Of class PDFImage.cs, I modified the method "void InitializeJpeg ()", changing the line
Code:
image.gdiImage.Save (memory, ImageFormat.Jpeg);

with
Code:
if (! image.path.StartsWith ("*"))
       {
           using (FileStream sourceFile = File.OpenRead (image.path))
           {
               int count = 0;
               byte [] buffer = new byte [1024];
               memory = new MemoryStream ((int) sourceFile.Length);
               do
               {
                   count = sourceFile.Read (buffer, 0, buffer.Length);
                   memory.Write (buffer, 0, buffer.Length);
               }
               while (count> 0);
           }
       }
       else
       {
           memory = new MemoryStream ();
           image.gdiImage.Save (memory, ImageFormat.Jpeg);
       }


I rebuilt project
I took the new dll and I have included it in my project.
The end result is not changed.

Author:  Thomas Hoevel [ Mon Apr 07, 2014 2:47 pm ]
Post subject:  Re: Img .JPG on Windows 7

Which DLL? "PdfSharp.dll" or "PdfSharp-WPF.dll"? The patch is for the former only.
If you are using "PdfSharp-WPF.dll", try "PdfSharp.dll" instead.
Make sure there are no old copies in the various BIN/DEBUG folders.

Tip: if you add a reference to the .CSPROJ files (instead of referencing the DLLs) you can step through PDFsharp.

Author:  zoranm80 [ Mon Apr 07, 2014 3:40 pm ]
Post subject:  Re: Img .JPG on Windows 7

as I write you before, I'm using solution BuildAll-PdfSharp-VS2010.sln
I've downloaded it from sourceforge (sourceforge.net/projects/pdfsharp/files/pdfsharp/)
version: PDFsharp 1.32 - 2012-03-07

I've include my personal solution inside your solution and debug single steps just to be sure I'm using right source.
I think this produce "PdfSharp.dll" and not "PdfSharp-WPF.dll"

No changes for my output.

Author:  zoranm80 [ Mon Apr 07, 2014 4:06 pm ]
Post subject:  Re: Img .JPG on Windows 7

Code:
   int count = 0;
   byte[] buffer = new byte[1024];
   memory = new MemoryStream((int)sourceFile.Length); // sourceFile.Length = 2462478
   do
   {
     count = sourceFile.Read(buffer, 0, buffer.Length);
     memory.Write(buffer, 0, buffer.Length);
   }
   while (count > 0);
        // memory.Length = 2463744

and, if I use old command
Code:
image.gdiImage.Save(memory, ImageFormat.Jpeg);

the value assumed by the variable "memory" is 2462516

So, original image length is 2462478
using the new statement the length is 2463744
using original statement the length is 2462516

Author:  Thomas Hoevel [ Mon Apr 07, 2014 4:18 pm ]
Post subject:  Re: Img .JPG on Windows 7

It could be the problem described in the other thread.
We don't have the JPEG image, we don't have an SSCCE, so there is not much we can do now.

Author:  Thomas Hoevel [ Tue Apr 08, 2014 12:06 pm ]
Post subject:  Re: Img .JPG on Windows 7

GDI+ supports the following image flags:
Code:
    ColorSpaceRgb = 16,
    ColorSpaceCmyk = 32,
    ColorSpaceGray = 64,
    ColorSpaceYcbcr = 128,
    ColorSpaceYcck = 256,


For your file, the flag ColorSpaceRgb is set. I presume under Windows XP the flag ColorSpaceYcck is set and image shows correctly.
PDFsharp lets Windows analyze the file. This sometimes fails under Windows 7 (and later).
Adobe Reader also does not analyze the file, it relies on the flags given in the PDF file.

If all your JPEG files are CMYK, then go to PdfImage.cs, method InitializeJpeg and replace this line
Code:
Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB");

with those:
Code:
Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");
Elements["/Decode"] = new PdfLiteral("[1 0 1 0 1 0 1 0]");


If you use both RGB and CMYK JPEGs: sorry, I currently have no idea.

BTW: That image is a good candidate for lossless compression (e.g. PNG) instead of JPEG. Might even reduce the file size.


Re different sizes:
zoranm80 wrote:
count = sourceFile.Read(buffer, 0, buffer.Length);
memory.Write(buffer, 0, buffer.Length);
The second line should be
Code:
memory.Write(buffer, 0, count)

This resolves the size difference - but image still doesn't work.

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