|
-
May 10th, 2011, 04:44 AM
#1
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.
-
May 10th, 2011, 05:00 AM
#2
Re: Embedding and loading custom resource file (jpeg) in the project
- you have not included the necessary SDK headers, e.g. <windows.h>
- you have not included the header where resource IDs are defined, e.g. "resource.h".
-
May 10th, 2011, 06:41 AM
#3
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 ►►
-
May 11th, 2011, 03:29 AM
#4
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.
-
May 11th, 2011, 04:13 AM
#5
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
-
May 11th, 2011, 04:31 AM
#6
Re: Embedding and loading custom resource file (jpeg) in the project
 Originally Posted by VictorN
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?
-
May 11th, 2011, 04:38 AM
#7
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?
-
May 11th, 2011, 04:43 AM
#8
Re: Embedding and loading custom resource file (jpeg) in the project
 Originally Posted by detox
... 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
-
May 11th, 2011, 05:07 AM
#9
Re: Embedding and loading custom resource file (jpeg) in the project
 Originally Posted by VictorN
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.
-
May 11th, 2011, 05:21 AM
#10
Re: Embedding and loading custom resource file (jpeg) in the project
What IDE are you using: VC6, VS2005/2008/2010?
Victor Nijegorodov
-
May 11th, 2011, 05:49 AM
#11
Re: Embedding and loading custom resource file (jpeg) in the project
 Originally Posted by VictorN
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.
-
May 11th, 2011, 07:48 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|