Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get an HGLOBAL for a DIB.

Posted on 2006-03-21
14
Medium Priority
?
453 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
0
Comment
Question by:rushtheweb
  • 5
  • 5
  • 4
14 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 16253458
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....
0
 

Author Comment

by:rushtheweb
ID: 16253510
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();
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16253559
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rushtheweb
ID: 16253604
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. :)
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16254654
>>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
0
 

Author Comment

by:rushtheweb
ID: 16257693
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.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16257832
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
0
 

Author Comment

by:rushtheweb
ID: 16257892
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.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16257932
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 2000 total points
ID: 16257941
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16260067
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!
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16260208
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16260516
Probably true.  Nevertheless, it would be best to hear exactly what helped solve the problem, directly from rushtheweb.
0
 

Author Comment

by:rushtheweb
ID: 16260542
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question