GDI+ 保存图片jpeg压缩

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)  
{
	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 )  
		{  
			*pClsid = pImageCodecInfo[j].Clsid;  
			free(pImageCodecInfo);  
			return j;  
		}      
	}  
	free(pImageCodecInfo);  
	return -1;  
}  

bool SaveFile(Bitmap* pImage, const wchar_t* pFileName){bool bRes = true;EncoderParameters encoderParameters; CLSID jpgClsid; GetEncoderClsid(L"image/jpeg", &jpgClsid); //image/bmp 压缩的话只能是image/jpeg ;image/bmp 的话quality不生效encoderParameters.Count = 1; encoderParameters.Parameter[0].Guid = EncoderQuality; encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong; encoderParameters.Parameter[0].NumberOfValues = 1; // Save the image as a JPEG with quality level 100. ULONG quality; quality = 100; encoderParameters.Parameter[0].Value = &quality; Status status = pImage->Save(pFileName, &jpgClsid, &encoderParameters); if (status != Ok) {bRes = false;wprintf(L"%d Attempt to save %s failed.\n", status, pFileName); } return bRes;} 重点是GetEncoderClsid(L"image/jpeg", &jpgClsid); 第一个参数 如果是L"image/bmp" ,quality 设置值无效

你可能感兴趣的:(C++)