Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

converting HBITMAP to byte array

I'm working with some scanner api which returns a HANDLE to an image in BMP format(so it is said so in documentation). I'm trying to somehow get BITMAP from this handle, but for example this code doesn't work:

HANDLE handle = getHandleFromScanner();
BITMAP bitmap;
int u = GetObject(handle, sizeof(BITMAP), &bitmap);

u is 0 here and getLastError() returns 6 which means that handle is invalid. But I cannot get any other handle except through getHandleFromScanner() function.

May be some transformations should be done with this handle? any ideas? What is the proper way to work with bitmap handles? Or any simple api exists? Samples I found via google didn't help me.

Thanks a lot.

Upvotes: 3

Views: 6770

Answers (2)

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Thanks guys.

The handle appeared to be the image itself, so the following code solved the problem:

char* pImage = NULL;
HANDLE hImage= getHandleFromScanner();
pImage = (char *)GlobalLock(hImage);
// pImage now contains the bytes of the image

If someone will ever need it, the Scanner is Olivetti PR2 plus scanner.

Upvotes: 1

jay.lee
jay.lee

Reputation: 19837

Have you tried GetDIBits()? This should work but you'll need the device context as well. You may always want to call GetObjectType() on the handle to see if it is really returning an HBITMAP.

Upvotes: 2

Related Questions