stdafx.h
#include "gdiplus.h"
using namespace Gdiplus;
#pragma comment( lib, "gdiplus.lib" )
class CImageResizeApp : public CWinApp
{
public:
CImageResizeApp();
GdiplusStartupInput m_Gdistart;
ULONG_PTR m_GdiplusToken;
BOOL CImageResizeApp::InitInstance()
{
GdiplusStartup(&m_GdiplusToken,&m_Gdistart,NULL);
}
int CImageResizeApp::ExitInstance()
{
// TODO: 在此添加专用代码和/或调用基类
GdiplusShutdown(m_GdiplusToken);
return CWinApp::ExitInstance();
}
void ResizePicture(LPCWSTR lpSrcFile,LPCWSTR lpDstFile,int wRatio,int hRatio)
{
Image srcImg(lpSrcFile);
int srcWidth=srcImg.GetWidth();
int srcHeight=srcImg.GetHeight();
//计算调整后的Width和Height
int dstWidth=srcWidth*wRatio;
int dstHeight=srcHeight*hRatio;
// Construct a Graphics object based on the image.
Graphics imgGraphics(&srcImg);
Bitmap bitmap(dstWidth,dstHeight,&imgGraphics);
Graphics bmpGraphics(&bitmap);
bmpGraphics.DrawImage(&srcImg,0,0,dstWidth,dstHeight);
// Save the altered image.
const wchar_t* lpExt=wcsrchr(lpSrcFile, '.');
if(!lpExt)
return;
lpExt++;
LPWSTR lpEncoder;
switch(*lpExt)
{
case 'J ':
case 'j ':
lpEncoder=L"image/jpeg ";
break;
case 'P ':
case 'p ':
lpEncoder=L"image/png ";
break;
case 'B ':
case 'b ':
lpEncoder=L"image/bmp ";
break;
case 'G ':
case 'g ':
lpEncoder=L"image/gif ";
break;
case 't ':
case 'T ':
lpEncoder=L"image/tiff ";
break;
default:
lpEncoder=L"image/jpeg ";
}
CLSID imgClsid;
GetEncoderClsid(lpEncoder, &imgClsid);
bitmap.Save(lpDstFile,&imgClsid,NULL);
}
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(wcsstr(pImageCodecInfo[j].MimeType, format))
{
*pClsid= pImageCodecInfo[1].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
call ResizePicture(L"QQ2.png",L"test.png",2,2);