简体   繁体   中英

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?

CreateCompatibleDC(NULL)将创建一个与屏幕兼容的设备上下文 - 听起来它在这种情况下是理想的。

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

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 ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM