.net Wrapper for DrawPageToIStream

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

.net Wrapper for DrawPageToIStream

Post by DolphinMann »

Does anyone have the .net wrapper for the DrawPageToIStream function? I couldn't find it anywhere in the examples but it is mentioned in the documentation and many different threads.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

Code: Select all

       [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct PXV_DrawToImageParams
        {
            public Int32 ImageFormat;
            public Int32 Flags;  // see PXV_DrawToImageFlags
            public Int32 Bpp;
        };

        public enum PXV_DrawToImageFlags
		{
			pxvdif_None						=	0x0000,
		};


        [DllImport("pxcview")]
        public static extern int PXCV_DrawPageToIStream(IntPtr hDoc,
        												Int32 page_num,
        												ref PXV_CommonRenderParameters pParams,
        												Int32 backColor,
        												ref PXCV_DrawPageToIStream pImageParams,
        												ref IStream pStream);

Where IStream is an object with IStream interface (http://msdn.microsoft.com/en-us/library ... -snippet-1).
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

Re: .net Wrapper for DrawPageToIStream

Post by DolphinMann »

Thank you for that. I am probably overlooking something obvious, but I just can't seem to make this work. I keep getting an error during the DLL call that I am attempting to read/write protected memory. I'm pretty sure it's my iStream memory but I can't be positive.

I was previously using DrawPageToDIB and was working, but I'd like to use the IStream version for the additional features of built-in formatting and built in pixel depth;

This was my old code( I didn't include the document/library initialization, nothing has changed there and it's outside of this block), and pretty much stripped from your examples:

Code: Select all

IntPtr hBase = IntPtr.Zero;
                        PXCV_Lib36.PXV_CommonRenderParameters crp = new PXCV_Lib36.PXV_CommonRenderParameters();
                        int[] rect = { 0, 0, width, height };
                        int size = Marshal.SizeOf(rect[0]) * rect.Length;
                        IntPtr rectPointer = Marshal.AllocHGlobal(size);
                        Marshal.Copy(rect, 0, rectPointer, 4);

                        crp.WholePageRect = rectPointer;
                        crp.Flags = (int)PXCV_Lib36.PXV_CommonRenderParametersFlags.pxvrpf_None;
                        crp.RenderTarget = PXCV_Lib36.PXCV_RenderMode.pxvrm_Exporting;
Int32 clr = aBkgColor.ToArgb();
IntPtr hBitmap = IntPtr.Zero;

PXCV_Lib36.PXCV_DrawPageToDIBSection(m_Doc, page, ref crp, hBase, clr, ref hBitmap, IntPtr.Zero, 0)
                        aBitmap = Image.FromHbitmap(hBitmap);
            DeleteObject(hBitmap);
new code that I can't get to work, I took the IStreamWrapper from one of the examples provided and you can see two commented out lines where i was trying to initialize a pointer from file or memory block directly, I could not get that to work either

Code: Select all

                        IntPtr hBase = IntPtr.Zero;
                        PXCV_Lib36.PXV_CommonRenderParameters crp = new PXCV_Lib36.PXV_CommonRenderParameters();
                        int[] rect = { 0, 0, width, height };
                        int size = Marshal.SizeOf(rect[0]) * rect.Length;
                        IntPtr rectPointer = Marshal.AllocHGlobal(size);
                        Marshal.Copy(rect, 0, rectPointer, 4);

                        crp.WholePageRect = rectPointer;
                        crp.Flags = (int)PXCV_Lib36.PXV_CommonRenderParametersFlags.pxvrpf_None;
                        crp.RenderTarget = PXCV_Lib36.PXCV_RenderMode.pxvrm_Exporting;

                        PXCV_Lib36.PXV_DrawToImageParams dip = new PXCV_Lib36.PXV_DrawToImageParams();
                        dip.Flags = (int)PXCV_Lib36.PXV_DrawToImageFlags.pxvdif_None;
                        if (!useColor)
                        {
                            dip.Bpp = 1;
                        }
                        else
                        {
                            dip.Bpp = 24;
                        }

                        dip.ImageFormat = 0x54494646;
Int32 clr = backColor.ToArgb();

                        Stream stream = new MemoryStream();
                        IStream iStream = new IStreamWrapper(stream);
                        //CreateStreamOnHGlobal(IntPtr.Zero, true, out iStream);
                        //SHCreateStreamOnFile("temp.tif", StgmConstants.STGM_CREATE | StgmConstants.STGM_READWRITE, ref iStream);
                        [b]PXCV_Lib36.PXCV_DrawPageToIStream(m_Doc, page, ref crp, clr, ref dip, ref iStream)[/b]
                        System.Runtime.InteropServices.ComTypes.STATSTG stat;
                        iStream.Stat(out stat, 1);
                        long bufferSize = stat.cbSize;
                        byte[] buffer = new byte[bufferSize];
                        stream.Write(buffer, 0, buffer.Length);
                        stream.Flush();
                        returnValue = new Bitmap(stream);
                        stream.Close();
I die on the bold line with memory access every time
User avatar
Will - Tracker Supp
Site Admin
Posts: 6815
Joined: Mon Oct 15, 2012 9:21 pm
Location: London, UK
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Will - Tracker Supp »

Hi DolphinMann,

The bold tags are stripped when using the code tags. Could you point us to the specific line that's killing you?

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
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

Re: .net Wrapper for DrawPageToIStream

Post by DolphinMann »

PXCV_Lib36.PXCV_DrawPageToIStream(m_Doc, page, ref crp, clr, ref dip, ref iStream)

That is where I get the access violation. As i said I think it has something to do with my iStream since I had this working with the DrawToDIB, though I suppose there is an issue with my Image params, but I cut and paste that from your code block.
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

Re: .net Wrapper for DrawPageToIStream

Post by DolphinMann »

Does anyone else watching the forums have a .NET sample code using the DrawPageToIStream?
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

I'm working on a sample for you. As I'm not experienced with C# it takes time.
In my sample I will use SHCreateStreamOnFileEx function (http://msdn.microsoft.com/en-us/library ... s.85).aspx) instead of the wrapper.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

Here is a sample code that works fine on my side:

Code: Select all

		[DllImport("Shlwapi.dll")]
		public static extern int SHCreateStreamOnFile(string pszFile, Int32 grfMode, out IStream ppstm);

		private void saveToPNGToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (m_PDFDoc == null)
				return;


            IStream aIStream;
			Color bgColor;

			PXCV_Lib36.PXV_CommonRenderParameters aCommonRenderParam = new PXCV_Lib36.PXV_CommonRenderParameters();
			PXCV_Lib36.PXV_DrawToImageParams aImageParams = new PXCV_Lib36.PXV_DrawToImageParams();

			PageDimension aPageDim;
			Size aPageSize = Size.Empty;
			PXCV_Helper.RECT aWholePage = new PXCV_Helper.RECT();

			IntPtr p1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));

			try
			{
				int res = SHCreateStreamOnFile("e:\\sample.png", 0x1042, out aIStream);

				m_PDFDoc.GetPageDimensions(this.pdfViewCtl1.PageNumber, out aPageDim.w, out aPageDim.h);
				aPageSize.Width = (int)(aPageDim.w * 96.0 / 72.0);
				aPageSize.Height = (int)(aPageDim.h * 96.0 / 72.0);
				aWholePage.left = 0;
				aWholePage.top = 0;
				aWholePage.right = aPageSize.Width;
				aWholePage.bottom = aPageSize.Height;

				Marshal.StructureToPtr(aWholePage, p1, false);

				aCommonRenderParam.WholePageRect = p1;
				aCommonRenderParam.DrawRect = System.IntPtr.Zero;
				aCommonRenderParam.RenderTarget = PXCV_Lib36.PXCV_RenderMode.pxvrm_Exporting;
				aCommonRenderParam.Flags = 0;

				aImageParams.Bpp = 32;
				aImageParams.ImageFormat = 0x504e4720;

				// aImageParams.Bpp = 1;
				// aImageParams.ImageFormat = 0x54494646;

				aImageParams.Flags = (int)PXCV_Lib36.PXV_DrawToImageFlags.pxvdif_None;

				bgColor = Color.FromArgb(0, 0, 0, 0);

				m_PDFDoc.DrawToIStream(aIStream, this.pdfViewCtl1.PageNumber, aCommonRenderParam, aImageParams, bgColor);
			}
			finally
			{
				Marshal.FreeHGlobal(p1);
			}
		}
