Problems using Bitmap.FromHBitmap() because of uint and inptr

PDF-XChange Editor SDK for Developers

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

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
Kutusow
User
Posts: 19
Joined: Tue Oct 30, 2018 12:14 pm

Problems using Bitmap.FromHBitmap() because of uint and inptr

Post by Kutusow »

Hey,

I am currently working on a Wrapper of your PDF document class. I want a method that extracts all images and returns a System.Drawing.Image Array.
Here is my Code:

Code: Select all

private Image[] GetImagesFromContentItem(PDFXEdit.IPXC_Content content)
{
	var imageList = new List<Image>();
	PDFXEdit.IIXC_Page ip;
	var items = content.Items;

	for (uint j = 0; j < items.Count; j++)
	{
		var itemType = items[j].Type;
		if (itemType == PDFXEdit.PXC_CIType.CIT_Image || itemType == PDFXEdit.PXC_CIType.CIT_InlineImage)
		{
			ip = items[j].Image_CreateIXCPage(false, PDFXEdit.PXC_RenderingIntent.RI_RelativeColorimetric);
			uint hbitmap = ip.CreateHBitmap();
			var ptr = new IntPtr(hbitmap);
			imageList.Add(Bitmap.FromHbitmap(ptr));
			DeleteObject(ptr);
		}
		if (itemType == PDFXEdit.PXC_CIType.CIT_XForm)
		{
			var xFormHandle = items[j].XForm_Handle;
			var xForm = this.Document.GetXFormByHandle(xFormHandle);
			var contentxForm = xForm.GetContent(PDFXEdit.PXC_ContentAccessMode.CAccessMode_WeakClone);
			imageList.AddRange(GetImagesFromContentItem(contentxForm));
		}
	}

	return imageList.ToArray();
}
The pertinent part is in the middle, where i create an hbitmap (which gives a uint) from a given IIXC_Page. Then i use it to get an Intptr, so that i can use Bitmap.FromHBitmap(Intpr pointer) to finally get my hands on the Bitmap.
The Problem is, that this Code leads to overflow exceptions, when i use the x86 dll.
Any Idea how i could resolve this issue?

kind regards,
Kutusow
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Problems using Bitmap.FromHBitmap() because of uint and inptr

Post by Sasha - Tracker Dev Team »

Hello Kutusow,

Here's a snippet that will help - just add the XForms logic and you are good to go:

Code: Select all

var imageList = new List<Image>();
IPXC_Content content = pdfCtl.Doc.CoreDoc.Pages[0].GetContent(PXC_ContentAccessMode.CAccessMode_Readonly);
for (uint i = 0; i < content.Items.Count; i++)
{
	IPXC_ContentItem item = content.Items[i];
	if ((item.Type != PXC_CIType.CIT_Image) && (item.Type != PXC_CIType.CIT_InlineImage))
		continue;
	PDFXEdit.IIXC_Page iPage = item.Image_CreateIXCPage();
	Bitmap b = new Bitmap((int)iPage.Width, (int)iPage.Height);
	Graphics g = Graphics.FromImage(b);
	g.Clear(Color.White);
	IntPtr hdc = g.GetHdc();
	iPage.DrawToDC((uint)hdc, 0, 0, iPage.Rect.right - iPage.Rect.left, iPage.Rect.bottom - iPage.Rect.top, 0, 0);
	g.ReleaseHdc(hdc);
	imageList.Add(b);
}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply