C++设置bmp、jpg、png、Tiff图像打印分辨率DPI

直接上代码:

//BMP图片格式修改DPI,bmp图片分辨率单位是象素/米表示
bool SetResolution_BMP(const char* path, int iResolution)
{
	FILE* file = fopen(path, "rb+");// - 打开图片文件 
	if (!file)return false;
	int len = _filelength(_fileno(file));// - 获取文件大小 
	char* buf = new char[len];
	fread(buf, sizeof(char), len, file);// - 将图片数据读入缓存 
	char* pResolution = (char*)&iResolution;// - iResolution为要设置的分辨率的数值,如72dpi 
	// - 设置JPG图片的分辨率 
	//buf[0x0D] = 1;// - 设置使用图片密度单位 
	// - 水平密度,水平分辨率 
	buf[0x26] = pResolution[0];
	buf[0x27] = pResolution[1];
	buf[0x28] = pResolution[2];
	buf[0x29] = pResolution[3];
	// - 垂直密度,垂直分辨率 
	buf[0x2A] = pResolution[0];
	buf[0x2B] = pResolution[1];
	buf[0x2C] = pResolution[2];
	buf[0x2D] = pResolution[3];
	// - 将文件指针移动回到文件开头 
	fseek(file, 0, SEEK_SET);
	// - 将数据写入文件,覆盖原始的数据,让修改生效 
	fwrite(buf, sizeof(char), len, file);
	fclose(file);
	delete[]buf;
	return true;

}
//Jpeg模式图片修改DPI,jpg图片分辨率单位是象素/英寸表示
bool SetResolution_JPEG(const char* path, int iResolution)
{
	FILE* file = fopen(path, "rb+");// - 打开图片文件 
	if (!file)return false;
	int len = _filelength(_fileno(file));// - 获取文件大小 
	char* buf = new char[len];
	fread(buf, sizeof(char), len, file);// - 将图片数据读入缓存 
	char* pResolution = (char*)&iResolution;// - iResolution为要设置的分辨率的数值,如72dpi 
	// - 设置JPG图片的分辨率 
	buf[0x0D] = 1;// - 设置使用图片密度单位 
	// - 水平密度,水平分辨率 
	buf[0x0E] = pResolution[1];
	buf[0x0F] = pResolution[0];
	// - 垂直密度,垂直分辨率 
	buf[0x10] = pResolution[1];
	buf[0x11] = pResolution[0];

	// - 将文件指针移动回到文件开头 
	fseek(file, 0, SEEK_SET);
	// - 将数据写入文件,覆盖原始的数据,让修改生效 
	fwrite(buf, sizeof(char), len, file);
	fclose(file);
	delete[]buf;
	return true;

}


//Tiff模式图片修改DPI
bool SetResolution_TIFF(const char* path, int iResolution)
{
	FILE* file = fopen(path, "rb+");// - 打开图片文件 
	if (!file)return false;
	int len = _filelength(_fileno(file));// - 获取文件大小 
	char* buf = new char[len];
	fread(buf, sizeof(char), len, file);// - 将图片数据读入缓存 
	char* pResolution = (char*)&iResolution;// - iResolution为要设置的分辨率的数值,如72dpi 
	// - 设置JPG图片的分辨率 
	buf[0x0D] = 1;// - 设置使用图片密度单位 
	// - 水平密度,水平分辨率 
	buf[0x0E] = pResolution[1];
	buf[0x0F] = pResolution[0];
	// - 垂直密度,垂直分辨率 
	buf[0x10] = pResolution[1];
	buf[0x11] = pResolution[0];

	// - 将文件指针移动回到文件开头 
	fseek(file, 0, SEEK_SET);
	// - 将数据写入文件,覆盖原始的数据,让修改生效 
	fwrite(buf, sizeof(char), len, file);
	fclose(file);
	delete[]buf;
	return true;

}

