Link to home
Start Free TrialLog in
Avatar of janapriya
janapriya

asked on

Displaying a bitmap from BITMAP structure. (And accessing BITs)

Dear All,

I am given a task to write a function like following,

BOOL AConverterFunction (BITMAP& srcBitmap, BITMAP& dstBitmap).

As it is seen, I will be given with a structure filled with BITMAP data. What I need is to have a some processing on the data of that BITMAP and to display the BITMAP for debugging purposes. Also I need to access pixel information from the BITMAP structure.

Could anyone plz provide a fraction of code to do this task or provide a know how to do this.

regards,

Janapriya.


Avatar of Mercurius0
Mercurius0

You should first use this to make a bitmap handle
HBITMAP hBitmap=CreateBitmapIndirect(bitmap);

And then this to display it
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);

To acces the bit-info use GetDIBits and give bitmap.bmBits with it
Avatar of janapriya

ASKER

In side OnPaint() I have written a code like this. (I know this is not a good place to read a file.. But to test the code I have done that)

CPaintDC dc(this);

HBITMAP hBitmap = (HBITMAP) LoadImage(NULL, "input.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
BITMAP bitmap;

CBitmap::FromHandle(hBitmap)->GetBitmap(&bitmap);

// I wanted to simulate my situation. (I will be receiving a Filled BITMAP i.e. bitmap)
// Now I will be creating the handle to the bitmap. Please focus on the code below. I know that simply I could have used hBitmap. Since I am getting a bitmap I have to create the handle.

HBITMAP hhBitmap = CreateBitmapIndirect(&bitmap);

HDC hMemDC = CreateCompatibleDC(dc.GetSafeHdc());
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hhBitmap);
BitBlt(dc.GetSafeHdc(), 0, 0, bitmap.bmWidth, bitmap.bmHeight, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);

But this code does not work. I get NULL for hOldBitmap. :(
Of course hOldBitmap gives NULL if you haven't used hMemDC before. It is just for neat coding and to prevent errors. With SelectObject you put hBitmap in hMemDC and then you blit this hMemDC.

What isn't working exactly? Isn't the bitmap drawn?
 
No it is not drawn.  :(
Are you sure the dc.GetSafeHdc() gives the right hDC?
I am not sure about it. I assumed that it will provide the correct hDC. Please note that I am not familiar with them very much.
ASKER CERTIFIED SOLUTION
Avatar of Mercurius0
Mercurius0

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
Sorry about the delay, I couldn't recall why I didn't do anything about the points.
project was a success, and answers from Mercurius0 was really helpful. So I will give full credits for him.
Please note that the answers given by him, helped a lot to gain the right track. My method was written in DLL and I couldn't get the hWnd of a window. Thus, somehow I have managed to find a way to complete it. I couldn't recall the way I have done, since it has been sometime now. Thanks Mercurius0 and EE for all da help