pcssecure
asked on
CBitmap
Hi,
Given a BITMAPINFO struct and a byte array of RGB pixel values (in the proper format with each row aligned to 4 bytes) void* lp, how do I copy them into a CBitmap object so that it can be displayed in the screen?
Thanks.
Given a BITMAPINFO struct and a byte array of RGB pixel values (in the proper format with each row aligned to 4 bytes) void* lp, how do I copy them into a CBitmap object so that it can be displayed in the screen?
Thanks.
Use CBitmap::SetBitmapBits (dwSize, lp);
ASKER
i tried it doesn't work. There is something that i am doing wrong...
Do you need code for all possible bitmap types (RGB 24 bits, RGB 32 bits, bitmap with color palette etc.) or for some specific bitmap type? In the last case code is much simpler.
It could be that the aligning is done inside of SetBitmapBits already and you don't need to care but this should not affect the call when it data is aligned and the size is given correctly.
How does the call fail? Or is it the craetion of the CBitmap object not respecting your BITMAPINFO structure? In the latter case you can use CBitmap::CreateBitmapIndir ect () using your BITMAPINFO as first part of the BITMAP structure. Leave the rest of the structure unfilled first and use SetBitmapBits afterwards. This should give you a bitmap with the parameters specified in the BITAMPINFO.
How does the call fail? Or is it the craetion of the CBitmap object not respecting your BITMAPINFO structure? In the latter case you can use CBitmap::CreateBitmapIndir
ASKER
Here's my code,
m_BitMap is of type BITMAP
m_BmpInfo is of type BITMAPINFO
m_BmpBits is of type void*
m_BitMap.bmBits = m_BmpBits;
m_BitMap.bmBitsPixel = m_BmpInfo->bmiHeader.biBit Count;
m_BitMap.bmHeight = m_BmpInfo->bmiHeader.biHei ght;
m_BitMap.bmPlanes = m_BmpInfo->bmiHeader.biPla nes;
m_BitMap.bmType = 0;
m_BitMap.bmWidth = m_BmpInfo->bmiHeader.biWid th;
m_BitMap.bmWidthBytes = m_BitMap.bmBitsPixel * m_BitMap.bmWidth / 8;
int patchBytes = m_BitMap.bmWidthBytes% 4; //Number of bits to patch.
if (patchBytes > 0)
m_BitMap.bmWidthBytes += 4 - m_BitMap.bmWidthBytes % 4;
m_Bmp->CreateBitmapIndirec t(&m_BitMa p);
m_Bmp->SetBitmapBits(m_Bmp Info->bmiH eader.biSi zeImage, m_BmpBits);
BITMAP bm;
m_Bmp->GetBitmap(&bm);
bm.bmBits points to a NULL pointer. So something is wrong.
m_BitMap is of type BITMAP
m_BmpInfo is of type BITMAPINFO
m_BmpBits is of type void*
m_BitMap.bmBits = m_BmpBits;
m_BitMap.bmBitsPixel = m_BmpInfo->bmiHeader.biBit
m_BitMap.bmHeight = m_BmpInfo->bmiHeader.biHei
m_BitMap.bmPlanes = m_BmpInfo->bmiHeader.biPla
m_BitMap.bmType = 0;
m_BitMap.bmWidth = m_BmpInfo->bmiHeader.biWid
m_BitMap.bmWidthBytes = m_BitMap.bmBitsPixel * m_BitMap.bmWidth / 8;
int patchBytes = m_BitMap.bmWidthBytes% 4; //Number of bits to patch.
if (patchBytes > 0)
m_BitMap.bmWidthBytes += 4 - m_BitMap.bmWidthBytes % 4;
m_Bmp->CreateBitmapIndirec
m_Bmp->SetBitmapBits(m_Bmp
BITMAP bm;
m_Bmp->GetBitmap(&bm);
bm.bmBits points to a NULL pointer. So something is wrong.
ASKER
m_Bmp was created as below:
if (!m_Bmp)
m_Bmp = new CBitmap;
if (!m_Bmp)
m_Bmp = new CBitmap;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Does LoadImage method in CBitMap do the same thing (CreateDIBSection and all)?
For CBitmap I only find LoadBitmap as member functions. This will get a resource into a CBitmap. Where do you get your BITAMPINFO and the bits from?
ASKER
The BITMAPINFO and Byte array pixels are obtained from memory mapped files. I have checked the contents. They are ok.
When using CreatingDIBSection, how do I free the memory allocated for the DIBSection after usage? Will calling
DeleteObject(HBITMAP) do the job?
When using CreatingDIBSection, how do I free the memory allocated for the DIBSection after usage? Will calling
DeleteObject(HBITMAP) do the job?
CBitmap class doesn't have LoadImage function. It's LoadBitmap function doesn't do this.
LoadImage API can load bitmap as DIB, if it is used with LR_CREATEDIBSECTION flag. HBITMAP returned by LoadImage may be attached to CBitmap using Attach function.
Internally, LoadImage with LR_CREATEDIBSECTION flag reads BITMAPINFO structure which comes from file, and creates DIB, calling CreateDIBSection or some other API.
LoadImage API can load bitmap as DIB, if it is used with LR_CREATEDIBSECTION flag. HBITMAP returned by LoadImage may be attached to CBitmap using Attach function.
Internally, LoadImage with LR_CREATEDIBSECTION flag reads BITMAPINFO structure which comes from file, and creates DIB, calling CreateDIBSection or some other API.
ASKER
CreateDIBSection Works beautifully..
Thanks a lot to everyone.
Thanks a lot to everyone.
>> When using CreatingDIBSection, how do I free the memory allocated for the DIBSection after usage?
CreateDIBSection returns HBITMAP. You attach it to CBItmap and don't care to release it since CBitmap is responsible for it. If you want to release CBitmap for reusing, call CBitmap::DeleteObject.
If you don't attach HBITMAP to CBitmap, use DeleteObject API to release it.
CreateDIBSection returns HBITMAP. You attach it to CBItmap and don't care to release it since CBitmap is responsible for it. If you want to release CBitmap for reusing, call CBitmap::DeleteObject.
If you don't attach HBITMAP to CBitmap, use DeleteObject API to release it.
ASKER
Thanks again Alex. You have been a great help over the last two days...
I can now load an image in a delphi process and display the image using another C process via memory mapped file.
Thanks a lot.
I can now load an image in a delphi process and display the image using another C process via memory mapped file.
Thanks a lot.