SHCreateStreamOnFileEx does not work properly for some reason - stream is created, all functions work ok, but file was not created.
But I found that SHCreateStreamOnFile works fine.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

Re: .net Wrapper for DrawPageToIStream

Post by DolphinMann »

Thank you for that code snippet, it was very helpful however I am still a bit stuck. I think it because I am not working with the correct version of the pxcview.dll or the correct PXCV_Lib.cs file(.net wrapper)

The code you have the following line:
m_PDFDoc.DrawToIStream

Is the m_PDFDoc variable supposed to be a PDFDoc value as defined in the PXCV_Lib.cs file from the examples?

And is this the right reference to the pxcview.dll?

[DllImport("pxcview.dll")]
public static extern int PXCV_DrawPageToIStream(IntPtr hDoc,
Int32 page_num,
ref PXV_CommonRenderParameters pParams,
Int32 backColor,
ref PXV_DrawToImageParams pImageParams,
ref IStream pStream);
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

Here is my sample.
Attachments
pxcv_sample.zip
(3.33 MiB) Downloaded 184 times
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

Re: .net Wrapper for DrawPageToIStream

Post by DolphinMann »

Thank you, that worked. My wrapper function was passing a ref to the IStream, not the actual IStream like you had in your example. Thank you!
DolphinMann
User
Posts: 158
Joined: Mon Aug 04, 2014 7:34 pm

Re: .net Wrapper for DrawPageToIStream

Post by DolphinMann »

One small note, it looks like you never release the iStream variable so the saved image cannot be viewed or modified until the executing application is close.

