Solved

Load bitmap from rar archive

Posted on 2002-06-20
10
725 Views
Last Modified: 2013-11-20
hi

i'm tring to load a bitmap image from a bmp file stored in a RAR archive and then to set the image for a toolbar.

i have the support to read the file in memory(a block of chars) and then i'm using the fallowing code to set image for the toolbar :

char* buffer;    // the block

CBitmap *bmp = new CBitmap();
bmp->CreateBitmap(80, 16, 4, 8, NULL);
bmp->SetBitmapBits(size, buffer);
BOOL bRes = m_wndToolBar.SetBitmap((HBITMAP)(*bmp));

this code works only if a set True color(32 bit)... in all other cases DOESN'T WORK !!!!!

anybody has an ideea or a suggestion ??????

10x A LOT
0
Comment
Question by:dragosh
10 Comments
 
LVL 12

Expert Comment

by:migel
ID: 7096296
Hi!
You have to convert loaded buffer to the device depended bitmap, since BITMAP file store DIB section not DDB (HBITMAP).
search MSDN
Q83034
ShowDIB: Opening/Displaying Device Independent Bitmaps
0
 
LVL 3

Author Comment

by:dragosh
ID: 7096365
10x for your tips, but i don't have time to search and stuff ... can u post some code ????
0
 
LVL 12

Expert Comment

by:migel
ID: 7097921
Hi!
article Q83034 contains link to MFC example so you spend not so much time for downloading source code from MSDN site
http://download.microsoft.com/download/win31/sdk6/1/W31/EN-US/Gdirsrcs.exe
You even haven`t download ShowDIB example frmo Interent - this sample located in the MSDN CD (you need to look at dib.c source file).
Good Luck
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 3

Accepted Solution

by:
Crius earned 200 total points
ID: 7098941
The problem you are experiencing is caused by the line:
bmp->CreateBitmap(80, 16, 4, 8, NULL);

The 3rd argument is what says how many colours there are per pixel. You have specified 4.

The 4th argument specifies how many bits per pixel you want.
You have specified 8.

8*4=32 bit colour for a pixel.

What you really want to do is CreateCompatibleBitmap with the CDC is the current display. This will guarantee that you have the proper number of colours per pixel, and number of bits per pixel.

You'll need to change your code a bit since you will no longer be able to set the Bitmap bits directly to the toolbar, since you can't guarantee it'll be 32bit true colour. Look into using BitBlt to do the copy from one Bitmap to the other.

BitBlt - If destination, source, and pattern bitmaps do not have the same color format, the BitBlt function converts the source and pattern bitmaps to match the destination. The foreground and background colors of the destination bitmap are used in the conversion.

So, more or less, what you want to do is:
1. CreateCompatibleBitmap to your screen DC.
2. Copy in the bitmap from your rar file into the bitmap as you have done.
3. Create the DCs and select the bitmaps into them (2 DCs, 1 compatible with screen, other with your 32bit bitmap)
4. BitBlt from your 32bit DC to the screen DC.
5. Release the bitmaps from your DCs, and put your screen compatible bitmap into the toolbar.
6. Free the memory of your 32bit bitmap.
7. When your application is closing, make sure you free your toolbar bitmap too, but don't do it until the app is closing (I think it needs to exist for the life of the app, but can't recall offhand).

Questions?
0
 
LVL 3

Expert Comment

by:Crius
ID: 7098956
Oh, sorry, I re-read my answer and saw a possible confusing part.

The bitmap you should copy into in step 2. is the one you made with bmp->CreateBitmap(80, 16, 4, 8, NULL);
Not the CreateCompatibleBitmap you made in step 1.

Sorry for possible confusion.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7101503
This function:
   bmp->SetBitmapBits(size, buffer);
is dumping bits into the image buffer, but you can't just stuff any old bits in there.  The data you use must match the settings for the bitmap that you created.

In what format is the image data?  You just said that you read a chunk of data from a RAR.  If it is, for instance text data, you're out of luck.  If it is the raw contents of a .BMP file then you need to treat it as a DIB and read the BITMAPHEADER and all that to understand the data layout.

>> in all other cases DOESN'T WORK !!!!!
What is the symptom of the failure?

-- Dan
0
 
LVL 3

Expert Comment

by:Crius
ID: 12046966
Well, I believe my answer is correct. I ran into the same problem once upon a time, and this is the solution I used. So, basically, this has been tested, the problem is well known, and the solution I provided does address the problem I had, and thus I feel this problem as well.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 93
iSeries FTP Exit Program 8 135
conditional code and condition difference 9 82
child constructor and parent constructor, overriding and overloading 6 83
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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