[RESOLVED] How do you permanently embed a bitmap into an executable?
Compiler = Dev-C++ V 4.9.9.2.
Operating System = Windows Vista
Hello, and good day.
I am a novice Win32 via C++ programmer, and I was wanting to know how to embed a bitmap or any file type for that matter - into one compiled executable. So I want to merge multiple files into one compiled executable.
I am a complete novice and I really don't know a lot about programming so I think that it would seriously help me learn if I saw a small working example of code. The code would have to be very simple and down to the bare bones. So anything that is not necessary should be left out, and anything that can be simplified should be.
If for some reason a small working example is not feasible then I would appreciate the names and descriptions of the functions or processes necessary in order to achieve this.
I have searched the web and this forum and I can't seem to find a good example on this topic. I searched the words custom resource, embed, merge, binary... Perhaps I just don't know what to look for.
---------I just asked a question a few days ago so I don't believe I deserve anymore help for now. But I just want to say that after searching this forum I have observed that a lot of the respondents on this forum seem to very helpful and nice. Some forums are not like this. I just want to say that even if no one helps me, you guys still seem pretty cool.
---------The best of wishes to you all in every endeavor!
Last edited by kmkkra; June 20th, 2008 at 05:50 AM.
Re: How do you permanently embed a bitmap into an executable?
One method of doing this is to use resources. Typically you define a resource in a .rc file in your project which is compiled by a resource compiler. You convert resources into usable objects using a macro like MAKEINTRESOURCE.
Re: How do you permanently embed a bitmap into an executable?
Thank you for the reply sockman. I will research the info on msdn.
Thank you very much Igor Vartanov for taking the time writting out that example. Unfortunately it seems too complicated for me to really learn from, and I don't know mfc or have a VC++ compiler. However I will try to learn what ever I can from your example. Thank you very much though...
If I could still get some more info on the matter or, better yet, a working and - simple - example in pure Win32 that would be awesome!
Re: How do you permanently embed a bitmap into an executable?
Sorry Igor Vartanov I am indeed a novice. I saw the Gdiplus namespace ,gdiplus.h file, and crtdbg.h file and I assumed that they were associated with VC++. This is because I don't seem to have gdiplus.h or crtdbg.h or their libraries with my version of Dev-C++. I am not sure where to get these files but I will try to find them anyhow.
Also it seems there could be a simpler example, however if your example is all I can get then I am greatful.
I have another question. Are there any other steps that need to be taken in order to compile your code?
Thank you for your help!
Last edited by kmkkra; June 21st, 2008 at 09:16 PM.
Re: How do you permanently embed a bitmap into an executable?
Okay, now it seems like we're getting more close to really valuable discussion. According to your original question, you have an intention to load any picture format (because that is what I take for mentioning 'picture'), that's why the sample describes a JPEG load, and that's why Gdi+ appeared there. In case you've been meaning only BMP picture format, the more basic (and simple) approach available (though trading the size to simplicity): LoadImage or LoadBitmap API. Use it instead of LoadPictResource function along with the BITMAP resource type in .rc, and comment out all Gdi+ and COM initialization/shutdown stuff.
BTW, crtdbg.h has to do with _ASSERTE macros only, so you can easily get rid of that by merely commenting that out.
Last edited by Igor Vartanov; June 20th, 2008 at 12:18 PM.
Re: How do you permanently embed a bitmap into an executable?
Originally Posted by Igor Vartanov
According to your original question, you have an intention to load any picture format (because that is what I take for mentioning 'picture'),
AH...Now I see. your code was for any image. Well, that is what I asked for, and what I needed. I will definitely be using it, and even if I can't reduce it's complexity I will be rather content.
Now, for the more simpler approach of loading only bitmaps using LoadImage() or LoadBitmap() I only know how to load a bitmap from a bitmap file. So the bitmap would not be embeded into the executable. I have learned that in order to embed a bitmap into one's executable it must be converted into hexadecimal values or binary or something, I'm not sure. Then those values must be placed in an .rc file or maybe a header file, I'm not sure. Well, I don't know how to convert a bitmap into these values or where exactly to place them or how to read them into the program while it's running. If the info goes into an .rc file then I'm not sure where to place it since I don't know a lot about the syntax used for .rc files. I must admit though, I think I'm onto something. I think the functions "CreateDIBitmap", "GetDIBits", "SetBitmapBits" may be the fuctions that I will use to accomplish this task, but I don't know.
Perhaps if I study long enough I might be able to figure it out, but if you want you can help me - maybe even give me another example for this particular type of loading. So far I only know how to load a bitmap by means of an .rc file through loading from a bitmap file rather than from the executable itself.
Here is an example:
Code:
#define bitMap 22
LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
HDC hDC, MemDC;
PAINTSTRUCT Ps;
HBITMAP bmp;
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmp = LoadBitmap(hInst, MAKEINTRESOURCE(bitMap));
// Create a memory device compatible with the above DC variable
MemDC = CreateCompatibleDC(hDC);
// Select the new bitmap
SelectObject(MemDC, bmp);
// Copy the bits from the memory DC into the current dc
BitBlt(hDC, 10, 10, 450, 400, MemDC, 0, 0, SRCCOPY);
// Restore the old bitmap
DeleteDC(MemDC);
DeleteObject(bmp);
EndPaint(hWnd, &Ps);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
///////////////////////////////////////RESOURCE////////////////////////////////////////////
#include <windows.h>
#define bitMap 22
bitMap BITMAP "C:\\Users\\MATT\\Desktop\\bitMap.bmp"
Another thing that you might not mind helping me with is your code that you gave me. I got the Gdiplus.dll and inlude files of codeguru but now I have to use "LoadLibrary()" to load the Gdiplus namespace into your program. The problem is that I don't know how to reference a namespace through this means. I do, however know how to reference a function. Any way here is your program with the "LoadLibrary" modification.
My request is- Can you fix the modifacations that I made to your program?
Lastly, I want to thank you Igor Vartanov for taking the time to help me. Even if you don't respond, that's okay... You have done your share of helping on this website. I will figure this stuff out eventually, and continue to study the msdn library as sockman suggested.
But anyway------Once again, the best of wishes to you brother
Last edited by kmkkra; July 14th, 2008 at 06:47 PM.
Re: How do you permanently embed a bitmap into an executable?
Kirants thank you so much. That was a very easy solution. The problem is that there doesn't seem to be much info on .rc files. Thank you so much.
And Igor Vartanov, I will definitely be using the code example you gave me. It's not entirely that complicated now that I know that it's not VC++. If you want to you can still fix the modifications that I made to your program. Otherwise this matter has been resolved
My thanks goes out to Igor Vartanov, Kirants and sockman for your help!
----Prosper and live long...
Last edited by kmkkra; June 21st, 2008 at 06:41 AM.
Re: [RESOLVED] How do you permanently embed a bitmap into an executable?
So far I only know how to load a bitmap by means of an .rc file through loading from a bitmap file rather than from the executable itself.
There is no way "to load a bitmap by means of an .rc file". Just because .rc file merely is a resource script describing composition of resource section of executable, and that script has nothing to do with runtime loading.
And loading itself is performed by LoadBitmap API. You never need to have the original bitmap file next to your exe after building it, because your bitmap appears embedded into the executable. You can tell that by the exe file size; it must be slightly bigger than a bitmap file. Just move exe anywhere you want or kill the source bmp - and see how exe still loads the bitmap with no problem.
Last edited by Igor Vartanov; June 23rd, 2008 at 09:07 AM.
Re: [RESOLVED] How do you permanently embed a bitmap into an executable?
Thank you very much Igor Vartanov for the more specific example and explanation. You are too kind! I am thinking about what you've said, and will make the most of it.
-----Thanks friend...
Last edited by kmkkra; June 23rd, 2008 at 07:04 AM.
Re: [RESOLVED] How do you permanently embed a bitmap into an executable?
Hello, it's me again. I just wanted to say thanks again to Igor Vartanov for the code in the "HOWTO_display_JPEG_from_resource.zip" file. I finally got your code working.
I figured out that I had to include the "ole2.h" file and it's library "libole32.a" (which is Mingw compiler compatible) for the COM functions. I found the Gdi+ header files and GdiPlus.lib on codeguru, and figured out how to convert the .lib into .a with the "reimp" program.
I also found out that I had to convert the RT_HTML constant to it's string equivalent "HTML". See the code you gave me would not work otherwise - for some reason.
Now I am able to embed small jpgs into my program and use them for animation. Your code allows me to use a number of image formats, and considerably reduce my program's size.
-------THANKS again Igor for your examples!
--------May you excel, succeed, and prosper in all ways...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.