Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

Load bitmap from rar archive

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
dragosh
Asked:
dragosh
1 Solution
 
migelCommented:
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
 
dragoshAuthor Commented:
10x for your tips, but i don't have time to search and stuff ... can u post some code ????
0
 
migelCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CriusCommented:
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
 
CriusCommented:
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
 
DanRollinsCommented:
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
 
CriusCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now