[高分]如何将Icon写入到指定的内存Buffer,再从Buffer中读取出来并创建icon?

erigido 2012-03-16 03:01:17
类似于Delphi的Icon.SaveToStream以及Icon.LoadFromStream.谢谢!
...全文
99 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2012-03-17
  • 打赏
  • 举报
回复

BOOL WriteIcon2File(LPBITMAPINFOHEADER pbifh)
{// Open file
CString name="any.ico";
HANDLE hFile;
DWORD dwBytesWritten;
hFile = CreateFile(name,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
// Write the header (the first 3 words of type ICONDIR)
WriteICOHeader(hFile,1);// 1 icon
// Color bits
DWORD lpvColorBits=(DWORD)pbifh+sizeof(BITMAPINFOHEADER)+PaletteSize(pbifh);
DWORD dibColorSize = WIDTHBYTES(pbifh->biWidth*pbifh->biBitCount)*pbifh->biHeight/2;
// Mask bits
DWORD lpvMaskBits=lpvColorBits+dibColorSize;
DWORD dibMaskSize=WIDTHBYTES(pbifh->biWidth*1)*pbifh->biHeight/2;
// Write the ICONDIRENTRY
ICONDIRENTRY ide;
// Convert internal format to ICONDIRENTRY
ide.bWidth = (BYTE)pbifh->biWidth;//32
ide.bHeight = (BYTE)pbifh->biHeight;//32
ide.bReserved = 0;
ide.wPlanes = pbifh->biPlanes;//1
ide.wBitCount = pbifh->biBitCount;//
//
if((ide.wPlanes * ide.wBitCount) >= 8) ide.bColorCount = 0;
else ide.bColorCount = 1 << (ide.wPlanes * ide.wBitCount);
// 512+128+40+64=744
ide.dwBytesInRes = dibColorSize+dibMaskSize+sizeof(BITMAPINFOHEADER)+PaletteSize(pbifh);//
ide.dwImageOffset = 3*sizeof(WORD) + /* entryNum=1 */ sizeof(ICONDIRENTRY)+PaletteSize(pbifh);
// Write ICONDIRENTRY
WriteFile(hFile,&ide,sizeof(ICONDIRENTRY),&dwBytesWritten,NULL);
// write bifh + pal
WriteFile(hFile,(VOID*)pbifh,sizeof(BITMAPINFOHEADER)+PaletteSize(pbifh),&dwBytesWritten,0);//
//write color dibs
WriteFile(hFile,(VOID*)lpvColorBits,dibColorSize,&dwBytesWritten,0);//616
// write mask dibs
WriteFile(hFile,(VOID*)lpvMaskBits,dibMaskSize,&dwBytesWritten,0);//128
//
CloseHandle(hFile);
AfxMessageBox("Selected Icon has saved to file 'Any.ico'\n");
//
return TRUE;
}
向立天 2012-03-17
  • 打赏
  • 举报
回复
如果你能拿到数据那直接复制到指定缓冲不就可以了
Angle_sean4 2012-03-17
  • 打赏
  • 举报
回复
loadIcon不就可以吗
gold_water 2012-03-16
  • 打赏
  • 举报
回复
Delphi的函数,估计没几个人知道具体干什么的吧;具体说说你想实现什么功能。
erigido 2012-03-16
  • 打赏
  • 举报
回复
在线等啊!大虾们!

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