使用GDI+将bmp格式图片转换为PNG格式

本文介绍使用GDI+库进行图片格式转换的方法,通过示例代码展示了如何将BMP格式图片转换为PNG格式,同时比较了转换前后图片文件大小的变化。

最近在学习GDI+,刚好学习到了图片的编码和解码部分。bmp格式的图片没有进行压缩,所以图片所占内存比较大,而PNG图片经过压缩后体积较小,所以我想进行下图片格式转换。刚好在微软GDI+的文档中有这么一个实例,我将他贴出来,并展现转换后两个图片所占空间的大小。

#include <windows.h>
#include <ObjIdl.h>
#include <iostream>
#include <gdiplus.h>
using namespace std;

#pragma comment(lib, "Gdiplus.lib")
using namespace Gdiplus;

int GetEncoderClsid(const WCHAR* format, CLSID* pClisid)
{
	UINT num = 0;
	UINT size = 0;

	ImageCodecInfo* pImageCodecInfo = NULL;
	GetImageEncodersSize(&num, &size);
	if (size == 0)
		return -1;
	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if (pImageCodecInfo == NULL)
		return -1;
	GetImageEncoders(num, size, pImageCodecInfo);

	for (UINT j = 0; j < num; ++j)
	{
		if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
		{
			*pClisid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;
		}
	}
	free(pImageCodecInfo);
	return -1;

}

int main()
{
	//初始化GDI+
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput,NULL);

	CLSID encoderClsid;
	Status stat;
	Image* image = new Image(L"C:\\Users\\DreamXY\\Desktop\\4.bmp");
	//Get the CLSID of the PNG encoder
	GetEncoderClsid(L"image/png", &encoderClsid);

	stat = image->Save(L"4.png", &encoderClsid, NULL);
	if (stat == Ok)
		cout << "4.png was saved successfully" << endl;
	else
		cout << "failure: stat = %d " << stat;

	delete image;
	GdiplusShutdown(gdiplusToken);

	return 0;
}

上面程序的原理是:首先获取你想要转换成哪个图像格式的编码器ID(我是将其理解为这个),当然这个编码器必须在windows上有的,如果没有就返回-1。找到对应图片格式的编码器ID,之后用

Image类中的Save方法将其保存到其他的图片格式。

需要注意的是,单纯的修改后缀名是没有用的。比如将程序中4.bmp改为4.png,这个操作是没有任何效果的,因为图片内的数据排列格式并没有改变,也就是说并没有对图片内部进行重新编码。

转换后图片大小对比:

首先是原图像4.bmp

 这是转换后的4.png图像

由上面两图可以看出,转换成PNG格式图片后,图片的大小明显变小了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamXY12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值