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 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
BYTE *Pixels;
void GetDIBS()
{
memset( &bmp, 0, sizeof(BITMAP) );
GetObject(HBit, sizeof(BITMAP), &bmp);
info.bmiHeader.biSize = sizeof(info.bmiHeader);
info.bmiHeader.biWidth = bmp.bmWidth;
// pay attention to the sign, you most likely want a
// top-down pixel array as it's easier to use
info.bmiHeader.biHeight = -bmp.bmHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biCompression = BI_RGB;
// the following calculations work for 16/24/32 bits bitmaps
// but assume a byte pixel array
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;
//Pixels.resize (bitmapSize);
//GetDIBits(HDCMemory, HBit, 0, bmp.bmHeight, &Pixels[0], &info, DIB_RGB_COLORS);
void* p = reinterpret_cast<void *>(Pixels);
HBit = CreateDIBSection(HDCMemory, &info, DIB_RGB_COLORS,&p, 0, 0);
}
image(int SizeWidth, int SizeHeight, COLORREF Backcolor=RGB(0,0,0))
{
//Creating HDC and HBITMAP memory:
HDCMemory = CreateCompatibleDC(NULL);
//HBit = CreateBitmap(SizeWidth, SizeHeight, 1, 32,NULL);
//SelectObject(HDCMemory, HBit);
//Getting image size:
Width = SizeWidth;
Height = SizeHeight;
GetDIBS();
cout << "hey";
//Clear image and change the backcolor:
Clear(Backcolor);
}
void RefreshFromHDC()
{
//Update the pixels array\vector from HDC:
GetDIBits(HDCMemory, HBit, 0, bmp.bmHeight, &Pixels[0], &info, DIB_RGB_COLORS);
}
void RefreshToHDC()
{
//Update the image from pixels array\vector:
SetDIBits(HDCMemory, HBit, 0, bmp.bmHeight, &Pixels[0], &info, DIB_RGB_COLORS);
}
~image()
{
SelectObject(HDCMemory, oldBit);
DeleteObject(HBit);
DeleteDC(HDCMemory);
}
void Clear(COLORREF backcolor = RGB(0,0,0))
{
HBRUSH HBrush = CreateSolidBrush(backcolor);
RECT rec={0,0,Width, Height};
FillRect(HDCMemory,&rec,HBrush);
DeleteObject(HBrush);
RefreshFromHDC();
}
|