C语言gdi显示图片,VC中使用Gdi+合并jpg图片

合并两张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

{

if (wcscmp(pCodecInfo.MimeType,format)==0)

{

*pClsid = pCodecInfo.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,NUL L);

}

}

return bCombine;

}

例子:

CombinePic(L”image/bmp”,”12.bmp”,”1.bmp”,”2.bmp”);

有了上面的功能,其他的就没问题了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值