[C/C++] RGBA数组生成Windows下的ico文件
Widows下只认.ico格式为程序图标,这是一种源自bmp的图片格式。ico中的颜色可以通过color table来表示也可以直接通过RGBA数组表示。
ico文件的格式见官方文档:
http://msdn.microsoft.com/en-us/library/ms997538.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376%28v=vs.85%29.aspx
其它一些关于ico文件的文档见:
比较详细的原理介绍:http://blogs.msdn.com/b/oldnewthing/archive/2010/10/18/10077133.aspx
ico格式描述:http://www.daubnet.com/en/file-format-ico
Programming Tips & Tricks:http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html
bmp和ico格式描述:http://www.digicamsoft.com/bmp/bmp.html
ico文件相关实现:
IconImagePlugin in Python PIL: http://code.google.com/p/casadebender/source/browse/python/PIL/Win32IconImagePlugin.py
construct ico from RGBA array - a python implementation:http://fayaa.com/code/view/6094/
png2ico:http://www.winterdrache.de/freeware/png2ico/
下面是一个用从RGBA生成ico文件的C语言实现,生成一个红色的图标文件 - test.ico。
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>void Fwrite(FILE * f, char * data, int byte)
{if (1 != fwrite(data, byte, 1, f)) {perror("fwrite error");exit(1);}
}void WriteByte(FILE* f, unsigned int val, int byte)
{char data[4];assert(byte <= 4);memcpy((void *)data, (void *)&val, byte);Fwrite(f, data, byte);
};void generate_ico_file(const char * filename, char * body, int width, int height, int has_alpha)
{int x, y;int index = 0;int Size = 0;int offset= 6 + 1 * 16;int bpp = 32;FILE* outfile=fopen(filename,"wb");if (outfile==NULL) {perror("fopen error"); exit(1);};WriteByte(outfile,0, 2); //idReservedWriteByte(outfile,1, 2); //idTypeWriteByte(outfile,1, 2); //idCount WriteByte(outfile,width, 1); //bWidthWriteByte(outfile,height, 1); //bHeightWriteByte(outfile,0, 1); //bColorCountWriteByte(outfile,0, 1); //bReservedWriteByte(outfile,1, 2); //wPlanesWriteByte(outfile,bpp, 2); //wBitCountSize = 40 + height * ((width + 31) / 32 * 32 / 8 + width * 3); //Note 4 bytes alignmentif (bpp == 32)Size += height * width;WriteByte(outfile,Size, 4); //dwBytesInResWriteByte(outfile,offset, 4); //dwImageOffsetWriteByte(outfile,40, 4); //biSizeWriteByte(outfile,width, 4); //biWidthWriteByte(outfile,2 * height, 4); //biHeight WriteByte(outfile,1, 2); //biPlanesWriteByte(outfile,bpp, 2); //biBitCountWriteByte(outfile,0, 4); //biCompressionWriteByte(outfile,0, 4); //biSizeImageWriteByte(outfile,0, 4); //biXPelsPerMeter WriteByte(outfile,0, 4); //biYPelsPerMeterWriteByte(outfile,0, 4); //biClrUsed WriteByte(outfile,0, 4); //biClrImportant// XOR maskfor (y= height - 1; y >= 0; --y) { for (x = 0; x < width; ++x) {index = (y * width + x) * 4;WriteByte(outfile, body[index], 1); //Blue WriteByte(outfile, body[index + 1], 1); //GreenWriteByte(outfile, body[index + 2], 1); //RedWriteByte(outfile, has_alpha ? body[index + 3] : 255, 1); //Alpha}}// AND maskfor (y = 0; y < (height * ((width + 31) / 32 * 32 / 8)); ++y) { WriteByte(outfile, 0, 1);}fclose(outfile);
}#define WIDTH 72
#define HEIGHT 72int main()
{int image[WIDTH * HEIGHT];int i, j;for (i = 0; i < HEIGHT; ++i) {for (j = 0; j < WIDTH; ++j) {image[i * WIDTH + j] = 0xFFFF0000; // pure red icon, for test}}generate_ico_file("test.ico", (char *)image, WIDTH, HEIGHT, 1);return 0;
}
[C/C++] RGBA数组生成Windows下的ico文件相关推荐
- windows下的.DLL文件
DLL(Dynamic Link Library)文件为动态链接库文件,又称为"应用程序拓展",是软件文件类型.在windows中,许多应用程序并不是一个完整的可执行文件,它们被分 ...
- Windows 下目录及文件向Linux同步
本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...
- windows下批处理删除文件及注册表项
1.windows下批处理删除注册表项 假如我要清除WinRAR的历史记录,可以新建个批处理文件, 然后在里面加入如下文字: reg delete HKEY_CURRENT_USER/Software ...
- windows下批处理删除文件夹、删除文件、if exist用法
windows下批处理删除文件夹.删除文件.if exist用法 开发中用到Window批处理del.rd.if exist,总结用法便于后续使用. windows下批处理删除文件: * 使用del指 ...
- 在windows下看linux文件-Explore2fs(转)
在windows下看linux文件-Explore2fs(转)[@more@]Explore2fs 1.00 pre 6b 你曾经在Linux操作系统下载了很多文件,但是当你重新开启在Windows9 ...
- 怎么生成windows上的ico图标
要生成 Windows 上的 ICO 图标,你可以使用专门的图标生成工具,比如说: IcoMaker:这是一款免费的图标生成工具,支持 Windows 和 Mac 平台.它可以让你轻松地将图像文件转换 ...
- 系统运维系列 之在Windows下可以查看文件夹大小的工具TreeSize Free
给大家推荐一个在Windows下可以查看文件夹大小的工具TreeSize Free. 下载链接: https://customers.jam-software.de/downloadTrial.php ...
- windows下修改hosts文件不需要重启也能生效。
windows下修改hosts文件不需要重启也能生效. 方法步骤: 一.单击Windows键,选择运行,输入CMD,回车. 二.在命令提示符中输入"ipconfig/displaydns&q ...
- Python生成Windows可执行exe文件
环境 python3.6.5 pyinstaller3.5 windows 10 下载地址 python:https://www.python.org/ftp/python/3.6.5/python- ...
最新文章
- c# 关闭软件 进程 杀死进程
- 【转】CentOS下firefox安装flash说明
- DHTMLX-Tabbar
- yii2使用 db log
- c++ 重载 覆盖 隐藏的区别和执行方式
- Win8离线安装.net framework组件
- C/C++二叉树前序遍历,中序遍历,后序遍历
- 【CCF】201709-1打酱油
- 收藏 | 90+深度学习开源数据集整理:包括目标检测、工业缺陷、图像分割等多个方向(附下载)...
- 华为交换机删除配置命令access_华为交换机基本配置命令详解
- 《浪潮之巅》内容整理
- linux 双显示器 异常,终于搞定双显示器了
- Java设计模式之——策略模式(Strategy)
- luogu4188 [USACO18JAN]Lifeguards S
- 【亲测可用】云里黑白第十三回——解决出现问题,你的PIN不可用,单击以重新设置PIN,诊断启动,禁用服务
- 最长递增子序列 java_最长递增子序列问题---动态规划
- GB9706.1-2007名词解释:电气间隙、爬电距离,绝缘、接地等
- 关于STM32 GPIO配置基础概括
- 三维实景沙盘数字模拟沙盘M3DGIS系统开发教程第18课
- Sallen-Key 有源滤波器-ONE