合并兩張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")
二。bmp圖片合并 BOOL CombinePic(const WCHAR *format, const CString &strDst, const CString &strPic1, \ const CString &strPic2) { BOOL bCombine = false; int nRet = 0; CLSID clsid; nRet = GetEncoderClsid(format,&clsid); if (nRet>=0) { USES_CONVERSION; Bitmap bmp1(A2W(strPic1)); Bitmap bmp2(A2W(strPic2)); int nWidth = 0, nHeight = 0; nWidth = bmp1.GetWidth(); //假設兩圖片大小同 nHeight = bmp1.GetHeight(); Bitmap bmpCombine(2*nWidth,nHeight); //高不變,寬*2,水平合并 Graphics * pG = NULL; pG = Graphics::FromImage(&bmpCombine); if (pG!=NULL) { pG->DrawImage(&bmp1,0,0); pG->DrawImage(&bmp2,nWidth,0); bmpCombine.Save(A2W(strDst),&clsid,NULL); } } return bCombine; } 例子: CombinePic(L"image/bmp","12.bmp","1.bmp","2.bmp");
有了上面的功能,其他的就沒問題了
|