您的位置:首页 > 其它

gdiplus将一种图片格式转换成另外一个指定图片格式,且可设置图片大小

2015-12-14 16:40 399 查看
int TransPicture(LPCWSTR lpSrcFile,LPCWSTR lpDstExt, long nWidth, long nHeight, CStringW &strDstFile)
{
USES_CONVERSION;
CFileFind find;
#ifdef UNICODE

if (!find.FindFile(lpSrcFile))//文件不存在
#else
if (!find.FindFile(W2A(lpSrcFile)))//文件不存在
#endif

{
find.Close();
return 0;
}
find.Close();

Image *psrcImg=NULL;
psrcImg = Image::FromFile(lpSrcFile);
if (!psrcImg || psrcImg->GetLastStatus() != Ok)
{
return 0;
}
int srcWidth=psrcImg->GetWidth();
int srcHeight=psrcImg->GetHeight();

// Construct a Graphics object based on the image.
//Graphics imgGraphics(psrcImg);
int nSaveWidth=nWidth;
int nSaveHeight = nHeight;
Bitmap *pBitmap = ::new Bitmap(nSaveWidth,nSaveHeight);
Graphics bmpGraphics(pBitmap);
//bmpGraphics.DrawImage(psrcImg,0,0,srcWidth,srcHeight);
bmpGraphics.DrawImage(psrcImg,RectF(0,0,nSaveWidth,nSaveHeight),0,0,srcWidth,srcHeight,UnitPixel);
delete psrcImg;
bmpGraphics.ReleaseHDC(NULL);

// Save the altered image.
LPWSTR lpExt=PathFindExtensionW(lpSrcFile);
LPWSTR lpExtDst = NULL;

lpDstExt++;

LPWSTR lpEncoder;
switch(*lpDstExt)
{
case L'J':
case L'j':
lpEncoder=L"image/jpeg";
lpExtDst = L".jpeg";
break;
case L'P':
case L'p':
lpEncoder=L"image/png";
lpExtDst = L".png";
break;
case L'B':
case L'b':
lpEncoder=L"image/bmp";
lpExtDst = L".bmp";
break;
case L'G':
case L'g':
lpEncoder=L"image/gif";
lpExtDst = L".gif";
break;
case L't':
case L'T':
lpEncoder=L"image/tiff";
lpExtDst = L".tiff";
break;
default:
lpEncoder=L"image/jpeg";
lpExtDst = L".jpeg";
}

CStringW strPicPath(lpSrcFile);
CStringW strExt(lpExt);
int nLen = strPicPath.Find(strExt);
if(nLen>0)
strPicPath=strPicPath.Left(nLen);
strPicPath += lpExtDst;

CLSID imgClsid;
GetImageCLSID(lpEncoder,&imgClsid);
Status statusSave = pBitmap->Save(strPicPath,&imgClsid,NULL);

DeleteFileW(lpSrcFile);

//lpSrcFile = strPicPath.GetBuffer();
//strPicPath.ReleaseBuffer();

strDstFile = CStringW(strPicPath);
::delete pBitmap;

return (statusSave == Ok )?1:0;
}

BOOL GetImageCLSID(const WCHAR* format, CLSID* pCLSID)
{
UINT num = 0;
UINT size = 0;

ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0){
return FALSE;
}
pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
if(pImageCodecInfo == NULL)
return FALSE;
GetImageEncoders(num, size, pImageCodecInfo);

// Find for the support of format for image in the windows
for(UINT i = 0; i < num; ++i)
{
//MimeType: Depiction for the program image
if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
{
*pCLSID = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return TRUE;
}
}
free(pImageCodecInfo);
return FALSE;
}


gdiplus将一种图片格式转换成另外一个指定图片格式,且可设置图片大小
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: