Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to convert HDC to CBitmap?

How to convert HDC to CBitmap?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TooShy2Talk
    wrote on last edited by
    #1

    Hello guys, I wanted to convert an HDC to a CBitmap object but I don't know how to. How can I possibly manipulate HDC and probably create a CBitmap from it. Hope you can help me with this one. It will be a big help. Thanks.

    H enhzflepE 2 Replies Last reply
    0
    • T TooShy2Talk

      Hello guys, I wanted to convert an HDC to a CBitmap object but I don't know how to. How can I possibly manipulate HDC and probably create a CBitmap from it. Hope you can help me with this one. It will be a big help. Thanks.

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Can you more explain HDC is a handle to device context and HBITMPA is a handle to a bitmap,for make a bitmap you can use of CreateCompatibaleBitmap.

      1 Reply Last reply
      0
      • T TooShy2Talk

        Hello guys, I wanted to convert an HDC to a CBitmap object but I don't know how to. How can I possibly manipulate HDC and probably create a CBitmap from it. Hope you can help me with this one. It will be a big help. Thanks.

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #3

        Here's some code that will retrieve the currently selected bitmap from a HDC. It should be noted that GetCurrentObject will only return a valid BITMAP if the supplpied HDC is a memory DC. If I tried to do dcBmp = (HBITMAP)GetCurrentObject(screenDC, OBJ_BITMAP), I'd get garbage results. If the DC you're trying to copy is not a memory DC, the alternative you have is to determine the position on screen of the image, then do a BitBlt from the screenDC to a memoryDC that has already got a HBITMAP selected onto it. Note that having a look into the BitBlt, SelectObject, & CreateCompatibleBitmap may be necessary.

        HDC screenDC, memDC;
        char buffer\[200\];
        BITMAP bmInfo;
        HBITMAP dcBmp;
        
        screenDC = GetWindowDC(hwnd);
        memDC = CreateCompatibleDC(screenDC);
        HBITMAP myBmp = CreateCompatibleBitmap(memDC, 100, 100);
        SelectObject(memDC, myBmp);
        
        dcBmp = (HBITMAP)GetCurrentObject(memDC, OBJ\_BITMAP);
        if (dcBmp)
        {
            GetObject(dcBmp, sizeof(bmInfo), &bmInfo);
            sprintf(buffer, "Bitmap is: %d x %d pixels.", bmInfo.bmWidth, bmInfo.bmHeight);
            MessageBox(hwnd, buffer, "Title", MB\_OK);
        }
        else
            MessageBox(hwnd, "Error", "Title", MB\_ICONEXCLAMATION);
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups