🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16115

How do you create an icon from a bitmap?

#16115
From: Chris
Date: Sat, 31 May 2003 03:18
341 lines
7735 bytes
Hello there
I've seen one or two examples of how to create an icon from a bitmap in BCB
rather simply, but the bitmap *always* comes out in a 16 color format.
One way I found myself was to use CreateIconIndirect() but that came out in
16 colors, and another way I found was to use a TImageList, the AddMasked
method, and then the GetIcon method... But that came out in 16 colors too.

I found an example that does it in MSVC++, first you clone the bitmap and
then you make an icon. I didn't have much luck getting it to work when I
converted it, I was wondering if some of you could help?

Some of the functions and what not can be removed because some of it isn't
absolutely necessary...




class CCloneBitmap : public CBitmap
{
public:
 COLORREF *m_lpData;
 HICON MakeIcon(COLORREF clrTransparent);
 int ChangeColor(COLORREF clrOld, COLORREF clrNew);
 int Clone(HBITMAP hSrc);
 CCloneBitmap();
 virtual ~CCloneBitmap();
};



int CCloneBitmap::Clone(HBITMAP hSrc)
{
 CBitmap bmpSrc;
 BITMAP bmp;
 if(!bmpSrc.Attach(hSrc))
  return 0;
 if(!bmpSrc.GetBitmap(&bmp))
  return 0;
 HGDIOBJ hOldSrc, hOldDest;
 if(m_hObject)
  DeleteObject();
 BITMAPINFOHEADER bmi;
 CDC dcSrc, dcDest, dcScr;
 HDC hdc;
 hdc=::GetDC(NULL);
 dcScr.Attach(hdc);
 dcDest.CreateCompatibleDC(&dcScr);
 dcSrc.CreateCompatibleDC(&dcScr);
 bmi.biBitCount=32;
 bmi.biClrImportant=0;
 bmi.biClrUsed=0;
 bmi.biCompression=BI_RGB;
 bmi.biHeight=bmp.bmHeight;
 bmi.biPlanes=1;
 bmi.biSize=sizeof(bmi);
 bmi.biSizeImage=0;
 bmi.biWidth=bmp.bmWidth;
 bmi.biXPelsPerMeter=0;
 bmi.biYPelsPerMeter=0;
 m_hObject=CreateDIBSection(dcScr, (LPBITMAPINFO)&bmi, 0, (void**)&m_lpData,
NULL, DIB_RGB_COLORS);
 hOldDest=dcDest.SelectObject(*this);
 hOldSrc=dcSrc.SelectObject(bmpSrc);
 if(!dcDest.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &dcSrc, 0, 0, SRCCOPY))
  return 0;
 dcSrc.SelectObject(hOldSrc);
 dcDest.SelectObject(hOldDest);
 dcSrc.DeleteDC();
 dcDest.DeleteDC();
 dcScr.Detach();
 ReleaseDC(NULL, hdc);
 bmpSrc.Detach();
 return 1;
}



HICON CCloneBitmap::MakeIcon(COLORREF clrTransparent)
{
 HICON hIcon;
 BITMAP bmp;
 DWORD dwSize;
 COLORREF *lpData;

 if(m_lpData==NULL)
  return NULL;
 if(!GetBitmap(&bmp))
  return NULL;
 if(bmp.bmHeight==0 || bmp.bmWidth==0)
  return NULL;

 HBITMAP hMask, hColor;
 LPCOLORREF lpColorData, lpMaskData;

 BITMAPINFOHEADER bmi;
 bmi.biBitCount=32;
 bmi.biClrImportant=0;
 bmi.biClrUsed=0;
 bmi.biCompression=BI_RGB;
 bmi.biHeight=32;
 bmi.biPlanes=1;
 bmi.biSize=sizeof(bmi);
 bmi.biSizeImage=0;
 bmi.biWidth=32;
 bmi.biXPelsPerMeter=0;
 bmi.biYPelsPerMeter=0;
 hColor=CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, 0, (void**)&lpColorData,
NULL, DIB_RGB_COLORS);
 if(hColor==NULL)
  return NULL;
 hMask=CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, 0, (void**)&lpMaskData,
NULL, DIB_RGB_COLORS);
 if(hMask==NULL)
 {
  ::DeleteObject(hColor);
  return NULL;
 }

 dwSize=32*32;

 lpData=m_lpData;

 for(DWORD d=0; d<dwSize; d++)
 {
  if(*lpData==clrTransparent)
  {
   lpMaskData[d]=0xFFFFFF;
   lpColorData[d]=0;
  }
  else
  {
   lpMaskData[d]=0;
   lpColorData[d]=*lpData;
  }
  lpData++;
 }


 ICONINFO ii;
 ii.fIcon=1;
 ii.hbmColor=hColor;
 ii.hbmMask=hMask;
 ii.xHotspot=0;
 ii.yHotspot=0;
 hIcon=CreateIconIndirect(&ii);

 ::DeleteObject(hMask);
 ::DeleteObject(hColor);

 return hIcon;
}




That's the important part, the whole CPP file is below:


















#include "stdafx.h"
#include "CloneBitmap.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCloneBitmap::CCloneBitmap()
{
 m_hObject=NULL;
 m_lpData=NULL;
}

