Inserting Grayscale Images

This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-Tools SDK of Library DLL functions(only) - Please use the PDF-XChange Drivers API SDK Forum for assistance with all PDF Print Driver related topics or PDF-XChange Viewer SDK if appropriate.

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Stefan - PDF-XChange

Xiot
User
Posts: 4
Joined: Mon Mar 13, 2006 5:06 pm

Inserting Grayscale Images

Post by Xiot »

I am trying to insert a lzw 8 bit grayscale image into a PDF, and it is showing up completely black.

I have attached both the original image and the created Pdf file.
I have tried a color lzw image, and that works fine, so I'm thinking that there is something that it doesn't like with the palette.

Any suggestions?

I am using pxclib40 v4.0.212.0

This is the code that I was using to produce the result.

Code: Select all

    Public Sub AddImages(ByVal sourceFile As String, ByVal pageWidthInInches As Double, ByVal pageHeightInInches As Double)
        Dim res As Integer
        Dim pp As IntPtr = IntPtr.Zero

        Try
            Dim pageWidth As Double = PDFXC.I2L(pageWidthInInches)
            Dim pageHeight As Double = PDFXC.I2L(pageHeightInInches)

            Dim npages As Integer = PDFXC.PXC_AddImageExA(_pdf, sourceFile, IntPtr.Zero, 0)
            CheckReturnValue(npages)

            Dim pages = New Integer(npages - 1) {}
            For pageno As Integer = 0 To npages - 1
                pages(pageno) = pageno + 1
            Next

            pp = Runtime.InteropServices.Marshal.AllocHGlobal(npages * IntPtr.Size)
            res = PDFXC.PXC_AddImageExA(_pdf, sourceFile, pp, npages)
            CheckReturnValue(res)

            Dim p4() As Integer = Nothing
            p4 = New Integer(npages - 1) {}
            Runtime.InteropServices.Marshal.Copy(pp, p4, 0, npages)
            Runtime.InteropServices.Marshal.FreeHGlobal(pp)
            pp = IntPtr.Zero

            For Each pageno As Integer In pages
                Dim page As IntPtr

                CheckReturnValue(PDFXC.PXC_AddPage(_pdf, pageWidth, pageHeight, page))

                Dim img As IntPtr = New IntPtr(p4(pageno - 1))

                Dim imageWidth As Double
                Dim imageHeight As Double
                Dim xDPI As Integer
                Dim yDPI As Integer
                CheckReturnValue(PDFXC.PXC_GetImageDimension(_pdf, img, imageWidth, imageHeight))
                CheckReturnValue(PDFXC.PXC_GetImageDPI(_pdf, img, xDPI, yDPI))
                If xDPI <= 0 Then xDPI = 96
                If yDPI <= 0 Then yDPI = 96

                Dim scaleW As Double
                Dim scaleH As Double
                GetScaleFactors(imageWidth, imageHeight, xDPI, yDPI, pageWidth, pageHeight, scaleW, scaleH)

                CheckReturnValue(PDFXC.PXC_PlaceImage(page, img, 0, pageHeight, pageWidth * scaleW, pageHeight * scaleH))
            Next

        Finally
            If pp <> IntPtr.Zero Then
                Runtime.InteropServices.Marshal.FreeHGlobal(pp)
            End If
        End Try
    End Sub
EDIT:
I just tried using PXC_AddImageFromHBITMAP using the code below and everything worked nicely.

Code: Select all

Public Sub AddImageFromBitmap(ByVal b As Bitmap, ByVal pageWidthInInches As Double, ByVal pageHeightInInches As Double)
        Dim palette As IntPtr = IntPtr.Zero
        Dim hBitmap As IntPtr = IntPtr.Zero

        Try
            palette = CreateWinPalette(b.Palette)
            hBitmap = b.GetHbitmap()

            Dim img As IntPtr
            CheckReturnValue(PDFXC.PXC_AddImageFromHBITMAP(_pdf, hBitmap, palette, img))
            
            Dim imageHorizontalResolution As Double = b.HorizontalResolution
            If imageHorizontalResolution <= 0 Then imageHorizontalResolution = 96
            Dim imageVerticalResolution As Double = b.VerticalResolution
            If imageVerticalResolution <= 0 Then imageVerticalResolution = 96
            Dim scaleW As Double
            Dim scaleH As Double
            Dim pageWidth As Double = PDFXC.I2L(pageWidthInInches)
            Dim pageHeight As Double = PDFXC.I2L(pageHeightInInches)
            GetScaleFactors(b.Width, b.Height, imageHorizontalResolution, imageVerticalResolution, pageWidth, pageHeight, scaleW, scaleH)

            Dim page As IntPtr
            CheckReturnValue(PDFXC.PXC_AddPage(_pdf, pageWidth, pageHeight, page))

            CheckReturnValue(PDFXC.PXC_PlaceImage(page, img, 0, pageHeight, pageWidth * scaleW, pageHeight * scaleH))
        Finally
            If hBitmap <> IntPtr.Zero Then DeleteObject(hBitmap)
            If palette <> IntPtr.Zero Then DeleteObject(palette)
        End Try
    End Sub
You do not have the required permissions to view the files attached to this post.
User avatar
Will - Tracker Supp
Site Admin
Posts: 6815
Joined: Mon Oct 15, 2012 9:21 pm

Re: Inserting Grayscale Images

Post by Will - Tracker Supp »

Hi Xiot,

Thanks for the post - I'm glad that you got everything sorted! :)

Cheers,
If posting files to this forum, you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded.
Thank you.

Best regards

Will Travaglini
Tracker Support (Europe)
Tracker Software Products Ltd.
http://www.tracker-software.com
Xiot
User
Posts: 4
Joined: Mon Mar 13, 2006 5:06 pm

Re: Inserting Grayscale Images

Post by Xiot »

I got it working, but I'm still curious on why AddImageEx wasnt working
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7463
Joined: Wed Mar 25, 2009 10:37 pm

Re: Inserting Grayscale Images

Post by Paul - PDF-XChange »

Hi Xiot,

we'll have to have one of our developers look into this and give feedback. The person I want to do this is in transit from Europe right now. He will be back in this office Tuesday and I'll ask him to take a look.

hth
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com
Xiot
User
Posts: 4
Joined: Mon Mar 13, 2006 5:06 pm

Re: Inserting Grayscale Images

Post by Xiot »

Sounds good.
As I have a working solution, there is no rush for me.
Just just curious

Thanks
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7463
Joined: Wed Mar 25, 2009 10:37 pm

Re: Inserting Grayscale Images

Post by Paul - PDF-XChange »

:)
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com