1.下载https://freeimage.sourceforge.io/
2.安装https://blog.csdn.net/woainishifu/article/details/71451119(可以参考,我下载的版本比他的高,直接安装没有出错)
3.使用,编译时加上-lfreeimage链接到库
static void save_png()
{
const char* fileName = "test.png";
int w = 250;
int h = 250;
BYTE* pixels = new BYTE[w * h * 4];
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
int i = x * w + y;
pixels[i * 4 + 0] = ColorMap[y][2];
pixels[i * 4 + 1] = ColorMap[y][1];
pixels[i * 4 + 2] = ColorMap[y][0];
pixels[i * 4 + 3] = 255;
}
}
//save-----------------------------------------------------------------
//图片格式
FREE_IMAGE_FORMAT fif = FIF_PNG;
//图片像素深度
int bpp = 8 * 4;
//图片宽
int width = w;
//图片高
int height = h;
//图片每行像素(freeimage自动做了32位对齐)
int pitch = width * 4;
//图片像素数组
BYTE* bits = pixels;
//图片所有数据
FIBITMAP* dib = FreeImage_ConvertFromRawBits(bits, width, height, pitch, bpp, 0, 0, 0);
FreeImage_Save(FIF_PNG, dib, fileName);
//释放
FreeImage_Unload(dib);
delete[] pixels;
}