Click to See Complete Forum and Search --> : unable to get pixel info from an hbitmap using gdiplus


heckubiss
February 8th, 2009, 06:19 PM
I am trying to display pixel information from a hbitmap using gdi plus and lockbits instead of the painstakingly slow getpixel routing. unfortunately when I try to obtain a GDI plus bitmap from an hBitmap, the data gets lost. This works fine when I load the bitmap from a file.

here is the code:


#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;




// Get the width height, and pixel data for an HBITMAP (needs to be a DIB)
//
byte* GetBitmapSizeAndData(HBITMAP hbmp, int& width, int& height)
{
// Get width and height
BITMAP bm;
memset((void*)&bm, 0, sizeof(BITMAP));
int irv = GetObject(hbmp, sizeof(BITMAP), (void*)&bm);
width = bm.bmWidth;
height = bm.bmHeight;
return (byte*)bm.bmBits;
}

// Create a Gdiplus::Bitmap copy of an HBITMAP
//
Bitmap* CreateGdibitmapFromHBITMAP(HBITMAP hbmp, HDC hdc)
{
// Get width and height and pixels
int width, height;
byte* sourceBytes = GetBitmapSizeAndData(hbmp, width, height);

// Create an argb GDI+ bitmap of the same dimensions.
Bitmap* copy = new Bitmap(width, height, PixelFormat32bppPARGB);

// Get access to the Gdiplus::Bitmap's pixel data
BitmapData bmd;
Rect rect(0, 0, width, height);
copy->LockBits(&rect, ImageLockMode::ImageLockModeWrite,
PixelFormat32bppPARGB, &bmd);

// Copy source pixel data to destination Bitmap (one is 'upsidedown' relative to the other)
int lineSize = width * 4;
byte* destBytes = (byte*)(bmd.Scan0);
for (int y = 0; y < height; y++)
{
memcpy(destBytes + (y * lineSize), sourceBytes + ((height - y -1) * lineSize), lineSize);
}

copy->UnlockBits(&bmd);

return copy;
}



INT main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


// BYTE redValue = 0;
// BYTE greenValue=0;
// BYTE blueValue =0;

HDC hDDC = GetDC( GetDesktopWindow() );
// HDC hCDC = CreateCompatibleDC(hDDC);

HBITMAP hBitmap = CreateCompatibleBitmap(hDDC, 500, 500);


// HGDIOBJ h1=SelectObject(hCDC,hBitmap);
// BitBlt(hCDC, CURSOR_X, CURSOR_X, 2, 2, hDDC, CURSOR_X, CURSOR_X, SRCCOPY|CAPTUREBLT);



HPALETTE hPalette = (HPALETTE)GetCurrentObject(hDDC, OBJ_PAL);
Bitmap* bitmap = Bitmap::FromHBITMAP (hBitmap,hPalette); //,hCDC);

/*


COLORREF color = GetPixel(hCDC, CURSOR_X+1, CURSOR_X+1);
redValue = GetRValue(color);
greenValue = GetGValue(color);
blueValue = GetBValue (color);
if ((redValue != 242)){

printf("red value %i green value %i blue value%i\n", redValue, greenValue, blueValue);
SetCursorPos(CURSOR_X,CURSOR_Y);
Sleep(300);
}*/

//Bitmap* bitmap = CreateGdibitmapFromHBITMAP(hBitmap,hDDC);

//HPALETTE hPalette = (HPALETTE)GetCurrentObject(hCDC, OBJ_PAL);
//Bitmap* bitmap = new Bitmap(hBitmap,hPalette);

// Bitmap* bitmap = new Bitmap(L"D:\wandk.jpg");
BitmapData* bitmapData = new BitmapData;
Rect rect(0, 0, 500, 300);

// Lock a 5x3 rectangular portion of the bitmap for reading.
bitmap->LockBits(
&rect,
ImageLockModeRead,
PixelFormat32bppARGB,
bitmapData);

printf("The stride is %d.\n\n", bitmapData->Stride);

// Display the hexadecimal value of each pixel in the 5x3 rectangle.
UINT* pixels = (UINT*)bitmapData->Scan0;

for(UINT row = 0; row < 300; ++row)
{
for(UINT col = 0; col < 500; ++col)
{
printf("%x\n", pixels[row * bitmapData->Stride / 4 + col]);
}
printf("- - - - - - - - - - \n");
}
//Graphics graphics(hBitmap);


//graphics.DrawImage(bitmap, 10, 10);


bitmap->UnlockBits(bitmapData);
delete bitmapData;
delete bitmap;
//SelectObject(hCDC,h1);
DeleteObject(hBitmap);
GdiplusShutdown(gdiplusToken);
return 0;
}