I cannot find any istream.close/dispose/release functions however. What is the best way to go about closing that stream so the file can be accessed?
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2353
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: .net Wrapper for DrawPageToIStream

Post by Vasyl-Tracker Dev Team »

Code: Select all

IStream aIStream;
- it defines smart-pointer to 'classic' IStream. The .NET's garbage-collector knows about that and will automatically release it a bit later, after using. Also you may release it immediately by:

Code: Select all

System.Runtime.InteropServices.Marshal.ReleaseComObject(aIStream);
HTH
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
michael wagmann
User
Posts: 7
Joined: Fri Sep 09, 2011 9:21 am

Re: .net Wrapper for DrawPageToIStream

Post by michael wagmann »

I've downloaded and run your sample code.
Unfortunatly, it seems that the bpp property of PXV_DrawToImageParams does not work correctly. In case of PNG(imageformart = 0x504e4720) a value of 8 is ignored and an image with 24 bpp is created. When bpp is set to 1, than a png image file with 1bpp is created, so in principle the code works.
According to the documentation of PXV_DrawToImageParams it is possible to export 8bpp PNG files. [ https://help.pdf-xchange.com/DEV/de ... mageparams ]
Can you please have a look into your library code? We need to export pdf files as 8bpp png files. Btw, in case of JPG 8 bpp doesn't work, too.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17941
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Tracker Supp-Stefan »

Hello Michael,

I've asked Ivan to take a look at this again, and he will advise as soon as he can.

Regards,
Stefan
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

That function can produce the following PNG files:

Code: Select all

====================================================================================
|  PNG type        | PXV_DrawToImageParams.bpp | PXV_CommonRenderParameters.Flags  |
====================================================================================
|  1 bpp B&W       |             1             | pxvrpf_BlackAndWhite              |
|  1 bpp mono      |             1             |                                   |
|  8 bpp gray      |             8             | pxvrpf_BlackAndWhite              |
|  16 bpp alphgray |            16             | pxvrpf_BlackAndWhite              |
|  32 bpp ARGB     |            32             |                                   |
|  24 bpp RGB      |       -------- all the rest combinations --------             |
====================================================================================
HTH
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
michael wagmann
User
Posts: 7
Joined: Fri Sep 09, 2011 9:21 am

Re: .net Wrapper for DrawPageToIStream

Post by michael wagmann »

Ok. Can you tell me where to find the value of the pxvrpf_BlackAndWhite constant? In the online documentation of PXV_CommonRenderParameters I can not find it [https://help.pdf-xchange.com/DEV/de ... parameters}.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17941
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Tracker Supp-Stefan »

Hi Michael,

I've passed that latest question to Ivan and he will post here shortly.

Regards,
Stefan
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

Yes, as I see two flags are undocumented. Will fix that on the help system, and also provide them here:

Code: Select all

====================================================================================
| pxvrpf_BlackAndWhite | 0x0080 | If set, a task will be rendered in black&white   |
|                      |        | mode. Has meaning for PXCV_DrawPageToDC and      |
|                      |        | PXCV_DrawPageToIStream methods                   |
| pxvrpf_Dither        | 0x0100 | Has meaning only with pxvrpf_BlackAndWhite flag  |
====================================================================================
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
michael wagmann
User
Posts: 7
Joined: Fri Sep 09, 2011 9:21 am

Re: .net Wrapper for DrawPageToIStream

Post by michael wagmann »

Hi Ivan,

I've set the flags property of the CommonRenderParametersFlags to the blackandwhite value (0x0080) and the bpp value of the DrawToImageParams to a value of 8.
Unfortunatly, the PXCV_DrawPageToIStream function fails in that case.

After having a closer look at the values of the PXV_CommonRenderParametersFlags enumeration, I used 0x0008 instead of 0x0080 and a grayscale PNG was created. However, still with 24 bpp even though bpp was set to a value of 8.

Your post from 27. Nov. indicates that there is no combination of BPP and CommonRenderParameter-Flags to produce an image with 8bpp and color. Is that correct? The Viewer is able to do so.

Can you please enhance your "pxcv_sample", so that I have a running example?

BR, Simon
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: .net Wrapper for DrawPageToIStream

Post by Ivan - Tracker Software »

I see that the combination of BPP=8 and pxvrpf_BlackAndWhite flag cause an error.

I have fixed that issue and also add support for 8bpp color images. Now the table will look like:

Code: Select all

==============================================================================================
|  PNG type        | PXV_DrawToImageParams.bpp | PXV_CommonRenderParameters.Flags            |
==============================================================================================
|  1 bpp B&W       |             1             | pxvrpf_BlackAndWhite                        |
|  1 bpp mono      |             1             |                                             |
|  8 bpp gray      |             8             | pxvrpf_BlackAndWhite or pxvrpf_RenderAsGray |
|  8 bpp palette   |             8             |                                             |
|  16 bpp alphgray |            16             | pxvrpf_BlackAndWhite or pxvrpf_RenderAsGray |
|  32 bpp ARGB     |            32             |                                             |
|  24 bpp RGB      |       -------- all the rest combinations --------                       |
==============================================================================================
These changes will be available in a next build.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
Post Reply