Solved

Load bitmap from rar archive

Posted on 2002-06-20
10
719 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now