1. 程式人生 > >CxImage透明圖片合成和文字疊加

CxImage透明圖片合成和文字疊加

1         CxImage的作用

CxImage將幾張圖片合成一張圖片,在一張底圖上新增圖片,有些圖片是有透明效果的,實現圖片之間的透明重疊效果。另外還可以新增文字。

(1)背景圖片mymix.png

 

(2)角標圖片,左下角為透明的,black.png和snap.png

(3)字圖片1MixImage1.jpgMixImage2.jpg

(4)合成後圖片效果圖

現在的目標就是將(2),(3)中的有透明的圖片和不透明的圖片合成到背景圖片(1)上,實現圖片(4)中的效果。

1         CxImage合成例項

CxImage imageMix;//合成後的圖片

CxImage imagesy1;//子圖片1

CxImage imagesy2;//字圖片2

CxImage imagesysnapfalg1;//透明圖片1

CxImage imagesyblackflag2;//透明圖片2

CxImage::CXTEXTINFO  BlackName, BlackSex, UserCardId, CompareSimilarity, SnapPicTime, CaseDescrip;//圖片疊加文字

imagesy1.load("MixImage1.jpg", CXIMAGE_FORMAT_JPG);//載入子圖片1

imagesy2. Load ("MixImage2.jpg", CXIMAGE_FORMAT_JPG);//載入字圖片2

imagesy1.Resample(340, 340, 0, &imagesy1);//設定子圖片大小

imagesy2.Resample(340, 340, 0, &imagesy2);    

//載入背景圖片                  

if (imageMix.Load(_T("mymix.png"), CXIMAGE_FORMAT_JPG))

{     //將兩張子圖片合成到背景圖片上

       imageMix.MixFrom(imagesy2, 35, 398);//圖片相對左下角,文字相對左上角

imageMix.MixFrom(imagesy1, 35, 28);//加入第一張子圖片

//將帶有一半透明的圖片合成到背景上

imagesysnapfalg1.Load(_T("snap.png"),CXIMAGE_FORMAT_JPG);

       imagesysnapfalg1.Resample(78,78,0,&imagesysnapfalg1);

       imagesysnapfalg1.SetTransIndex(255);//設定圖片的透明度0不透明,255完全透明

       imageMix.MixFrom(imagesysnapfalg1,297,398+261);

//將帶有一半透明的圖片合成到背景上

       imagesyblackflag2.Load(_T("black.png"),CXIMAGE_FORMAT_JPG);

       imagesyblackflag2.Resample(78,78,0,&imagesyblackflag2);

       imagesyblackflag2.SetTransIndex(255);

       imageMix.MixFrom(imagesyblackflag2,297,290);

       //初始化疊加文字結構體

imageMix.InitTextInfo(&BlackName);

       imageMix.InitTextInfo(&BlackSex);

       imageMix.InitTextInfo(&UserCardId);

       imageMix.InitTextInfo(&CompareSimilarity);

       imageMix.InitTextInfo(&SnapPicTime);

       imageMix.InitTextInfo(&CaseDescrip);

//設定文字的屬性

_stprintf(BlackName.lfont.lfFaceName, _T("微軟雅黑"));//字型

BlackName.lfont.lfCharSet = GB2312_CHARSET;//字符集

BlackName.lfont.lfHeight = 40;//高度

BlackName.fcolor = RGB(255, 255, 255);//顏色

BlackName.opaque = 0;

_stprintf(BlackSex.lfont.lfFaceName, _T("微軟雅黑"));

BlackSex.lfont.lfCharSet = GB2312_CHARSET;

BlackSex.lfont.lfHeight = 40;

BlackSex.fcolor = RGB(255, 255, 255);

BlackSex.opaque = 0;

 

_stprintf(UserCardId.lfont.lfFaceName, _T("微軟雅黑"));

UserCardId.lfont.lfCharSet = GB2312_CHARSET;

UserCardId.lfont.lfHeight = 40;

UserCardId.fcolor = RGB(255, 255, 255);

UserCardId.opaque = 0;

 

_stprintf(CompareSimilarity.lfont.lfFaceName, _T("微軟雅黑"));

CompareSimilarity.lfont.lfCharSet = GB2312_CHARSET;

CompareSimilarity.lfont.lfHeight = 40;

CompareSimilarity.fcolor = RGB(255, 255, 255);

CompareSimilarity.opaque = 0;

 

_stprintf(SnapPicTime.lfont.lfFaceName, _T("微軟雅黑"));

SnapPicTime.lfont.lfCharSet = GB2312_CHARSET;

SnapPicTime.lfont.lfHeight = 40;

SnapPicTime.fcolor = RGB(255, 255, 255);

SnapPicTime.opaque = 0;

 

_stprintf(CaseDescrip.lfont.lfFaceName, _T("微軟雅黑"));

CaseDescrip.lfont.lfCharSet = GB2312_CHARSET;

CaseDescrip.lfont.lfHeight = 40;

CaseDescrip.fcolor = RGB(255, 255, 255);

CaseDescrip.opaque = 0; //背景

//從結構體中輸入文字的值

float similarity = sFasData.fSimilarity * 100;

sprintf_s(UserCardId.text, "%s", sFasData.strBlackCerNo.c_str());

sprintf_s(SnapPicTime.text, "%s", sFasData.strSnapTime.c_str());

sprintf_s(CompareSimilarity.text, "%0.*f", 2, similarity);

sprintf_s(BlackName.text,"%s",sFasData.strBlackName.c_str());

 

std::string strCaseDescrip = "未知";

//開始將文字疊加到圖片

imageMix.DrawStringEx(0, 710, 138, &SnapPicTime);

imageMix.DrawStringEx(0, 679, 198, &CompareSimilarity);

imageMix.DrawStringEx(0, 519, 518, &BlackName);

imageMix.DrawStringEx(0, 509, 574, &BlackSex);

imageMix.DrawStringEx(0, 690, 638, &UserCardId);

}