CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    [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.

  2. #2
    Join Date
    Aug 2006
    Posts
    157

    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.

    For more information from microsoft on this matter check out:
    http://msdn.microsoft.com/en-us/libr...83(VS.85).aspx

    s

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How do you permanently embed a bitmap into an executable?

    Here you go.
    Attached Files Attached Files
    Best regards,
    Igor

  4. #4
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    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!

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: How do you permanently embed a bitmap into an executable?

    Man, this is pure Win32, please take care to have a more intent look at the sample.
    Best regards,
    Igor

  6. #6
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Question 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.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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.
    Best regards,
    Igor

  8. #8
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Re: How do you permanently embed a bitmap into an executable?

    Quote 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?

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include "gdiplus.h"
    //#include <crtdbg.h>
    //#include "resource.h"
    #define IDR_PICT   1000
    
    //	HMODULE   hMod;
    using namespace Gdiplus;
    
    
    
        HINSTANCE hMod;
        FARPROC *fp, *fp2, *fp3;
    
    
    
    
    typedef struct _ComInit
    {
    	HRESULT hr;
    	_ComInit() { hr = CoInitialize(NULL); }
    	~_ComInit() { if (SUCCEEDED(hr)) CoUninitialize(); }
    } ComInit;
    
    ComInit comInit;
    
    HBITMAP g_hbm = NULL;
    BITMAP  g_bm  = {0};
    
    void OnPaint(HWND hWnd)
    {
    	PAINTSTRUCT ps = {0};
    	HDC hdc = BeginPaint(hWnd, &ps);
    	LPCTSTR text = _T("Failed to load");
    
    	if (g_hbm)
    	{
    		HDC ddc = GetDC(GetDesktopWindow());
    		HDC cdc = CreateCompatibleDC(ddc);
    		HBITMAP hbm = CreateCompatibleBitmap(ddc, g_bm.bmWidth, g_bm.bmHeight);
    		HBITMAP hbmOld = (HBITMAP)SelectObject(hdc, hbm);
    		HBITMAP hbmol2 = (HBITMAP)SelectObject(cdc, g_hbm);
    		BitBlt(hdc, 0, 0, g_bm.bmWidth, g_bm.bmHeight, cdc, 0, 0, SRCCOPY);
    		SelectObject(hdc, hbmOld);
    		SelectObject(cdc, hbmol2);
    		DeleteDC(cdc);
    		ReleaseDC(GetDesktopWindow(), ddc);
    	}
    	else
    		TextOut(hdc, 10, 10, text, _tcslen(text));
    	EndPaint(hWnd, &ps);
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
    {
    	switch (msg)
    	{
    	case WM_PAINT:
    		OnPaint(hWnd);
    		return 0;
    	case WM_CLOSE:
    		DestroyWindow(hWnd);
    		PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(hWnd, msg, wp, lp);
    }
    
    HBITMAP LoadPictResource(LPCTSTR resId, LPCTSTR resType)
    {
    	HRSRC hRes = FindResource( NULL, resId, resType);
    
    
    	DWORD dwFileSize = SizeofResource( NULL, hRes );
    
    	HGLOBAL hSplash = LoadResource( NULL, hRes );
    	
    
    	LPVOID pSplash = LockResource( hSplash );
    
    	LPVOID pvData = NULL;
    	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
    	
    
    	pvData = GlobalLock(hGlobal);
    	
    
    	DWORD dwBytesRead = 0;
    	CopyMemory( pvData, pSplash, dwFileSize );
    	GlobalUnlock(hGlobal);
    
    	LPSTREAM pstm = NULL;
    	HRESULT hr = CreateStreamOnHGlobal(hGlobal, FALSE, &pstm);
    	
    
    	HBITMAP hbm = NULL;
    	/*another modification*/
                 *fp::Bitmap* pBitmap = Gdiplus::Bitmap::FromStream(pstm);
        if (pBitmap)
        {
            pBitmap->GetHBITMAP(*fp::Color(0, 0, 0), &hbm);
        }
        pstm->Release();
        GlobalFree(pvData);
        return hbm;
    }
    
    int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmd, INT nShow)
    {
    
         hMod = LoadLibrary(
    "C:\\Users\\MATT\\Desktop\\custormres\\HOWTO_display_JPEG_from_resource\\NewFolder(2)\\gdiplus.dll");
    
        
        fp = (Gdiplus*)GetProcAddress(hMod, "Gdiplus");
     /* I am not sure how to cast the Gdiplus namespace*/   
       
    
        
        
        ULONG_PTR gdiplusToken;
    	*fp::GdiplusStartupInput gdiplusStartupInput;
    	
    /*Here - you can see I don't know how to reference a namespace*/
    
                    if (*fp::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != *fp::Ok)/*another modifacation*/
    	{
    		return 0;
    	}
    
    	HBITMAP hbm = LoadPictResource(MAKEINTRESOURCE(IDR_PICT), RT_HTML);
    	if (hbm)
    	{
    		GetObject(hbm, sizeof(g_bm), &g_bm);
    		g_hbm = hbm;
    	}
    
    	WNDCLASS wc = {0};
    	wc.lpfnWndProc = WndProc;
    	wc.hInstance   = hInst;
    	wc.lpszClassName = _T("Resource Display");
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
    	wc.style = CS_VREDRAW | CS_HREDRAW;
    
    	if (!RegisterClass(&wc))
    		return 0;
    
    	HWND hWnd = CreateWindow(
    		wc.lpszClassName, 
    		wc.lpszClassName, 
    		WS_DLGFRAME|WS_CAPTION|WS_SYSMENU, 
    		0, 0, 
    		hbm ? g_bm.bmWidth : 200, 
    		hbm ? g_bm.bmHeight : 200, 
    		NULL, NULL, hInst, NULL);
    
    	if (!hWnd)
    		return 0;
    
    	ShowWindow(hWnd, SW_SHOW);
    	UpdateWindow(hWnd);
    
    	MSG msg;
    	while (0 < GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
           /*another modification*/
    	*fp::GdiplusShutdown(gdiplusToken);
    	return 0;
    }



    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.

  9. #9
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: How do you permanently embed a bitmap into an executable?

    To embed bitmap do this:

    You add a line in your RC file like below:
    IDB_BITMAP 100 DISCARDABLE "res\\bitmap.bmp"

    where IDB_BITMAP is the value for the bitmap resource ID. You are using 22, so plug in that value.

  10. #10
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Talking 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.

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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.
    Attached Files Attached Files
    Last edited by Igor Vartanov; June 23rd, 2008 at 09:07 AM.
    Best regards,
    Igor

  12. #12
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    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.

  13. #13
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Talking 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...

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