15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Bitmap Problem

Wayne G. Maas -- wgmaas@usa.nai.net
Friday, November 15, 1996

Environment: Win 95, VC 4.2flat
---------------------------------------------

I've got a question I hope someone can help me with.  It concerns
bitmaps.
I have some code that builds toolbars.  The button bitmaps are
extracted
from various Windows applications.  I am attempting to resize the
icon images
from 32x32 to 24x24 which is the button size I am using.  The code
almost works.
I am getting the icon image but the image is in one color.  The
on-line help states
that the CreateCompatibleDC will create a device context selects a
1-by-1
monochrome stock bitmap.  So I believe that is why I am getting the
image in all
black.

My question, how can I get the colors to display correctly.  Am I
overlooking something.
Any help would be appreciated.  (The code segment appears below.)

Wayne

--------------
...
...					
CDC dcIcon;
CBitmap bmpIcon;

CDC dcButton;
CBitmap bmpButton;

//extracts the icon from a windows application filename
HICON hIcon =
ExtractIcon(AfxGetInstanceHandle(),pActionTool->GetAppFileName(),0);
			
dcIcon.CreateCompatibleDC(NULL);	// handle to memory device context
bmpIcon.FromHandle((HBITMAP)hIcon);
dcIcon.SelectObject(bmpIcon);
			
dcButton.CreateCompatibleDC(NULL);
bmpButton.CreateCompatibleBitmap(&dcButton, 24, 24);
dcButton.SelectObject(bmpButton);
			
SetStretchBltMode(dcButton, STRETCH_DELETESCANS);
dcButton.StretchBlt(0,0,23,23,&dcIcon,0,0,31,31,SRCCOPY);
...
...



Jim Barry -- Jim.Barry@ilp.com
Monday, November 18, 1996

[Mini-digest: 3 responses]

On 16 November 1996 04:29, Wayne G. Maas[SMTP:wgmaas@usa.nai.net] wrote:
>Environment: Win 95, VC 4.2flat
>---------------------------------------------
>
>I've got a question I hope someone can help me with.  It concerns
>bitmaps.
>I have some code that builds toolbars.  The button bitmaps are
>extracted
>from various Windows applications.  I am attempting to resize the
>icon images
>from 32x32 to 24x24 which is the button size I am using.  The code
>almost works.
>I am getting the icon image but the image is in one color.  The
>on-line help states
>that the CreateCompatibleDC will create a device context selects a
>1-by-1
>monochrome stock bitmap.  So I believe that is why I am getting the
>image in all
>black.
>
>My question, how can I get the colors to display correctly.  Am I
>overlooking something.
>Any help would be appreciated.  (The code segment appears below.)
>
>Wayne
>
>--------------
>...
>...					
>CDC dcIcon;
>CBitmap bmpIcon;
>
>CDC dcButton;
>CBitmap bmpButton;
>
>//extracts the icon from a windows application filename
>HICON hIcon =
>ExtractIcon(AfxGetInstanceHandle(),pActionTool->GetAppFileName(),0);
>			
>dcIcon.CreateCompatibleDC(NULL);	// handle to memory device context
>bmpIcon.FromHandle((HBITMAP)hIcon);
>dcIcon.SelectObject(bmpIcon);
>			
>dcButton.CreateCompatibleDC(NULL);
>bmpButton.CreateCompatibleBitmap(&dcButton, 24, 24);
>dcButton.SelectObject(bmpButton);
>			
>SetStretchBltMode(dcButton, STRETCH_DELETESCANS);
>dcButton.StretchBlt(0,0,23,23,&dcIcon,0,0,31,31,SRCCOPY);
>...

Don't use a memory DC for creating the compatible bitmap - use a screen DC:

CClientDC dcScreen(NULL);
bmpButton.CreateCompatibleBitmap(&dcScreen, 24, 24);
...

See article Q139165 "PRB: Memory DC Produces Monochrome Images" if  you want to know why.


Jim Barry
Interactive Learning Productions
Newcastle upon Tyne, UK

-----From: joew@statsoft.com (Joe Willcoxson)

Try using CopyImage().  You can get it to scale from 32x32 to 24x24.
--
Joe Willcoxson (joew@statsoft.com)
Senior Software Engineer
Visit us: http://www.statsoft.com
Visit me: http://users.aol.com/chinajoe


-----From: "Umesh Chandwani" 

Hi Wayne,
        Your problem lies in the following line(as you have already guessed.)

        bmpButton.CreateCompatibleBitmap(&dcButton, 24, 24);

        This will create a monochrome bitmap.

Instead you must use the foll. lines of code.

[Moderator's note: The message ended here.]





Dong Chen -- d_chen@ix.netcom.com
Monday, November 18, 1996

I did it this way.

1. load the icon:
SHFILEINFO         sfi; 
CString                 strFile; 
HICON                   hicon
// strFile is the path of your application to load icon from
// it can be an .exe or a .lnk etc. 
// hicon is the handle of the icon
SHGetFileInfo(strFile, 0, &sfi, sizeof(SHFILEINFO), SHGFI_ICON |
SHGFI_SHELLICONSIZE );
hicon = sfi.hIcon;
//Refer to the help for more flags

2. create memory dc
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
// where pDC is the pointer to your parinting dc
// it can be CDC, CPaintDC*, CClientDC* etc depends on where you call your
draw icon function which I can't tell from your posting

3. create bitmap
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, 32, 32))

4. draw to memory
CBitmap* pBitmapOld = dcMem.SelectObject(&bitmap);
dcMem.DrawIcon(0, 0, hicon);

5. bilt it
pDC->StretchBlt(startx, starty, 24, 24,
&dcMem, 0, 0, 32, 32, SRCCOPY);

Of course, there are some other things you need to take care of. Such as
background painting, restore the gdi objects, 3D look,  and focus and
selected modes etc.

--
Dong
d_chen@ix.netcom.com




Andi Giri -- agiri@ctiis.com
Thursday, November 21, 1996

In response to Wayne G. Maas's query "Bitmap Problem"
Jim Barry says:
>Don't use a memory DC for creating the compatible bitmap - use a screen
>DC:
>
>CClientDC dcScreen(NULL);
>bmpButton.CreateCompatibleBitmap(&dcScreen, 24, 24);
>...
>
>See article Q139165 "PRB: Memory DC Produces Monochrome Images" if  you
>want to know why.
>
There is no need to make the destination DC as a screen DC.
As far as we create the destination bitmap using a screen (client) DC,
we are fine.  Hnce, it will suffice to just change the line
FROM:
>bmpButton.CreateCompatibleBitmap(&dcButton, 24, 24);
      TO:
>bmpButton.CreateCompatibleBitmap(&dcIcon, 24, 24);
because dcIcon was created as a screen bitmap as follows:
>dcIcon.CreateCompatibleDC(NULL);
>Following is a quote from the on-line help for
CreateCompatibleDC(NULL);	
If pDC is NULL, the function creates a memory device context that is
compatible with the system display.


Andi




| Вернуться в корень Архива |