CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2011
    Posts
    6

    Embedding and loading custom resource file (jpeg) in the project

    Hello,

    I am working on a Visual Studio 2008 project and have been experiencing some troubles with custom (jpg) resource files. So far I have managed to add the resource into my project. I can see "IDR_JPEG1" correctly in the resource view. However, I have no idea how I can load and use that resource file in my code.

    After some googling I figured out that I maybe I should use loadResource and findResource methods. I tried with the following piece of code:

    HRSRC res = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_JPEG1),"JPEG");
    GLOBAL mem = LoadResource(GetModuleHandle(NULL), res);
    void *data = LockResource(mem);

    But during compiling I receive undeclared identifier errors:

    error C2065: 'HRSRC' : undeclared identifier
    error C2065: 'IDR_JPEG1' : undeclared identifier
    error C3861: 'FindResource': identifier not found

    And so on...

    How can I load the jpg file and use it in my code? All the help is appreciated. Thanks.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Embedding and loading custom resource file (jpeg) in the project

    1. you have not included the necessary SDK headers, e.g. <windows.h>
    2. you have not included the header where resource IDs are defined, e.g. "resource.h".
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Embedding and loading custom resource file (jpeg) in the project

    Many time discussed in forum. Please do a well search.

    'Embedding resource with exe'.. this keyword can perform a well search I guess
    ◄◄ hypheni ►►

  4. #4
    Join Date
    May 2011
    Posts
    6

    Re: Embedding and loading custom resource file (jpeg) in the project

    Thank you for your support. I had forgottend to include the necessary files. That fixed the original problem. However, I am still having problems with obtaining the JPEG data properly. Right now I am using the following code:

    HMODULE mod = GetModuleHandle(TEXT("MyDll.dll"));
    HRSRC res = FindResource(mod, MAKEINTRESOURCE(IDR_JPEG1),TEXT("JPEG"));
    HGLOBAL mem = LoadResource(mod, res);
    size_t sz = SizeofResource(mod, res);
    unsigned char* data = (unsigned char*)LockResource(mem);

    if (data)
    {
    std:fstream outputFile("C:\\temp.jpg");
    // Write image to the file:

    for (int i = 0; i < sz; ++i)
    {
    outputFile << data[i];
    }

    outputFile.close();
    }

    This kinda works. I am able to open the written temp.jpg with Windows Photo Viewer and it seems to be OK. However, when I tried to pass this image to 3rd party library it fails to read the jpeg header and thus the whole image. Data from the resource seems to be somehow corrupted because this happens only when trying to pass the image that has been extracted from the resource (original file works OK).

    I know this is a bit out of the scope of the original question, but I wonder if I am still missing something when reading the data from resource?

    Thanks for all the help.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Embedding and loading custom resource file (jpeg) in the project

    What does the documentation of this "3rd party library" say you about the expected format of .jpeg image in the .exe file?
    Victor Nijegorodov

  6. #6
    Join Date
    May 2011
    Posts
    6

    Re: Embedding and loading custom resource file (jpeg) in the project

    Quote Originally Posted by VictorN View Post
    What does the documentation of this "3rd party library" say you about the expected format of .jpeg image in the .exe file?
    The libraru I am using is libharu for PDF creation. The documentation doesn't seems to provide any extra information on the expected format. But as I said the original file works. The problem is that the data seems to get corrputed when importing/loading the resource.

    I also noticed that whereas Paint is able to open the written file Photoshop says "Could not complete your request because a JPEG marker segment is too short (the file may be truncated or incomplete).

    Any suggestions?

  7. #7
    Join Date
    May 2011
    Posts
    6

    Re: Embedding and loading custom resource file (jpeg) in the project

    I noticed something else. When I double click my resource in Visual Studio it opens a bitmap editor, even though it is imported as a custom resource. So for curiosity changed the extension of the written file from jpg to bmp and now even Photoshop can open the file. It seems that Visual Studio converts the file into bmp format during importing. Why is that?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Embedding and loading custom resource file (jpeg) in the project

    Quote Originally Posted by detox View Post
    ... It seems that Visual Studio converts the file into bmp format during importing. Why is that?
    How do you "import" this resource and from what?
    Victor Nijegorodov

  9. #9
    Join Date
    May 2011
    Posts
    6

    Re: Embedding and loading custom resource file (jpeg) in the project

    Quote Originally Posted by VictorN View Post
    How do you "import" this resource and from what?
    In the resource view I right clicked on my project, chose "Add -> Resource". In the opening dialog I chose "Import" and selected the my jpg file. I was prompted to enter the type for the custom resource. I named it "JPEG".

    Resource (IDR_JPEG1) shows now correctly under the custom resource node "JPEG". However, if I double click the resource or select "Open binary data" it shows the image in bitmap editor.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Embedding and loading custom resource file (jpeg) in the project

    What IDE are you using: VC6, VS2005/2008/2010?
    Victor Nijegorodov

  11. #11
    Join Date
    May 2011
    Posts
    6

    Re: Embedding and loading custom resource file (jpeg) in the project

    Quote Originally Posted by VictorN View Post
    What IDE are you using: VC6, VS2005/2008/2010?
    Visual Studio 2008.

    I tried to get around this by renaming my .jpg to .dat file before importing. Now I am able to view the binary data correctly instead of bitmap editor. However I still haven't been able to reproduce a proper jpg from the resource. I think I am getting closer though.

    The problem is that imported file and the file written from the resource differ slightly. In the resulted file there seems to be Carriage Return preceeding New Line Feed.

    For instance whereas the original data is

    00 0A FC 80 00 00 27 10 00 0A FC 80 00 00 27 10

    Results in

    00 0D 0A FC 80 00 00 27 10 00 0D 0A FC 80 00 00

    I think this is what corrupts my file at the moment. Any ideas how to get rid of Carriage Returns? I appreciate your help.

  12. #12
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Embedding and loading custom resource file (jpeg) in the project

    Hi,

    You should write it as binary, so open the file as:
    std::ofstream outputFile("C:\\temp.jpg",ios::out | ios::binary);
    Last edited by alanjhd08; May 11th, 2011 at 07:53 AM. Reason: Disable smilies in text

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured