Thread: GDI Question

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Bensalem PA
    Posts
    58
    Rep Power
    14

    GDI Question


    I've been looking around on MSDN and the internet and can't find how to draw bitmaps with GDI(not plus). All I want to know is the syntax for drawing a bitmap(from a resource or external file) to a device context at a given coordinate. Thanks for any help.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    You're looking for the BitBlt() function.
    http://msdn.microsoft.com/library/de...tmaps_0fzo.asp
    Hope this helps! :D
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Bensalem PA
    Posts
    58
    Rep Power
    14
    Yeah I found that much but how do I load and get a device context to a bitmap file or resource?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    You can load the image with LoadImage(), if loading from file, or LoadBitmap(), if loading from resource. Next, you need to get the device context for your current window, which you can obtain with GetDC(). After that, you have to create a compatible device context with GetCompatibleDC() and use BitBlt() to draw to it. The code would go something like this:
    Code:
    HDC hDC, hDCBmp;
    HBITMAP hBitmap;
    BITMAP bmp; 
    
    hBitmap = LoadBitmap(...) or (HBITMAP)LoadImage(...);
    hDC = GetDC(hWnd);
    hDCBmp = CreateCompatibleDC(hDC);
    
    GetObject(hBitmap, sizeof(BITMAP), &bmp);
    SelectObject(hDCBmp, hBitmap);
    BitBlt(hDC,100,100, bmp.bmWidth,bmp.bmHeight, hDCBmp, 0,0, SRCCOPY);
    
    DeleteObject(hBitmap);
    DeleteDC(hDCBmp);
    ReleaseDC(hDC);
    Please note that this code is off the top of my head and I did not test it. However, it ought to be pretty close to what you need. Don't forget to check MSDN for more information on the functions. Hope this helps! :D
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Bensalem PA
    Posts
    58
    Rep Power
    14
    Thank you, i'll try it later.
  10. #6
  11. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    This code snippet is a memory leak. The hBitmap will never be released as it is still selected into the HDC.

    You can not delete a GDI object while it is selected into a HDC.

    You must catch the returns of SelectObject() .
    Before the function exits you must restore the HDC to its original resources and delete the created ones.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Bensalem PA
    Posts
    58
    Rep Power
    14
    yeah thanks I noticed that too. I had found the LoadBitmap function in MSDN before but it said that it was depricated and I was wondering what is supposed to replace it?
  14. #8
  15. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    LoadImage()
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo