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将一种图片格式转换成另外一个指定图片格式,且可设置图片大小
{
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将一种图片格式转换成另外一个指定图片格式,且可设置图片大小
相关文章推荐
- 【初面实习】爱奇艺测试实习生面试总结
- 10027---JavaScript--Number,String对象
- 【LEETCODE】20-Valid Parentheses
- (数据挖掘-入门-6)十折交叉验证和K近邻
- (数据挖掘-入门-5)基于内容的协同过滤与分类
- Android studio使用中遇到的坑——错误提示没有了(不出现了)
- 用border做纯CSS的三角形
- 归并排序
- git处理时的问题
- php数组函数-array_rand()
- 第十六周--验证算法(基数排序)
- OpenGL边用边学------2 经典照相机模型
- Oracle12c新特性之基本操作
- 运行时的“VFY: unable to resolve static field”错误
- 第7周项目4 队列数组
- autoencoder
- 窗口重叠,WS_CLIPCHILDREN和WS_CLIPSIBLINGS
- rpmbuild线上实战
- 第十六周项目(1):验证算法2——希尔排序
- 2743: [HEOI2012]采花