How do I 'fill' a CBitmap with a DC's 'image'
-
I would like to create a CBitmap object from a DC, then draw the bitmap on each successive repaint rather than doing all the drawing stuff again (there is quite a lot, which is why I want to use this approach). So, I know how to blit the CBitmap back to the DC each time the window is repainted, but I don’t know how to copy the stuff I’ve drawn to the DC into the CBitmap. How would I do this? Thanks in advance for any help you can give me. Yours, James Millson
-
I would like to create a CBitmap object from a DC, then draw the bitmap on each successive repaint rather than doing all the drawing stuff again (there is quite a lot, which is why I want to use this approach). So, I know how to blit the CBitmap back to the DC each time the window is repainted, but I don’t know how to copy the stuff I’ve drawn to the DC into the CBitmap. How would I do this? Thanks in advance for any help you can give me. Yours, James Millson
Once I've create a function CopyBitmap(..) to copy a bitmap form one to another. Note that it does not work on all kind of bitmaps (eg 32-bits not ). You should be able to find the way to copy the image from your DC to a CBitmap. Hint: iso SourceDC use your own pointer to DC in the function BitBlt(..). Have fun! Kit BOOL CBetterBitmap::CopyBitmap ( CBitmap* pbmpSource, // IN: Source bitmap CBitmap* pbmpDest // OUT: Target bitmap ) { BOOL bResult = FALSE; // Invalid source bitmap ASSERT(pbmpSource != NULL); // pbmpDest must point to an existing CBitmap instance ASSERT(pbmpDest != NULL); ASSERT(pbmpDest->GetSafeHandle() == NULL); // Quick pointer verification if ( (pbmpSource != NULL) && (pbmpDest != NULL) ) { BITMAP bmp; pbmpSource->GetBitmap(&bmp); // First copy the bitmap information if (pbmpDest->CreateBitmapIndirect(&bmp)) { CDC SourceDC; CDC DestDC; // Now copy the bitmap via display compatible DC's if ( (SourceDC.CreateCompatibleDC(NULL) != FALSE) && (DestDC.CreateCompatibleDC(NULL) != FALSE) ) { CBitmap* pbmpPrevSrc = SourceDC.SelectObject(pbmpSource); CBitmap* pbmpPrevDest = DestDC.SelectObject(pbmpDest); bResult = DestDC.BitBlt(0, // Source Left 0, // Source Top bmp.bmWidth, // Source Width bmp.bmHeight, // Source Height &SourceDC, // Source DC 0, // Target Left 0, // Target Top SRCCOPY); // Raster Operation DestDC.SelectObject(pbmpPrevSrc); SourceDC.SelectObject(pbmpPrevDest); } } } // Return result return bResult; }
-
I would like to create a CBitmap object from a DC, then draw the bitmap on each successive repaint rather than doing all the drawing stuff again (there is quite a lot, which is why I want to use this approach). So, I know how to blit the CBitmap back to the DC each time the window is repainted, but I don’t know how to copy the stuff I’ve drawn to the DC into the CBitmap. How would I do this? Thanks in advance for any help you can give me. Yours, James Millson
If you've got a DC and you want to copy it to a CBitmap, just select the CBitmap into another DC and Blt across. Jonathon