I am trying out a code to capture bitmap information using CreateDIBSection. Since I want to make the size of the array (which to hold each pixel value of the display monitor) flexible in order to suit monitor of different size, I create a dynamic unsigned char array (each color channel is 1 byte).
However, when I run the program, the program crashed right at the part of deleting the array, near the end of the program.
I tried to cast the array back to original type, i.e. unsigned char*, since I suspected it had been casted to void** when it being passed into CreateDIBSection(), but it doesn't work.
Below is the code, appreciate all the advice.
#include <Windows.h>
#include <cstdint>
HWND m_hwnd;
void GetBitMapInfo(const int& x_Coordinate, const int& y_Coordinate, const int& iWidth, const int& iHeight)
{
DWORD imageSize = iWidth * iHeight * 4;
// Get the display window
HDC displayWindow = GetDC(m_hwnd);
HDC hdc = CreateCompatibleDC(displayWindow);
// Fill in the Bitmap information
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = iWidth;
bmpInfo.bmiHeader.biHeight = iHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
// Create the storage for the pixel information
uint8_t* image = new uint8_t[imageSize];
// Populate the storage with the BMP pixel information
HBITMAP hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, (void**)(&image), nullptr, NULL);
SelectObject(hdc, hBitmap);
BitBlt(hdc, x_Coordinate, y_Coordinate, iWidth, iHeight, displayWindow, 0, 0, SRCCOPY);
delete[] image; //Program crashed here: identifier "image" is undefined
image = nullptr;
DeleteDC(hdc);
DeleteDC(displayWindow);
DeleteObject(hBitmap);
return;
}
int main()
{
GetBitMapInfo(0, 0, 1920, 1080);
return 0;
}