We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Get an HGLOBAL for a DIB.

rushtheweb
rushtheweb asked
on
Medium Priority
505 Views
Last Modified: 2013-11-20
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
Comment
Watch Question

CERTIFIED EXPERT
Author of the Year 2009

Commented:
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....

Author

Commented:
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();
CERTIFIED EXPERT
Author of the Year 2009

Commented:
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

Author

Commented:
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. :)
mahesh1402IT Professional
Top Expert 2005

Commented:
>>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

Author

Commented:
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.
mahesh1402IT Professional
Top Expert 2005

Commented:
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

Author

Commented:
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.
mahesh1402IT Professional
Top Expert 2005

Commented:
IT Professional
Top Expert 2005
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Author of the Year 2009

Commented:
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!
mahesh1402IT Professional
Top Expert 2005

Commented:
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
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Probably true.  Nevertheless, it would be best to hear exactly what helped solve the problem, directly from rushtheweb.

Author

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.