图像拼接及保存

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;
}
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;
}

例子:
CombinePic(L"image/bmp", "12.bmp ", "1.bmp ", "2.bmp ");

 

 

用CLSID的成员可以直接获取5种格式图片的CLSID

GetEncoderClsid(L"image/png", &encoderClsid);

GetEncoderClsid(L"image/bmp", &encoderClsid);

GetEncoderClsid(L"image/gif", &encoderClsid);

GetEncoderClsid(L"image/jpeg", &encoderClsid);

GetEncoderClsid(L"image/tiff", &encoderClsid);


 

posted on 2011-08-24 13:35  carekee  阅读(767)  评论(0)    收藏  举报