Win32 device context without window

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

I use the CreateDIBSection function to create these bitmaps, however, for this function, DC (Device Context) is required as the first parameter. I am currently getting this by calling GetDC (hWnd) in the main window handle (hWnd). But I would like to be able to create HBITMAPS without requiring an application window without any DC memory, is this possible?

+3
source share
3 answers

CreateCompatibleDC(NULL) will create a screen-compatible device context for you that sounds as if it would be ideal in a situation.

+2
source

CreateDC :

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

DeleteDC(). . NULL , , .

GDI +, #include <gdiplus.h> Bitmap...

+2

try it. he 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 ); 
0
source

Source: https://habr.com/ru/post/1757543/


All Articles