Print Page | Close Window

AddImageFromString() in C++ DLL Project (RESOLVED)

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2144
Printed Date: 04 Jul 25 at 9:43AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: AddImageFromString() in C++ DLL Project (RESOLVED)
Posted By: barqyDev
Subject: AddImageFromString() in C++ DLL Project (RESOLVED)
Date Posted: 10 Feb 12 at 8:30PM
I'm using Visual Studio 2010 in a MFC Dialog App and I would like to be able to 
place images in my PDF that come from bitmaps stored in my resource file

reason 1 - don't want to have files travelling with the app.  I'd like it as standalone as possible
reason 2 - don't want to create temporary files on disk just to load with AddImageFromFile()

I've tried loading the image into memory with CImage:
CImage Image;
 Image.LoadFromResource( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_VESSIX) );

I have access to the bitmap data via Image.GetBits(), but it's not clear to me
what format data AddImageFromString() actually wants.

I know it takes a std::string parameter, but that doesn't say what it's expecting.

Any help would be appreciated!




Replies:
Posted By: barqyDev
Date Posted: 10 Feb 12 at 9:19PM
I do have a working project using AddImageFromFile() it just makes me unhappy for the reasons mentioned.


Posted By: barqyDev
Date Posted: 10 Feb 12 at 10:24PM
Another update....loaded a JPG from disk into a PDF file:
CImage image;
image.LoadFromResource( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_LOGO)  );
image.Save( L"test.jpg" );
index = pdf.AddImageFromFile( L"test.jpg", 0 );
pdf.SelectImage( index );
pdf.DrawImage( 0, 0, 100, 100 );
// all that above works

// now, let's what data got stuffed into the PDF
index = pdf.GetPageImageList(0);
std::string Grab = pdf.GetImageListItemDataToString( index, 1, 0 );
// and it looks like the exact same content as what's in my JPG file on disk.


Assuming I have that right, I'm now chunking through some more API calls:
FindResource
LoadResource
LockResource
SizeofResource

Hopefully I'm on the right track.


Posted By: AndrewC
Date Posted: 11 Feb 12 at 2:13AM
AddImage from file expects a full BMP, JPG, PNG or TIFF file.  It sounds like you are on the right track extracting the JPEG resource.

Here is a post that gives some hope.  The last reply indicates that it might work better when the JPG is marked as RCDATA in the resource file.

http://www.sfml-dev.org/forum/viewtopic.php?p=33890&sid=ba955874d6e717c8b858ede78a74c029 - http://www.sfml-dev.org/forum/viewtopic.php?p=33890&sid=ba955874d6e717c8b858ede78a74c029


Posted By: barqyDev
Date Posted: 11 Feb 12 at 8:03AM
Thank you for your reply today, it helped!

Unfortunately, I remained my own worst enemy.  After the the RCDATA update, I was getting the data and length right, but it wasn't working.

After dinner I came back and took a look at the actual call to AddImageFromString() that I was making.  Turns out I wasn't getting all the data stuffed into the std::string I thought I was.  I only got the first chunk of bytes up to the first NULL.

Builidng my input parameter with this helped: std::string test( res_data, res_size );

So....Here's the working code block for anybody else that might stroll by:

      int result = 0;
      HINSTANCE Instance = AfxGetInstanceHandle();
      HRSRC res = FindResource( Instance,MAKEINTRESOURCE( IDB_BINARY ), RT_RCDATA );
      if( res )
      {
         HGLOBAL res_handle = LoadResource( Instance , res );
         if( res_handle )
         {
            char *res_data = (char *) LockResource( res_handle );
            if( res_data )
            {
               DWORD res_size = SizeofResource( Instance, res );
               if( res_size )
               {
                  std::string test( res_data, res_size );
                  result = m_PdfWriter->AddImageFromString( test, 0 );
                  m_PdfWriter->SelectImage( result );
                  m_PdfWriter->DrawImage( 550, 5, 60, 60 );
               } // got the length
            } // locked it..i.e. got pointer to 1st byte
         } // loaded the resource
      } // found the resource item




Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk