Link to home
Start Free TrialLog in
Avatar of rushtheweb
rushtheweb

asked on

Get an HGLOBAL for a DIB.

Hi,

My application loads a picture from file and converts it to a DDB and then to a DIB; however I am in need of using globalsize and globallock functions on the DIB to get the size and such. How can I get an HGLOBAL to a loaded bitmap(DDB) or DIB?

Thanks
Avatar of DanRollins
DanRollins
Flag of United States of America image

Depending on the way the data was read in and converted, your HBITMAP to the DIB is quite possibly an HGLOBAL already.  Have you tried using GlobalSize on the handle?

Also:  If you did the conversion, I'd think that you would already know the size, etc....
Avatar of rushtheweb
rushtheweb

ASKER

Here is a snippet of my code that is loading the image to bitmap and then getting the dib section for it.... I dont actually know if this is the correct way of doing it or not. When I do a GlobalSize on the DIB handle I always get 0; is there another way of getting the DIB and the size?

WCHAR wpath[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, szPath, -1, wpath, MAX_PATH);
      
IPicture *pic;
OleLoadPicturePath(wpath, NULL, NULL, NULL, IID_IPicture, (LPVOID *)&pic);
                                    
HBITMAP hPic      = NULL;
HBITMAP h            = NULL;
DIBSECTION            info;

pic->get_Handle((UINT *)&hPic);
GetObject(h, sizeof(DIBSECTION), &info);
void *pBits                  = info.dsBm.bmBits;

nSize = GlobalSize(hData);
pPtr = GlobalLock(hData);

if ( nSize != NULL )
{
      SetData(CF_DIB, pPtr, (UINT)nSize);
}

GlobalUnlock(hData);
pic->Release();
I'm not up to speed on  OleLoadPicturePath...
Verify that the OleLoadPicturePath call succeeded.  Also try:
     short nType;
     HRESULT hr= pic->get_Type( &nType );
as a sanity check to see if you have a real HBITMAP
I've verified that the picture is being loaded successfully by displaying the bitmap on a dialog. To double check I have done as you suggested and came up with the result: S_OK.

I just wish I knew why GlobalSize always returns 0; I imagine its something kinda simple that im overlooking, however I havent done C++ is 4 years and even then I didnt use MFC very often so its a given that im going to mess something up. :)
>>I just wish I knew why GlobalSize always returns 0;

How you are processing hData before ? is this handle returned by either the GlobalAlloc or GlobalReAlloc ??

MAHESH
Sorry that was a typo when I was editing the code, it really is:

nSize = GlobalSize(h);
pPtr = GlobalLock(h);

My mistake, I've also tried using pBits in GlobalSize with no luck, both cause GlobalLock to return nothing.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalsize.asp <===As per this documentation handle for GlobalSize() is returned by either the GlobalAlloc or GlobalReAlloc function....


MAHESH
Hmm, I wish I read that entire page through... lol.

However Im still back to the where I started in a sense because I dont know how to get the total size I need to allocate. Did I mention I havent coded in C++ in forever. ;p I appreciate the help.
ASKER CERTIFIED SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rushtheweb,
Out of curiosity -- and to improve the value of this question in the PAQ -- what specifically did you learn from those links and how did you obtain the size information that you needed?  Thanks!
I think he was passing HBITMAP instead of HANDLE(hDIB) to GlobalSize()......also GLobalSize() needs handle returned by GlobalAlloc()/GlobalReAlloc().......So I suggested to refer above links...and to refer BitmapToDIB()function....

BITMAPHEADERSIZE + PALETTE + BITMAPBITS.....


MAHESH
Probably true.  Nevertheless, it would be best to hear exactly what helped solve the problem, directly from rushtheweb.
Im currently very busy at work so I apoligize for the late reply. What mahesh said was right, I was simply confused and in the many examples he provided I was able to clearly see that I wasnt using the correct handle and on top of that the BitmapToDib function turned out to be fantastic!

Thanks for everyone for the help.