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. Capturing HDC

Capturing HDC

Scheduled Pinned Locked Moved C / C++ / MFC
comgraphicshelpquestion
4 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.
  • R Offline
    R Offline
    rocky_pulley
    wrote on last edited by
    #1

    I have an issue where I have a function that takes an HDC and draws to it. Instead of just displaying the data in a window, I want to write it to a bitmap file. I don't want to show the window at all, so I'm hoping that I can do this with a hidden window some how. I've tried calling GetCurrentBitmap() on the CDC object and that did not give me a valid bitmap. I also tried the following: hDC = GetDC(hDCWnd); hBmp = CreateCompatibleBitmap(hDC, 500, 500); SelectObject(hDC, hBmp); MyFuncToDraw(hDC); CBitmap* bitmap = CBitmap::FromHandle(hBmp); but that only gave me a blank bitmap. Does anyone have any idea how I can get a valid CBitmap so I can save it to a file? Is this even possible?? -- Rocky Dean Pulley -- DreamSys Software -- http://www.dreamsyssoft.com

    E 1 Reply Last reply
    0
    • R rocky_pulley

      I have an issue where I have a function that takes an HDC and draws to it. Instead of just displaying the data in a window, I want to write it to a bitmap file. I don't want to show the window at all, so I'm hoping that I can do this with a hidden window some how. I've tried calling GetCurrentBitmap() on the CDC object and that did not give me a valid bitmap. I also tried the following: hDC = GetDC(hDCWnd); hBmp = CreateCompatibleBitmap(hDC, 500, 500); SelectObject(hDC, hBmp); MyFuncToDraw(hDC); CBitmap* bitmap = CBitmap::FromHandle(hBmp); but that only gave me a blank bitmap. Does anyone have any idea how I can get a valid CBitmap so I can save it to a file? Is this even possible?? -- Rocky Dean Pulley -- DreamSys Software -- http://www.dreamsyssoft.com

      E Offline
      E Offline
      emadns
      wrote on last edited by
      #2

      You need to create a memory dc hDC = GetDC(hDCWnd); // create the memory dc HDC hdcMem = ::CreateCompatibleDC(hDC); hBmp = ::CreateCompatibleBitmap(hDC, 500, 500); // select the bitmap into the memory device context instead HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdcMem, hBmp); // pass the memory dc handle instead MyFuncToDraw(hdcMem); CBitmap* bitmap = CBitmap::FromHandle(hBmp); ... after done ::SelectObject(hdcMem, hbmpOld); ::DeleteDC(hdcMem);

      R 1 Reply Last reply
      0
      • E emadns

        You need to create a memory dc hDC = GetDC(hDCWnd); // create the memory dc HDC hdcMem = ::CreateCompatibleDC(hDC); hBmp = ::CreateCompatibleBitmap(hDC, 500, 500); // select the bitmap into the memory device context instead HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdcMem, hBmp); // pass the memory dc handle instead MyFuncToDraw(hdcMem); CBitmap* bitmap = CBitmap::FromHandle(hBmp); ... after done ::SelectObject(hdcMem, hbmpOld); ::DeleteDC(hdcMem);

        R Offline
        R Offline
        rocky_pulley
        wrote on last edited by
        #3

        Ok, here's what I'm trying, it's just giving me a blank (black) 500x500 bitmap: CDC memDC; memDC.CreateCompatibleDC(GetDC()); CBitmap *bmp = new CBitmap(); bmp->CreateCompatibleBitmap(&memDC, 500, 500); CBitmap *old = memDC.SelectObject(bmp); CRect rc(0, 0, 500, 500); memDC.FillRect(rc, new CBrush(RGB(0, 255, 255))); memDC.TextOut(50, 50, "BLAH"); //This stuff writes the bitmap and I know that it works. HANDLE hDib = DDBToDIB(*bmp, BI_RGB, NULL); WriteDIB("c:\\test\\testbmp.bmp", hDib); memDC.SelectObject(old); I have also tried to put the "memDC.SelectObject(old);" line before the bitmap writes and it gives the same problem. I know that the bitmap writing code works, I've used it before on CBitmaps just fine, so I'm sure that the problem exists in this drawing code somewhere. This code is being run from a CDialog, so that's where the GetDC() comes from. -- Rocky Dean Pulley -- DreamSys Software -- http://www.dreamsyssoft.com

        C 1 Reply Last reply
        0
        • R rocky_pulley

          Ok, here's what I'm trying, it's just giving me a blank (black) 500x500 bitmap: CDC memDC; memDC.CreateCompatibleDC(GetDC()); CBitmap *bmp = new CBitmap(); bmp->CreateCompatibleBitmap(&memDC, 500, 500); CBitmap *old = memDC.SelectObject(bmp); CRect rc(0, 0, 500, 500); memDC.FillRect(rc, new CBrush(RGB(0, 255, 255))); memDC.TextOut(50, 50, "BLAH"); //This stuff writes the bitmap and I know that it works. HANDLE hDib = DDBToDIB(*bmp, BI_RGB, NULL); WriteDIB("c:\\test\\testbmp.bmp", hDib); memDC.SelectObject(old); I have also tried to put the "memDC.SelectObject(old);" line before the bitmap writes and it gives the same problem. I know that the bitmap writing code works, I've used it before on CBitmaps just fine, so I'm sure that the problem exists in this drawing code somewhere. This code is being run from a CDialog, so that's where the GetDC() comes from. -- Rocky Dean Pulley -- DreamSys Software -- http://www.dreamsyssoft.com

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          rocky_pulley wrote: bmp->CreateCompatibleBitmap(&memDC, 500, 500); try using GetDC() instead of &memDC. Cleek | Image Toolkits | Thumbnail maker

          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