Solved

CBitmap

Posted on 2004-04-15
14
1,060 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:pcssecure
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10831391
Use CBitmap::SetBitmapBits (dwSize, lp);
0
 

Author Comment

by:pcssecure
ID: 10831419
i tried it doesn't work. There is something that i am doing wrong...
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10831476
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.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 13

Expert Comment

by:SteH
ID: 10831488
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::CreateBitmapIndirect () 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.
0
 

Author Comment

by:pcssecure
ID: 10831673
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.biBitCount;
m_BitMap.bmHeight      = m_BmpInfo->bmiHeader.biHeight;
m_BitMap.bmPlanes      = m_BmpInfo->bmiHeader.biPlanes;
m_BitMap.bmType      = 0;
m_BitMap.bmWidth      = m_BmpInfo->bmiHeader.biWidth;
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->CreateBitmapIndirect(&m_BitMap);

m_Bmp->SetBitmapBits(m_BmpInfo->bmiHeader.biSizeImage, m_BmpBits);

      
BITMAP bm;
m_Bmp->GetBitmap(&bm);


bm.bmBits points to a NULL pointer. So something is wrong.

      
0
 

Author Comment

by:pcssecure
ID: 10831700
m_Bmp was created as below:

if (!m_Bmp)
            m_Bmp = new CBitmap;
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10831733
You need to use CreateDIBSection function. It returns HBITMAP, after this attach it to CBitmap:

m_Bmp->Attach(hBitmap);

In this case CBitmap is devide-independent bitmap (DIB). DIB allows using of SetBitmapBits, and bm.bmBits for such bitmap is not NULL.
0
 

Author Comment

by:pcssecure
ID: 10831780
Does LoadImage method in CBitMap do the same thing (CreateDIBSection and all)?
0
 
LVL 13

Expert Comment

by:SteH
ID: 10831820
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?
0
 

Author Comment

by:pcssecure
ID: 10831837
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?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10831838
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.
0
 

Author Comment

by:pcssecure
ID: 10831853
CreateDIBSection Works beautifully..

Thanks a lot to everyone.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10831871
>> 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.
0
 

Author Comment

by:pcssecure
ID: 10831889
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Suggested Courses

617 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