Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Load bitmap from rar archive

Posted on 2002-06-20
10
Medium Priority
?
736 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Accepted Solution

by:
Crius earned 800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

604 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