关于BMP格式到ICO格式的转换
这是我转换的代码,但是看不到结果,哪位高人给参谋参谋,都晕了……
-----.h-----
//---------------------------------------
#ifndef Unt_BMP2ICOH
#define Unt_BMP2ICOH
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Dialogs.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TImage *Image1;
TButton *Button1;
TOpenDialog *OpenDialog1;
void __fastcall Button1Click(TObject *Sender);
private:// User declarations
typedef struct tagIconDirEntry
{
BYTE bWidth; // 图标图片的显示宽度,以像素为单位,最大值为255 (0x10=16D)
BYTE bHeight; // 图标图片的显示高度,以像素为单位,最大值为255 (0x10=16D)
BYTE bColorCount; // 图标图片的颜色数(0x00)
BYTE bReserved; // 保留域总是 0 (0x00)
WORD wPlanes; // 图标图片的位面数 (0x0000)
WORD wBitCount; // 图标图片的颜色深度 (0x0000)
DWORD dwBytesInRes; // 图标图片占用的数据量 (0x00000568)
DWORD dwImageOffset; // 图标图片的开始位置 (0x00000046);
}tagICONDIRENTRY;
typedef struct tagIconDir
{
WORD idReserved; // 保留域,目前始终为 0(开始的数据0x0000)
WORD idType; //定义为资源类型,图标值为 0x0001,光标是0x0002(0x0001)
WORD idCount; //idCount 表示的是这个文件里包含了几个图标(0x0004)
tagIconDirEntry idEntries[1];
}tagICONDIR;
typedef struct tagIconImage
{
TBitmapInfoHeader icHeader; //BMP文件的信息头
TRGBQuad icColors[1];
BYTE icXOR[1];
BYTE icAND[1];
}tagICONIMAGE;
BOOL __fastcall MakeICOHead(TMemoryStream *Mem);
BOOL __fastcall MakeICOData(TMemoryStream *Mem);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
-----.cpp-----
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unt_BMP2ICO.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
TMemoryStream *BMPStream;
Graphics::TBitmap *Bmp;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
/*
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
*/
}
//---------------------------------------
BOOL __fastcall TForm1::MakeICOHead(TMemoryStream *Mem)
{
tagBITMAPFILEHEADER BMPHead1;
TBitmapInfoHeader BMPHead2;
tagICONDIR IconHand;
DWORD BitsTotal;
BOOL RESULT;
Mem-> Position = 0;
Mem-> Read(&BMPHead1,sizeof(tagBITMAPFILEHEADER));
Mem-> Read(&BMPHead2,sizeof(TBitmapInfoHeader));
//if (BMPHead2.biCompression != 0)
//{
if (BMPHead2.biWidth <= 255 && BMPHead2.biHeight <= 255)
{
IconHand.idEntries[0].bWidth = Byte(BMPHead2.biWidth);
IconHand.idEntries[0].bHeight = Byte(BMPHead2.biHeight);
BitsTotal = (Mem-> Size - 54) * 2 + 40;
IconHand.idEntries[0].dwBytesInRes = BitsTotal;
IconHand.idEntries[0].dwImageOffset = 0x00000016;
RESULT = TRUE;
return RESULT;
}
Mem-> Position = 0;
//}
}
//---------------------------------------
BOOL __fastcall TForm1::MakeICOData(TMemoryStream *Mem)
{
TMemoryStream *Mem1,*Mem2;
long int Size;
TBitmapInfoHeader BmtMapHandle2;
tagICONDIR IconHand;
BOOL RESULT;
Mem1 = new TMemoryStream;
Mem2 = new TMemoryStream;
Size = Mem-> Size - 14;
Mem-> Position = 14;
try
{
Mem1-> SetSize((int)Size);
Mem-> Read(Mem1-> Memory,Size);
Mem1-> Seek(0,soFromBeginning);
Mem1-> Read(&BmtMapHandle2,sizeof(TBitmapInfoHeader));
Mem2-> SetSize((int)Size - 40);
memset(Mem2-> Memory,Size - 40,0x00);
Mem2-> Position = 0;
//BmtMapHandle2.biHeight = IconHand.idEntries[0].bHeight * 2;
BmtMapHandle2.biSizeImage = Mem2-> Size * 2;
Mem1-> Seek(0,soFromBeginning);
Mem1-> Write(&BmtMapHandle2,sizeof(TBitmapInfoHeader));
Mem1-> Position = 0;
Mem-> Size = 0;
Mem-> Write(&IconHand,sizeof(tagICONDIR));
Mem-> Write(Mem1-> Memory,Mem1-> Size);
Mem-> Write(Mem2-> Memory,Mem2-> Size);
RESULT = TRUE;
return RESULT;
}
__finally
{
delete Mem1,Mem2;
Mem1 = NULL;
Mem2 = NULL;
}
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (OpenDialog1-> Execute())
Image1-> Picture-> LoadFromFile(OpenDialog1-> FileName);
Bmp = new Graphics::TBitmap;
Bmp-> Width = 32;
Bmp-> Height = 32;
Bmp-> PixelFormat = pf24bit;
Bmp-> Canvas-> StretchDraw(Rect(0,0,Bmp-> Width,Bmp-> Height),Image1-> Picture-> Bitmap);
BMPStream = new TMemoryStream();
BMPStream-> Position = 0;
Bmp-> SaveToStream(BMPStream);
if (MakeICOHead(BMPStream))
if (MakeICOData(BMPStream))
;
delete BMPStream;
delete Bmp;
}
//---------------------------------------
[解决办法]
http://faq.csdn.net/read/211872.html
------解决方案--------------------
http://topic.csdn.net/t/20020524/21/750455.html