CLSID codecClsid; EncoderParameters encoderParameters; long quality; Status stat; GdiplusStartupInput gdiplusStartupInput; ULONG gdiplusToken; BOOL bRet = TRUE;
WCHAR wszcBmpFileName[ 512 ]; WCHAR wszJpgFileName[ 512 ]; WCHAR *lpwcBmpFileName = NULL; WCHAR *lpwcJpgFileName = NULL;#ifdef UNICODE wcscpy( wszcBmpFileName, lpszBMPFileName ); wcscpy( wszJpgFileName, lpszJPEGFileName ); #else lpwcBmpFileName = A2W( lpszBMPFileName ); lpwcJpgFileName = A2W( lpszJPEGFileName ); wcscpy( wszcBmpFileName, lpwcBmpFileName ); wcscpy( wszJpgFileName, lpwcJpgFileName ); #endif GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); { // Get an image from the disk. Image image( wszcBmpFileName ); // Get the CLSID of the JPEG codec. CSYSGlobal::GetCodecClsid( L"image/jpeg", &codecClsid ); // Before we call Image::Save, we must initialize an // EncoderParameters object. The EncoderParameters object // has an array of EncoderParameter objects. In this // case, there is only one EncoderParameter object in the array. // The one EncoderParameter object has an array of values. // In this case, there is only one value ( of type LONG ) // in the array. We will set this value to 0, 50, and 100. 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 0. quality = lQuality; encoderParameters.Parameter[0].Value = &quality; stat = image.Save( wszJpgFileName, &codecClsid, &encoderParameters); if( stat == Ok ) { //AfxMessageBox( _T( "保存JPEG文件成功" ) ); bRet = TRUE; } else { AfxMessageBox( _T( "保存JPEG文件失败!" ) ); bRet = FALSE; } } GdiplusShutdown(gdiplusToken);
很简单...
CImage img;
img.Load(L"D:\\VC2005program\\BmpToJpg\\abc.bmp");
img.Save(L"D:\\VC2005program\\BmpToJpg\\abc.jpg",ImageFormatJPEG);
可是提示错误信息呀:错误 1 error C2065: “ImageFormatJPEG”: 未声明的标识符 d:\VC2005program\BmpToJpg\BmpToJpgDlg.cpp 160
using namespace Gdiplus ;
#include <gdiplusimaging.h>
=====
按照上面的做法试试
=========================================================
//=======================================================================
// 函数原型: BOOL CSYSGlobal::BMP2JPEG(LPCTSTR lpszBMPFileName, LPCTSTR lpszJPEGFileName , long lQuality /*= 100*/ )
// 功能描述: BMP文件转换成JPEG文件
// 参数说明: 名称 类型 说明
// lpszBMPFileName LPCTSTR 要转换的BMP文件名( 已存在文件 )
// lpszJPEGFileName LPCTSTR JPEG文件名( 根据BMP转换 新生成文件 )
// lQuality long 图片质量 ( 默认100% )
// 返 回 值:
// 依 赖 于: GDI+, ATL字符转换( 用 A2W() 转换ASCII -> Unicode )
// 具体 做法如下:
// 1. 加入GDI+支持: 在StdAfx.h的 "#endif // _AFX_NO_AFXCMN_SUPPORT" 前加入如下代码:
// #include <gdiplus.h>
// #include <GdiPlusEnums.h>
// using namespace Gdiplus;
// #pragma comment( lib, "gdiplus.lib" )
//
// //加入ATL字符转换支持: 在StdAfx.h中加入
// #include <atlconv.h>
//-----------------------------------------------------------------------
// 引 用 表: 无
// 更 新 表: 无
// 被引用于: 全局
// 创建日期: 2007年12月21日 16时02分57秒
// 修改日期:
// 修改说明:
//=======================================================================
BOOL CSYSGlobal::BMP2JPEG(LPCTSTR lpszBMPFileName, LPCTSTR lpszJPEGFileName, long lQuality /*= 100*/ )
{
USES_CONVERSION; //调用 A2W() 等宏时,必须先执行该行!!!
CLSID codecClsid;
EncoderParameters encoderParameters;
long quality;
Status stat;
GdiplusStartupInput gdiplusStartupInput;
ULONG gdiplusToken;
BOOL bRet = TRUE;
WCHAR wszcBmpFileName[ 512 ];
WCHAR wszJpgFileName[ 512 ];
WCHAR *lpwcBmpFileName = NULL;
WCHAR *lpwcJpgFileName = NULL;#ifdef UNICODE
wcscpy( wszcBmpFileName, lpszBMPFileName );
wcscpy( wszJpgFileName, lpszJPEGFileName );
#else
lpwcBmpFileName = A2W( lpszBMPFileName );
lpwcJpgFileName = A2W( lpszJPEGFileName );
wcscpy( wszcBmpFileName, lpwcBmpFileName );
wcscpy( wszJpgFileName, lpwcJpgFileName );
#endif
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
// Get an image from the disk.
Image image( wszcBmpFileName ); // Get the CLSID of the JPEG codec.
CSYSGlobal::GetCodecClsid( L"image/jpeg", &codecClsid ); // Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value ( of type LONG )
// in the array. We will set this value to 0, 50, and 100. 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 0.
quality = lQuality;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save( wszJpgFileName, &codecClsid, &encoderParameters); if( stat == Ok )
{
//AfxMessageBox( _T( "保存JPEG文件成功" ) );
bRet = TRUE;
}
else
{
AfxMessageBox( _T( "保存JPEG文件失败!" ) );
bRet = FALSE;
}
} GdiplusShutdown(gdiplusToken);
return bRet;
}//=======================================================================
// 函数原型: int CSYSGlobal::GetCodecClsid(const WCHAR *format, CLSID *pClsid)
// 功能描述:
// 参数说明: 名称 类型 说明
// 返 回 值:
// 依 赖 于: 无
// 引 用 表: 无
// 更 新 表: 无
// 被引用于: BOOL CSYSGlobal::BMP2JPEG(LPCTSTR lpszBMPFileName, LPCTSTR lpszJPEGFileName )
// 创建日期: 2007年12月21日 16时43分28秒
// 修改日期:
// 修改说明:
//=======================================================================
int CSYSGlobal::GetCodecClsid(const WCHAR *format, CLSID *pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
{
return -1; // Failure
}
pImageCodecInfo = (ImageCodecInfo*)( malloc( size ) );
if(pImageCodecInfo == NULL)
{
return -1; // Failure
}
GetImageEncoders(num, size, pImageCodecInfo); for( int j = 0; j < num; ++j )
{
if( wcscmp(pImageCodecInfo[j].MimeType, format ) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
return j; // Success
}
} // for return -1; // Failure
}