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
|
typedef std::vector<BYTE> pixeldata;
pixeldata GetImagePixel(HDC hdcImage)
{
BITMAP bmp = {0};
BITMAPINFO Info = {0};
memset( &bmp, 0, sizeof(BITMAP) );
HBITMAP hBitmap =(HBITMAP) GetCurrentObject(hdcImage, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &bmp);
BITMAPINFO info { };
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
size_t pixelSize = info.bmiHeader.biBitCount / 8;
// the + 3 ) & ~3 part is there to ensure that each
// scan line is 4 byte aligned
size_t scanlineSize = (pixelSize * info.bmiHeader.biWidth + 3) & ~3;
size_t bitmapSize = bmp.bmHeight * scanlineSize;
pixeldata pixels(bitmapSize);
GetDIBits(hdcImage, hBitmap, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS);
bmp.bmHeight = std::abs(bmp.bmHeight);
return pixels;
}
void SetImageData(HDC hdcDestination, pixeldata pixels)
{
BITMAP bmp = {0};
BITMAPINFO Info = {0};
memset( &bmp, 0, sizeof(BITMAP) );
HBITMAP hBitmap =(HBITMAP) GetCurrentObject(hdcDestination, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &bmp);
BITMAPINFO info { };
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;
SetDIBits(hdcDestination, hBitmap, 0, bmp.bmHeight, &pixels[0], &info, DIB_RGB_COLORS);
}
//how use it
/*pixeldata pixels=GetImagePixel(imgImage);
size_t pixelSize = bm.bmBitsPixel / 8;
size_t scanlineSize = (pixelSize * bm.bmWidth + 3) & ~3;
int x=0, y=0;
for(y=0; y<imgImage.height(); y++)
{
for(x=0; x<imgImage.width(); x++)
{
size_t pixelOffset = y * scanlineSize + x * pixelSize;
COLORREF clrActualColor=RGB(pixels[pixelOffset+2],pixels[pixelOffset+1],pixels[pixelOffset+ 0]);
if(clrActualColor ==RGB(0,0,0))
{
pixels[pixelOffset+2]=GetRValue(GetSysColor(COLOR_MENU));
pixels[pixelOffset+1]=GetGValue(GetSysColor(COLOR_MENU));
pixels[pixelOffset+0]=GetBValue(GetSysColor(COLOR_MENU));
}
}
}
SetImageData(hMemDC,pixels);*/
|