Hello, I am trying to pull out an array of pixels from the shared bitmap (completely filled with RGB(0,0,255) ) via GetDIBits , but some other colors are given. And when you try to make changes to an array, it crashes altogether. What's wrong?

 case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); HBRUSH hb = CreateSolidBrush(RGB(0, 0, 255)); HDC hdcc = CreateCompatibleDC(hdc); HBITMAP bm = CreateCompatibleBitmap(hdc, r.right, r.bottom); SelectObject(hdcc, bm); SelectObject(hdcc, hb); Rectangle(hdcc, 0, 0, r.right, r.bottom); //Π·Π°Ρ€ΠΈΡΠΎΠ²Ρ‹Π²Π°ΡŽ синСй ΠΊΠΈΡΡ‚ΡŒΡŽ BITMAPINFO bi = { 0 }; bi.bmiHeader.biSize = sizeof(bi.bmiHeader); int er = GetDIBits(hdcc, bm, 0, 0, NULL, &bi, DIB_RGB_COLORS); //Π’ GetDIBits Π² качСствС HDC Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΆΠ΅ совмСстный Π²Ρ‹ΡΡ‚ΡƒΠΏΠ°Ρ‚ΡŒ, Π΄Π°? if (!er) { cout << "ERROR HERE:"<< GetLastError()<<"ENDS"; } COLORREF *buf = new COLORREF(bi.bmiHeader.biSizeImage); \\Π’Π°ΠΊ ΠΈ Π½Π΅ понял ΠΊΠ°ΠΊΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ массив - char, BYTE, COLORREF ΠΈΠ»ΠΈ ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ Π΅Ρ‰Ρ‘ bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biHeight = abs(bi.bmiHeader.biHeight); GetDIBits(hdcc, bm, 0, bi.bmiHeader.biHeight, buf, &bi, DIB_RGB_COLORS); for (int i(0); i < 100; i++) { cout << (int)GetRValue(buf[i]) << ","; cout << (int)GetGValue(buf[i]) << ","; cout << (int)GetBValue(buf[i]) << ","; cout << endl; } SetDIBits(hdcc, bm, 0, bi.bmiHeader.biHeight, buf, &bi, DIB_RGB_COLORS); delete []buf; BitBlt(hdc, 0, 0, r.right, r.bottom, hdcc, 0, 0, SRCCOPY); DeleteObject(hb); DeleteDC(hdcc); DeleteObject(bm); EndPaint(hwnd, &ps); } break; 

    1 answer 1

    After the first call to GetDIBits we become aware of the sizes of the bitmap (although here they are known in advance). You can then calculate the size of the required buffer ( biSizeImage may be 0 or not the same as the required buffer size if the required format is different from the current one) and select the array. When allocating an array, you also have an error - you select only one element, initializing it with the value bi.bmiHeader.biSizeImage.

     auto const bitmap_pixels_count{::std::abs(bi.bmiHeader.biWidth * bi.bmiHeader.biHeight)}; COLORREF * buf = new COLORREF[bitmap_pixels_count]; // выдСляСм массив 

    I also note that Rectangle draws a rectangle with a stroke with the current pen.