Thread: bitmaps

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    bitmaps

    i did a search about bitmaps but i still cant get it to work.

    Code:
    HDC hdc,hdc2;
    HBITMAP hBit;
    
    hBit=(HBITMAP)LoadImage(NULL,"pic.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
    			
    hdc=GetDC(hwnd);
    hdc2=CreateCompatibleDC(hdc);
    
    SelectObject(hdc2,hBit);
    
    ReleaseDC(hwnd,hdc);
    nothing happens. pic.bmp is in the working directory.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    nothing i ever do to load bitmaps ever work for me either. and theres no resource or path problem. theres something strange!
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  3. #3
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43
    Maybe this will help you.
    Code:
    int LoadBitmapFile(char* FileName, int x, int y, HWND hwnd)
    
    
        {
        BITMAP bm;
        HDC hdc = GetDC(hwnd);
        BitHandle = (HBITMAP)LoadImage(NULL, FileName, IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
        if(BitHandle == NULL)
    
    
            {
            MessageBox(0, "Error loading the specified bitmap", "Program Error!",
            MB_ICONERROR | MB_SYSTEMMODAL | MB_OK);
        }
        HDC dc = CreateCompatibleDC(hdc);
        SelectObject(dc, BitHandle);
        GetObject(BitHandle, sizeof(BITMAP), &bm);
        BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, dc, 0,0, SRCCOPY); 
        ReleaseDC(hwnd, hdc);
        return(0);
    }
    to load a bmp call the function as follows : LoadBitmapFile("bitmap.bmp",50,50,hwnd);
    hope this helps.

    P.S. I found it in the Net and it saved me ones.
    C++ rulez!!!

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Just load it in as a resource. In resource.h you define a bitmap name. In script.rc you:
    Code:
    #include "resource.h"
    //you should have done #define IDB_PIC 101 in resource.h
    IDB_PIC BITMAP "pic.bmp"
    then in your main program you do:
    Code:
    #include "resource.h"
    #include <windows.h>
    
    HBITMAP pic = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PIC));
    ....and you should know how to use BitBlt()

    if you want more specific code I'll post an example when I get home

    oh and alex, i think in your example you're going to have a resource leak because you never select the defaults back into dc. i dunno, i could be wrong.

    and benny, make sure you have "GetModuleHandle(NULL)" as the first param, not just "NULL" otherwise it won't work. and of course you know that is not a full main.cpp. put that line i gave (loadbitmap one) under WM_CREATE.

    just a quick example for blitting:
    Code:
    HDC hDC = GetDC(hWnd);
    HDC hDCMem = CreateCompatibleDC(hDC);
    HDC hDCBuffer = CreateCompatibleDC(hDC);
    HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
    HBITMAP hbmOldMem = (HBITMAP)SelectObject(hDCMem, pic);
    HBITMAP hbmOldBuff = (HBITMAP)SelectObject(hDCBuffer, hbmBuffer);
    
    FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH));
    
    BitBlt(hDCBuffer, x, y, width, height, hDCMem, 0, 0, SRCAND);
    BitBlt(hDC, 0, 0, prc->right, prc->bottom, hDCBuffer, 0, 0, SRCAND);
    
    SelectObject(hDCBuffer, hbmOldBuff);
    SelectObject(hDCMem, hDCOldMem);
    DeleteObject(hbmBuffer);
    DeleteDC(hDCBuffer);
    DeleteDC(hDCMem);
    like i said when i get home i'll post a whole program example if you want

    btw, i just typed this quick at school, so there might be some stupid errors or somethign
    Last edited by Leeman_s; 01-08-2003 at 02:44 PM.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    leeman, damn you resource users, you're all cheaters. its so much more fun to work out how to actually code rather than letting your development studio do it all for you. but thanks for the other code, i'll give it a try.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    leeman, i was reading over the blitting example and i couldn't work out what the idea was. why blit from one hdc to another, then finally to the screen? wouldn't that just take more time?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Letting MSVC doing it is totally different than what I did.

    I showed you the old-school method. Here's an example of my resource file for my most recent program. IN TEXT, not PICTURES of menus and other **** like that.

    Code:
    #include "resource.h"
    #include <windows.h>
    
    IDI_MYICON ICON "myicon.ico"
    IDB_C BITMAP "c.bmp"
    IDB_T BITMAP "t.bmp"
    
    IDR_MENU1 MENU
    BEGIN
        POPUP "&File"
    	BEGIN
    	    MENUITEM "N&ew", ID_FILE_NEW
    	    MENUITEM "E&xit", ID_FILE_EXIT
    	END
    	
    	POPUP "&Music"
    	BEGIN
    	    MENUITEM "T&oggle Music On/Off", ID_FILE_MUSIC
    		MENUITEM "S&witch Beat", ID_FILE_SWITCH
    	END
    END

  8. #8
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Oh, and as for the extra DC. If you are blitting on more than one picture, you blit everything (all pics) from hDCMem onto hDCBuffer. Then finally you blit hDCBuffer onto your main DC.

    The reason is this:

    If you blit right from hDCMem to your main DC, you need to pick the coordinates of where to blit it. If you blit something at say, the point 0,0, and then at 5,5 you're going to have to use FillRect() or a similar function on the main DC in order to not have leftovers of the "animation" like a moving ball. This creates flicker. If you use FillRect on hDCBuffer, then blit it onto the main DC at 0,0 first of all you are painting over the whole screen so you don't get any leftovers. Second, there isn't flickering.

  9. #9
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    ....if I didn't explain that very good, just say. I'll post two different programs, one using my method and the other not using the method. You'll see the difference.

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    very clever blitting. it reminds me of the good old C days, where you'd fill a buffer and write it directly to screen memory. and sorry about the resource stuff, i'm an arrogant bastard who doesn't like things being easy. im not happy unless i type a years worth of code to make a menu or an accelerator, etc.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    .....not all IDE's have resource editors

    But most of the time you can get away with the visual editor for resources. Why type it out when you can just click a few times? "Cheating" is a word n00bs like to use because they are jealous that they don't know the easier way to do it. Not that I'm calling you one or nothing, just making the point.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    thanks for keeping me in line, frenchfry. and whats a n00b?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing bitmaps efficiently
    By scwizzo in forum Windows Programming
    Replies: 28
    Last Post: 06-30-2009, 08:25 PM
  2. DX9 Not Displaying Bitmaps
    By Sentral in forum Game Programming
    Replies: 9
    Last Post: 01-31-2006, 05:35 AM
  3. MFC: Multiple clicks shows multiple bitmaps - how?
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2004, 07:25 PM
  4. Loading bitmaps with createfile() +CreateDIBSection
    By Stevo in forum Windows Programming
    Replies: 7
    Last Post: 05-13-2004, 09:22 PM
  5. using standard bitmaps in the toolbar editor
    By Kibble in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2002, 08:43 PM