//PNG图片格式修改DPI,png图片分辨率单位是象素/米表示,参考https://blog.csdn.net/joqian/article/details/8290389
bool SetResolution_PNG(const char* path, int iResolution)
{
	FILE* file = fopen(path, "rb+");// - 打开图片文件 
	if (!file)return false;
	int len = _filelength(_fileno(file));// - 获取文件大小 
	char* buf = new char[len];
	fread(buf, sizeof(char), len, file);// - 将图片数据读入缓存 
	char* pResolution = (char*)&iResolution;// - iResolution为要设置的分辨率的数值,如72dpi 

	//PNG图片pHYs模块可能有可能没有,如果有就改写,无就增加 70 48 59 73
	//pHYs模块存在位置0x25 0x26 0x27 0x28
	int len2 = len + 21;
	char* buf_copy = new char[len2];//插入pHYs模块

	if (len2 < 0x30) //图像内存异常
		return false;

	if (buf[0x25] != 0x70 || buf[0x26] != 0x48 || buf[0x27] != 0x59 || buf[0x28] != 0x73) //不存在pHYs块
	{
		for (int i = 0; i < 0x21; i++) //前20位不变
		{
			buf_copy[i] = buf[i];
		}
		buf_copy[0x21] = 0;
		buf_copy[0x22] = 0;
		buf_copy[0x23] = 0;
		buf_copy[0x24] = 9;    //pHYs模块固定,长度位9位

		buf_copy[0x25] = 0x70;
		buf_copy[0x26] = 0x48;
		buf_copy[0x27] = 0x59;
		buf_copy[0x28] = 0x73; //pHYs块头

		//四位横向分辨率 23622像素/米 600像素/英寸换算
		buf_copy[0x29] = pResolution[3];
		buf_copy[0x2A] = pResolution[2];
		buf_copy[0x2B] = pResolution[1];
		buf_copy[0x2C] = pResolution[0];

		//四位纵向分辨率 23622像素/米 600像素/英寸换算
		buf_copy[0x2D] = pResolution[3];
		buf_copy[0x2E] = pResolution[2];
		buf_copy[0x2F] = pResolution[1];
		buf_copy[0x30] = pResolution[0];

		buf_copy[0x31] = 1;  //表示定义单位为米

		//buf_copy[0x32] = 0x00;
		//buf_copy[0x33] = 0x9A;
		//buf_copy[0x34] = 0x9C;
		//buf_copy[0x35] = 0x18; //CRC值

		buf_copy[0x32] = 0x14;
		buf_copy[0x33] = 0x94;
		buf_copy[0x34] = 0x43;
		buf_copy[0x35] = 0x41; //CRC值 600dpi的循环冗余校验码,在线计算器http://www.ip33.com/crc.html

		for (int i = 0x21; i < len; i++)
		{
			buf_copy[0x35 + i - 0x20] = buf[i];
		}
		// - 将文件指针移动回到文件开头 
		fseek(file, 0, SEEK_SET);
		// - 将数据写入文件,覆盖原始的数据,让修改生效 
		fwrite(buf_copy, sizeof(char), len + 21, file);
		fclose(file);
	}
	else
	{
		//四位横向分辨率 23622像素/米 600像素/英寸换算
		buf[0x29] = pResolution[3];
		buf[0x2A] = pResolution[2];
		buf[0x2B] = pResolution[1];
		buf[0x2C] = pResolution[0];

		//四位纵向分辨率 23622像素/米 600像素/英寸换算
		buf[0x2D] = pResolution[3];
		buf[0x2E] = pResolution[2];
		buf[0x2F] = pResolution[1];
		buf[0x30] = pResolution[0];

		buf[0x31] = 1;  //表示定义单位为米

		buf[0x32] = 0x14;
		buf[0x33] = 0x94;
		buf[0x34] = 0x43;
		buf[0x35] = 0x41; //CRC值

		fseek(file, 0, SEEK_SET);
		// - 将数据写入文件,覆盖原始的数据,让修改生效 
		fwrite(buf, sizeof(char), len, file);
		fclose(file);

	}
	delete[]buf;
	delete[]buf_copy;

	return true;
}

注意:png设置dpi,需要根据不同的值设置不同的CRC校验码

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值