1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
class image
{
public:
int ImageWidth = 0;
int ImageHeight = 0;
HDC ImageHDC = NULL;
HBITMAP ImageBitmap;
HBITMAP oldBit;
BITMAP bmp;
BITMAPINFO info;
size_t pixelSize;
size_t scanlineSize;
size_t bitmapSize;
void* p;
LPBYTE Pixels;
void Clear(COLORREF BackColor = RGB(0,0,0))
{
for(int i=0; i<int(sizeof(Pixels)/sizeof(LPBYTE)-3); i+=3)
{
Pixels[i+2]=GetRValue(BackColor);
Pixels[i+1]=GetGValue(BackColor);
Pixels[i+0]=GetBValue(BackColor);
}
}
image(int Width, int Height, COLORREF BackColor=RGB(0,0,0))
{
ImageHDC = CreateCompatibleDC(NULL);
ImageWidth = Width;
ImageHeight =Height;
ZeroMemory (&info, sizeof (BITMAPINFO));
info.bmiHeader.biSize = sizeof(info.bmiHeader);
info.bmiHeader.biWidth = ImageWidth;
// pay attention to the sign, you most likely want a
// top-down pixel array as it's easier to use
info.bmiHeader.biHeight = -ImageHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = 0;
info.bmiHeader.biXPelsPerMeter = 0;
info.bmiHeader.biYPelsPerMeter = 0;
info.bmiHeader.biClrUsed = 0;
info.bmiHeader.biClrImportant = 0;
// the following calculations work for 16/24/32 bits bitmaps
// but assume a byte pixel array
ImageBitmap = CreateDIBSection(ImageHDC, &info, DIB_RGB_COLORS, (LPVOID*)&Pixels, 0, 0);
SelectObject(ImageHDC, ImageBitmap);
pixelSize = info.bmiHeader.biBitCount / 8;
// the + 3 ) & ~3 part is there to ensure that each
// scan line is 4 byte aligned
scanlineSize = (pixelSize * info.bmiHeader.biWidth + 3) & ~3;
bitmapSize = bmp.bmHeight * scanlineSize;
Clear(BackColor);
}
| |