ronag
ronag

Reputation: 51255

Win32 Device Context without Window

In my application i need to create HBITMAP objects to which I render and from where I copy the result.

I use the function "CreateDIBSection" to create these bitmaps, however this function required a DC (Device Context) as first parameter. Currently I get this by calling GetDC(hWnd) on the main windows handle (hWnd). But I would like to be able to create HBITMAPS without the requirement of having an application window, without some kind of in memory DC, Is this possible?

Upvotes: 5

Views: 3107

Answers (3)

gugu2
gugu2

Reputation: 11

try this. it worked.

HDC hdcScreen = ::GetDC( NULL );
HDC hdcMemDC = ::CreateCompatibleDC(hdcScreen); 
HBITMAP hbmScreen = ::CreateCompatibleBitmap(hdcScreen, cx, cy);
HBITMAP hOldBitmap  =  (HBITMAP) ::SelectObject(hdcMemDC, hbmScreen);

    MyImageDraw(hdcMemDC, ...);

    // The drawing image is held in hBitmap. You can save it
HBITMAP hBitmap = (HBITMAP)::SelectObject(hdcMemDC,  hOldBitmap); 

    // save The trend image into c:\test.bmp
    PBITMAPINFO pbi = CreateBitmapInfoStruct(hBitmap);
CreateBMPFile("C:\\Temp\\test.bmp", pbi, hBitmap, hdcMemDC);

    //Clean up
::DeleteObject(hbmScreen);
::DeleteObject(hdcMemDC);
::ReleaseDC( NULL, hdcScreen ); 

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941417

You can get one with CreateDC for the display:

 HDC hDc = CreateDC(L"DISPLAY", NULL, NULL, NULL);

Cleanup with DeleteDC(). It is only used to initialize the palette for bitmaps with indexed format. NULL might work if you don't use such a format, never tried it.

Then there's GDI+, #include <gdiplus.h> and the Bitmap class...

Upvotes: 3

Will A
Will A

Reputation: 24988

CreateCompatibleDC(NULL) will create you a device context that is compatible with the screen - which sounds like it would be ideal in the situation.

Upvotes: 4

Related Questions