将bmp图片转化作jpg图片
将bmp图片转化为jpg图片
刚刚才开始看,还没有找到什么资料,目前需要知道的几个问题有:
1.Jpg图片文件的格式是什么样的,如有哪些属性,分别对应的存储位置等
2.把bmp图片转化为jpg图片的算法
不过貌似更简单的一个方法是找到Win32接口直接将bmp转化为jpg,mfc接口的不行。
------解决方案--------------------
使用开源CXimage库,关于CXimage库使用方法网络上很多,google一下,http://blog.****.net/byxdaz/archive/2005/11/29/539221.aspx
CxImage m_cxImage;
cxImage.Load(bmpfilename,CXIMAGE_FORMAT_BMP);
cxImage.Save(jpgfilename,CXIMAGE_FORMAT_JPG);
------解决方案--------------------
GDI+可以完成的
合并两张jpg图片为一张jpg图片,思路是先把两张图片jpg图片都转化成bmp图片,然后把两张bmp图片合并成一张bmp图片,然后是把这张bmp图片转化为jpg图片。
一。jpg,bmp互相转化
/*********************************
format:bmp转为jpg, format为image/jpeg,jpg转为bmp,format为image/bmp
strDst为最终转化结果的图片路径
strSrc为原来图片的路径
**********************************/
BOOL ConvertPic(const WCHAR *format, const CString &strDst, const CString &strSrc)
{
BOOL bConvert = false;
CLSID clsid;
int nRet = 0;
nRet = GetEncoderClsid(format,&clsid); //得到CLSID
USES_CONVERSION;
if (nRet>=0)
{
Image image(A2W(strSrc));
image.Save(A2W(strDst),&clsid,NULL);
bConvert = true;
}
return bConvert;
}
其中GetEncoderClsid函数如下:
/*****************************************************
返回值为-1表示失败,其他为成功
******************************************************/
int GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
{
int nRet = -1;
ImageCodecInfo * pCodecInfo = NULL;
UINT nNum = 0,nSize = 0;
GetImageEncodersSize(&nNum,&nSize);
if (nSize<0)
{
return nRet;
}
pCodecInfo = new ImageCodecInfo[nSize];
if (pCodecInfo==NULL)
{
return nRet;
}
GetImageEncoders(nNum,nSize,pCodecInfo);
for (UINT i=0; i<nNum; i++)
{
if (wcscmp(pCodecInfo[i].MimeType,format)==0)
{
*pClsid = pCodecInfo[i].Clsid;
nRet = i;
delete[] pCodecInfo;
return nRet;
}
else
{
continue;
}
}
delete[] pCodecInfo;
return nRet;
}
bmp转化为jpg
ConvertPic(L"image/jpeg","c:\\1.jpg","c:\\1.bmp")
jpg转化为bmp
ConvertPic(L"image/bmp","c:\\1.bmp","c:\\1.jpg")
刚刚才开始看,还没有找到什么资料,目前需要知道的几个问题有:
1.Jpg图片文件的格式是什么样的,如有哪些属性,分别对应的存储位置等
2.把bmp图片转化为jpg图片的算法
不过貌似更简单的一个方法是找到Win32接口直接将bmp转化为jpg,mfc接口的不行。
------解决方案--------------------
使用开源CXimage库,关于CXimage库使用方法网络上很多,google一下,http://blog.****.net/byxdaz/archive/2005/11/29/539221.aspx
CxImage m_cxImage;
cxImage.Load(bmpfilename,CXIMAGE_FORMAT_BMP);
cxImage.Save(jpgfilename,CXIMAGE_FORMAT_JPG);
------解决方案--------------------
GDI+可以完成的
合并两张jpg图片为一张jpg图片,思路是先把两张图片jpg图片都转化成bmp图片,然后把两张bmp图片合并成一张bmp图片,然后是把这张bmp图片转化为jpg图片。
一。jpg,bmp互相转化
/*********************************
format:bmp转为jpg, format为image/jpeg,jpg转为bmp,format为image/bmp
strDst为最终转化结果的图片路径
strSrc为原来图片的路径
**********************************/
BOOL ConvertPic(const WCHAR *format, const CString &strDst, const CString &strSrc)
{
BOOL bConvert = false;
CLSID clsid;
int nRet = 0;
nRet = GetEncoderClsid(format,&clsid); //得到CLSID
USES_CONVERSION;
if (nRet>=0)
{
Image image(A2W(strSrc));
image.Save(A2W(strDst),&clsid,NULL);
bConvert = true;
}
return bConvert;
}
其中GetEncoderClsid函数如下:
/*****************************************************
返回值为-1表示失败,其他为成功
******************************************************/
int GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
{
int nRet = -1;
ImageCodecInfo * pCodecInfo = NULL;
UINT nNum = 0,nSize = 0;
GetImageEncodersSize(&nNum,&nSize);
if (nSize<0)
{
return nRet;
}
pCodecInfo = new ImageCodecInfo[nSize];
if (pCodecInfo==NULL)
{
return nRet;
}
GetImageEncoders(nNum,nSize,pCodecInfo);
for (UINT i=0; i<nNum; i++)
{
if (wcscmp(pCodecInfo[i].MimeType,format)==0)
{
*pClsid = pCodecInfo[i].Clsid;
nRet = i;
delete[] pCodecInfo;
return nRet;
}
else
{
continue;
}
}
delete[] pCodecInfo;
return nRet;
}
bmp转化为jpg
ConvertPic(L"image/jpeg","c:\\1.jpg","c:\\1.bmp")
jpg转化为bmp
ConvertPic(L"image/bmp","c:\\1.bmp","c:\\1.jpg")