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

Greyscale images not scaling correctly
http://forum.pdfsharp.de/viewtopic.php?f=2&t=2351
Page 1 of 1

Author:  whitet [ Mon Mar 04, 2013 10:05 pm ]
Post subject:  Greyscale images not scaling correctly

Hello there,

I am having some issues with PdfSharp not scaling Greyscale images (image.gdiImage.PixelFormat=Format8bppIndexed) correctly to a given Rect using the XGraphics.DrawImage(image, rect) method.

Basically it looks like this:
Image

But should look like this (example only):
Image

I changed the code below, and this seems to have fixed the issue but I don't know if this may break something else and still don't understand exactly why this is occurring. Please help so I can fix this properly.

PdfImage.cs
InitializeJpeg() method. Line 183 in my version of PdfSharp.
Refer to code within else if statement.
Code:
<code snippet>...
#if GDI
      if ((image.gdiImage.Flags & ((int)ImageFlags.ColorSpaceCmyk | (int)ImageFlags.ColorSpaceYcck)) != 0)
      {
        // TODO: Test with CMYK JPEG files
        // THHO: I only found ImageFlags.ColorSpaceYcck JPEG files ...
        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)
      {
        // TW 5/2/2013: /DeviceGray option below was causing images with
        // .gdiImage.pixelFormat=Format8bppIndexed to not be scaled properly in PDF.
        // Replaced with default /DeviceRGB and now seems to work.
        //Elements[Keys.ColorSpace] = new PdfName("/DeviceGray");
        Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB"); 
      }
      else
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB");
      }


Kind Regards
Travis

Attachments:
goodscale.jpg
goodscale.jpg [ 17.72 KiB | Viewed 3877 times ]
badscale.jpg
badscale.jpg [ 17.78 KiB | Viewed 3877 times ]

Author:  Thomas Hoevel [ Tue Mar 05, 2013 9:21 am ]
Post subject:  Re: Greyscale images not scaling correctly

Hi, Travis!
whitet wrote:
I am having some issues with PdfSharp not scaling Greyscale images (image.gdiImage.PixelFormat=Format8bppIndexed) correctly to a given Rect using the XGraphics.DrawImage(image, rect) method.
AFAIK this is a fault that came with Windows Vista (and was still around with Windows 7 (maybe even Windows 8)). PDFsharp works correctly under Windows XP.

Your fix probably will not work under Windows XP.

See also:
viewtopic.php?p=4456#p4456

Author:  whitet [ Thu Mar 07, 2013 5:49 am ]
Post subject:  Re: Greyscale images not scaling correctly

Thanks Thomas :D

This is exactly what is happening to me. On XP it was working fine. Good point - I will test my change there also.

Thanks for the link also - very interesting. I'm just trying to figure out which is the most elegant and sustainable fix for this.

Cheers
Travis

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