Capturing HDC
-
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
-
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
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);
-
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);
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
-
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
rocky_pulley wrote: bmp->CreateCompatibleBitmap(&memDC, 500, 500); try using GetDC() instead of &memDC. Cleek | Image Toolkits | Thumbnail maker