Solved

Load bitmap from rar archive

Posted on 2002-06-20
10
726 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 163
abstract class with all non abstract mentods 6 84
Annoying "thing" blocks my view 4 133
Increment column based of a FK 8 51
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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