CCloneBitmap::~CCloneBitmap()
{
}

int CCloneBitmap::Clone(HBITMAP hSrc)
{
 CBitmap bmpSrc;
 BITMAP bmp;
 if(!bmpSrc.Attach(hSrc))
  return 0;
 if(!bmpSrc.GetBitmap(&bmp))
  return 0;
 HGDIOBJ hOldSrc, hOldDest;
 if(m_hObject)
  DeleteObject();
 BITMAPINFOHEADER bmi;
 CDC dcSrc, dcDest, dcScr;
 HDC hdc;
 hdc=::GetDC(NULL);
 dcScr.Attach(hdc);
 dcDest.CreateCompatibleDC(&dcScr);
 dcSrc.CreateCompatibleDC(&dcScr);
 bmi.biBitCount=32;
 bmi.biClrImportant=0;
 bmi.biClrUsed=0;
 bmi.biCompression=BI_RGB;
 bmi.biHeight=bmp.bmHeight;
 bmi.biPlanes=1;
 bmi.biSize=sizeof(bmi);
 bmi.biSizeImage=0;
 bmi.biWidth=bmp.bmWidth;
 bmi.biXPelsPerMeter=0;
 bmi.biYPelsPerMeter=0;
 m_hObject=CreateDIBSection(dcScr, (LPBITMAPINFO)&bmi, 0, (void**)&m_lpData,
NULL, DIB_RGB_COLORS);
 hOldDest=dcDest.SelectObject(*this);
 hOldSrc=dcSrc.SelectObject(bmpSrc);
 if(!dcDest.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &dcSrc, 0, 0, SRCCOPY))
  return 0;
 dcSrc.SelectObject(hOldSrc);
 dcDest.SelectObject(hOldDest);
 dcSrc.DeleteDC();
 dcDest.DeleteDC();
 dcScr.Detach();
 ReleaseDC(NULL, hdc);
 bmpSrc.Detach();
 return 1;
}

int CCloneBitmap::ChangeColor(COLORREF clrOld, COLORREF clrNew)
{
 if(m_lpData==NULL)
  return 0;
 BITMAP bmp;
 if(!GetBitmap(&bmp))
  return 0;
 if(bmp.bmHeight==0 || bmp.bmWidth==0)
  return 0;
 DWORD dwSize;
 dwSize=bmp.bmHeight*bmp.bmWidth;

 COLORREF *pData;
 pData=m_lpData;

 for(DWORD d=0; d<dwSize; d++)
 {
  if(*pData==clrOld)
   *pData=clrNew;
  pData++;
 }

 return 1;
}

HICON CCloneBitmap::MakeIcon(COLORREF clrTransparent)
{
 HICON hIcon;
 BITMAP bmp;
 DWORD dwSize;
 COLORREF *lpData;

 if(m_lpData==NULL)
  return NULL;
 if(!GetBitmap(&bmp))
  return NULL;
 if(bmp.bmHeight==0 || bmp.bmWidth==0)
  return NULL;

 HBITMAP hMask, hColor;
 LPCOLORREF lpColorData, lpMaskData;

 BITMAPINFOHEADER bmi;
 bmi.biBitCount=32;
 bmi.biClrImportant=0;
 bmi.biClrUsed=0;
 bmi.biCompression=BI_RGB;
 bmi.biHeight=32;
 bmi.biPlanes=1;
 bmi.biSize=sizeof(bmi);
 bmi.biSizeImage=0;
 bmi.biWidth=32;
 bmi.biXPelsPerMeter=0;
 bmi.biYPelsPerMeter=0;
 hColor=CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, 0, (void**)&lpColorData,
NULL, DIB_RGB_COLORS);
 if(hColor==NULL)
  return NULL;
 hMask=CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, 0, (void**)&lpMaskData,
NULL, DIB_RGB_COLORS);
 if(hMask==NULL)
 {
  ::DeleteObject(hColor);
  return NULL;
 }

 dwSize=32*32;

 lpData=m_lpData;

 for(DWORD d=0; d<dwSize; d++)
 {
  if(*lpData==clrTransparent)
  {
   lpMaskData[d]=0xFFFFFF;
   lpColorData[d]=0;
  }
  else
  {
   lpMaskData[d]=0;
   lpColorData[d]=*lpData;
  }
  lpData++;
 }


 ICONINFO ii;
 ii.fIcon=1;
 ii.hbmColor=hColor;
 ii.hbmMask=hMask;
 ii.xHotspot=0;
 ii.yHotspot=0;
 hIcon=CreateIconIndirect(&ii);

 ::DeleteObject(hMask);
 ::DeleteObject(hColor);

 return hIcon;
}
Technical Details
Message-ID: <3ed81f0c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,f4fd0f9c26e98e6c,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-30 20:25:17 PST
From: "Chris" <Chris8852@adelphia.net>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: How do you create an icon from a bitmap?
Date: Fri, 30 May 2003 23:18:35 -0400
Lines: 343
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 68.65.164.124
Message-ID: <3ed81f0c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1054351116 68.65.164.124 (30 May 2003 20:18:36 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail