DlgRenderings.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  1. // DlgRenderings.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Renderings.h"
  5. #include "DlgRenderings.h"
  6. #include <Shlwapi.h>
  7. #include "IMGCommon.h"
  8. ThreadSection CDlgRenderings::m_cs;
  9. CRect CDlgRenderings::m_rcWndClient;
  10. vector<STImageAlpha> CDlgRenderings::m_vtPngAlphaInfo;
  11. unsigned __stdcall LoadJPGThumbNail(LPVOID lpParam);
  12. unsigned __stdcall LoadPNGThumbNail(LPVOID lpParam);
  13. // CDlgRenderings 对话框
  14. IMPLEMENT_DYNAMIC(CDlgRenderings, CDialog)
  15. CDlgRenderings::CDlgRenderings(CWnd* pParent /*=NULL*/)
  16. : CDialog(CDlgRenderings::IDD, pParent)
  17. {
  18. m_pbmp = NULL;
  19. m_pPNGImage = NULL;
  20. m_pImagebak = NULL;
  21. m_pJPGImage = NULL;
  22. //}}AFX_DATA_INIT
  23. m_nSelectPNGPosition = -1;
  24. m_nSelectJPGPosition = -1;
  25. m_fscale = 1.0;
  26. m_bTerminate = false;
  27. m_bRunning = false;
  28. m_hThread = NULL;
  29. m_bTerminate2 = false;
  30. m_bRunning2 = false;
  31. m_hThread2 = NULL;
  32. m_bLoadEffectPNGImg = 1;
  33. m_bLoadPhotoJPGImg = 1;
  34. m_bMousePress = FALSE;
  35. m_nCurAlphaIndex = 0;
  36. }
  37. CDlgRenderings::~CDlgRenderings()
  38. {
  39. }
  40. void CDlgRenderings::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. DDX_Control(pDX, LIST_PNG, m_ListPNG);
  44. DDX_Control(pDX, LIST_JPG, m_ListJPG);
  45. }
  46. BEGIN_MESSAGE_MAP(CDlgRenderings, CDialog)
  47. ON_WM_PAINT()
  48. ON_WM_ERASEBKGND()
  49. ON_NOTIFY(NM_CLICK, LIST_PNG, &CDlgRenderings::OnNMClickList1)
  50. ON_NOTIFY(NM_CLICK, LIST_JPG, &CDlgRenderings::OnNMClickList2)
  51. ON_WM_DESTROY()
  52. ON_BN_CLICKED(Btn_ADD, &CDlgRenderings::OnBnClickedAdd)
  53. ON_BN_CLICKED(Btn_Delete, &CDlgRenderings::OnBnClickedDelete)
  54. ON_WM_LBUTTONUP()
  55. ON_WM_LBUTTONDOWN()
  56. ON_WM_MOUSEMOVE()
  57. ON_WM_MOUSEWHEEL()
  58. END_MESSAGE_MAP()
  59. // CDlgRenderings 消息处理程序
  60. BOOL CDlgRenderings::OnInitDialog()
  61. {
  62. CDialog::OnInitDialog();
  63. // TODO: 在此添加额外的初始化
  64. m_pPhotopatharray = &g_AryCustomerImage;
  65. if (m_pPhotopatharray->GetSize() == 0)
  66. {
  67. AfxMessageBox(_T("没有加载到任何相片"));
  68. }
  69. if (PathFileExists(g_strPathOfPNG))
  70. {
  71. TString str;
  72. int nIndex = 0;
  73. STR_VEC vtfiles;
  74. filehelpImpl ffhelp;
  75. ffhelp.getfiles_findout_subfolder(g_strPathOfPNG, "*.png", &vtfiles);
  76. for (STR_VEC::iterator it = vtfiles.begin(); it != vtfiles.end(); it++)
  77. {
  78. str = it->c_str();
  79. nIndex = it->find_last_of(_T('\\'));
  80. if ( nIndex != TString::npos )
  81. {
  82. // 是否是s,或m文件;
  83. if (str.substr(nIndex + 1, -1).at(0) == _T('s') || str.substr(nIndex + 1, -1).at(0) == _T('m'))
  84. {
  85. str.erase(nIndex + 1, 1);
  86. if ( PathFileExists(str.c_str()) )
  87. continue;
  88. }
  89. m_AryEffectPNGImg.Add(it->c_str());
  90. }
  91. }
  92. }
  93. size_t nSize = m_AryEffectPNGImg.GetSize();
  94. if (nSize == 0)
  95. {
  96. AfxMessageBox(_T("您还没有安装效果图!"), MB_ICONINFORMATION);
  97. ::CreateDirectory(g_strPathOfPNG, NULL);
  98. }
  99. // TODO: Add extra initialization here
  100. // m_rcscreen:屏幕尺寸;
  101. #ifdef _DEBUG
  102. ::SetWindowPos(m_hWnd, NULL, 0, 0, g_nScreenWidth, g_nScreenHeight, SWP_SHOWWINDOW);
  103. #else
  104. ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, g_nScreenWidth, g_nScreenHeight, SWP_SHOWWINDOW);
  105. #endif
  106. m_rcscreen = CRect(0, 0, g_nScreenWidth, g_nScreenHeight);
  107. GetClientRect(m_rcWndClient);
  108. HANDLE hThread = CreateThread(NULL, 0, Thread_GetAllPNGInfo, NULL, 0, NULL);
  109. CloseHandle(hThread);
  110. // rc:临时区域;
  111. // rc2:客户区域大小,用于放置PNG效果图;
  112. // rc3:按钮区域;
  113. CRect rc, rc2, rc3;
  114. GetClientRect(rc2);
  115. GetDlgItem(Btn_Delete)->GetWindowRect(rc3);
  116. ScreenToClient(rc3);
  117. rc = rc2;
  118. rc.right = 110;
  119. rc.top = rc3.bottom + rc3.top;
  120. m_ListPNG.MoveWindow(rc);
  121. rc = rc2;
  122. rc.left = rc.right - 110;
  123. m_ListJPG.MoveWindow(rc);
  124. // m_pbmp:最终显示PNG效果图的Bimap对象(PNG+客人相片 合二为一的结果);
  125. m_rcBackground = rc2;
  126. m_rcBackground.left += 110;
  127. m_rcBackground.right -= 110;
  128. m_pbmp = new Gdiplus::Bitmap(m_rcBackground.Width(), m_rcBackground.Height(), PixelFormat24bppRGB);
  129. m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);
  130. m_ListPNG.SetImageList(&m_ImageListThumb, LVSIL_NORMAL);
  131. m_ImageListThumb2.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);
  132. m_ListJPG.SetImageList(&m_ImageListThumb2, LVSIL_NORMAL);
  133. ::SetWindowLong(m_ListJPG.GetSafeHwnd(), GWL_STYLE, WS_CHILD | WS_VISIBLE | WS_HSCROLL);
  134. ::SetWindowLong(m_ListPNG.GetSafeHwnd(), GWL_STYLE, WS_CHILD | WS_VISIBLE | WS_HSCROLL);
  135. if (m_bRunning)
  136. return 0;
  137. CStringArray *pArray = &m_AryEffectPNGImg;
  138. m_vPNGFile.RemoveAll();
  139. for (int i = 0; i < pArray->GetSize(); i++)
  140. {
  141. CString filePath = pArray->ElementAt(i);
  142. filePath.MakeLower();
  143. m_vPNGFile.Add(filePath);
  144. }
  145. TerminateThread();
  146. m_hThread = (HANDLE)_beginthreadex(NULL, 0, LoadJPGThumbNail, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &m_dwThreadID);
  147. m_bRunning = true;
  148. if (m_bRunning2)
  149. return 0;
  150. pArray = NULL;
  151. pArray = m_pPhotopatharray;
  152. m_vJPGFile.RemoveAll();
  153. for (int i = 0; i < pArray->GetSize(); i++)
  154. {
  155. CString filePath = pArray->ElementAt(i);
  156. filePath.MakeLower();
  157. m_vJPGFile.Add(filePath);
  158. }
  159. TerminateThread2();
  160. m_hThread2 = (HANDLE)_beginthreadex(NULL, 0, LoadPNGThumbNail, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &m_dwThreadID2);
  161. m_bRunning2 = true;
  162. DWORD dwSize = m_AryEffectPNGImg.GetSize();
  163. //LoadPNGImageSuper(0);
  164. return TRUE; // return TRUE unless you set the focus to a control
  165. // 异常: OCX 属性页应返回 FALSE
  166. }
  167. // 如果向对话框添加最小化按钮,则需要下面的代码
  168. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  169. // 这将由框架自动完成。
  170. void CDlgRenderings::OnPaint()
  171. {
  172. CPaintDC dc(this); // 用于绘制的设备上下文
  173. try
  174. {
  175. if (m_pbmp)
  176. {
  177. Graphics graphics(dc.GetSafeHdc());
  178. graphics.DrawImage(m_pbmp, m_rcBackground.left, m_rcBackground.top);
  179. }
  180. }
  181. catch (...)
  182. {
  183. }
  184. //CDialog::OnPaint();
  185. }
  186. BOOL CDlgRenderings::OnEraseBkgnd(CDC* pDC)
  187. {
  188. // TODO: 在此添加消息处理程序代码和/或调用默认值
  189. static BOOL bErase = FALSE;
  190. if (!bErase)
  191. {//第一次时,要让窗口完全绘制出来,不然会有其他窗口背景;
  192. bErase = TRUE;
  193. return CDialog::OnEraseBkgnd(pDC);
  194. }
  195. return TRUE;
  196. }
  197. void CDlgRenderings::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult) // 单击PNG列表图标;
  198. {
  199. LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  200. POSITION pos = m_ListPNG.GetFirstSelectedItemPosition();
  201. if (pos == NULL)
  202. return;
  203. m_nCurAlphaIndex = 0;
  204. m_nSelectJPGPosition = -1;
  205. m_bLoadEffectPNGImg = LoadPNGImageSuper(m_ListPNG.GetNextSelectedItem(pos));
  206. // 清空Offsset图片内容;
  207. for (vector<STOffSetInfo>::iterator it = m_vtOffSetInfo.begin(); it != m_vtOffSetInfo.end(); it++ )
  208. {
  209. if (it->pImage)
  210. {
  211. it->fScale = 1.0;
  212. it->nXOffSet = 0;
  213. it->nYOffSet = 0;
  214. delete it->pImage;
  215. it->pImage = NULL;
  216. }
  217. }
  218. *pResult = 0;
  219. }
  220. void CDlgRenderings::OnNMClickList2(NMHDR *pNMHDR, LRESULT *pResult) // 单击JPG列表图标;
  221. {
  222. LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  223. POSITION pos = m_ListJPG.GetFirstSelectedItemPosition();
  224. if (pos == NULL)
  225. return;
  226. static int nLastPosition = -1;
  227. nLastPosition = m_nSelectJPGPosition;
  228. m_nSelectJPGPosition = m_ListJPG.GetNextSelectedItem(pos);
  229. static int nLastAlphaPosition = -1;
  230. if (nLastAlphaPosition == m_nCurAlphaIndex && nLastPosition == m_nSelectJPGPosition)
  231. {// 前后两次选中透明区域相同,且前后两次选中的JPG也相同,不执行任何操作,直接返回;
  232. return;
  233. }
  234. nLastAlphaPosition = m_nCurAlphaIndex;
  235. nLastPosition = m_nSelectJPGPosition;
  236. m_bLoadPhotoJPGImg = TRUE;
  237. Image *pJpgImage = NULL;
  238. CString strText = m_pPhotopatharray->ElementAt(m_nSelectJPGPosition);
  239. IMGCommon::LoadImgFromFile((Image**)&pJpgImage, strText);
  240. if (pJpgImage)
  241. {// 加载JPG成功,则要清除上一次透明区域的JPG图片对象;
  242. STOffSetInfo *pOffSet = NULL;
  243. if ( m_vtOffSetInfo.size() == 0 )
  244. {
  245. STOffSetInfo OffSet;
  246. OffSet.nIndexOfAlpha = m_nCurAlphaIndex;
  247. OffSet.nXOffSet = 0;
  248. OffSet.nYOffSet = 0;
  249. OffSet.pImage = pJpgImage;
  250. m_vtOffSetInfo.push_back(OffSet);
  251. LoadJPGImageSuper(&m_vtOffSetInfo[0]);
  252. return;
  253. }
  254. for (vector<STOffSetInfo>::iterator it = m_vtOffSetInfo.begin(); it != m_vtOffSetInfo.end(); it++)
  255. {
  256. if (it->nIndexOfAlpha == m_nCurAlphaIndex)
  257. {
  258. it->nXOffSet = 0;
  259. it->nYOffSet = 0;
  260. if (it->pImage)
  261. {
  262. delete it->pImage;
  263. it->pImage = NULL;
  264. }
  265. it->fScale = 1.0;
  266. it->pImage = pJpgImage;
  267. pOffSet = (STOffSetInfo *)(&*it);
  268. }
  269. }
  270. if (pOffSet)
  271. LoadJPGImageSuper(pOffSet);
  272. }
  273. *pResult = 0;
  274. }
  275. #ifdef __UN_USE__
  276. void CDlgRenderings::MakeEffect()
  277. {
  278. // Jeff.m_AryEffectPNGImg,客人原片\效果图\ 目录下的所有PNG图片;;
  279. if (m_AryEffectPNGImg.GetSize() == 0)
  280. return;
  281. // m_pbmp:最终显示PNG效果图的Bimap对象(PNG+客人相片 合二为一的结果);
  282. Graphics graph(m_pbmp);
  283. // 将对象区域全部以透明色填充;
  284. graph.Clear(Color(255, 255, 255, 255));
  285. if (m_bLoadPhotoJPGImg)
  286. {
  287. if (m_pJPGImage)
  288. delete m_pJPGImage;
  289. m_pJPGImage = NULL;
  290. }
  291. if (m_bLoadEffectPNGImg)
  292. {
  293. if (m_pPNGImage)
  294. delete m_pPNGImage;
  295. m_pPNGImage = NULL;
  296. if (m_pImagebak)
  297. delete m_pImagebak;
  298. m_pImagebak = NULL;
  299. // m_pPNGImage为显示的PNG效果图Bitmap对象;LoadImageFromBuf指定加载哪张PNG相片作为效果图显示;
  300. IMGCommon::LoadImgFromFile((Image**)&m_pPNGImage, m_AryEffectPNGImg.ElementAt(m_nSelectPNGPosition));
  301. if (m_pPhotopatharray->GetSize())
  302. {
  303. // 如果Jpg存在,计算png的透明区域;
  304. GetRgn(m_pPNGImage);
  305. }
  306. m_pImagebak = m_pPNGImage->Clone(0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), PixelFormat32bppARGB);
  307. }
  308. int width, height;
  309. width = m_pPNGImage->GetWidth();
  310. height = m_pPNGImage->GetHeight();
  311. if (width == 0 || height == 0)return;
  312. InitRc(m_pPNGImage);
  313. CPoint pt1, pt2, pt3;
  314. int leng1, leng2, leng3;
  315. CPoint curpt;
  316. int leng;
  317. Graphics graph2(m_pPNGImage);
  318. // m_pPhotopatharray:客人的相片;
  319. if (m_pPhotopatharray->GetSize())
  320. {
  321. if (m_bLoadPhotoJPGImg)
  322. {
  323. CString strText = m_pPhotopatharray->ElementAt(m_nSelectJPGPosition);
  324. IMGCommon::LoadImgFromFile((Image**)&m_pJPGImage, strText);
  325. }
  326. if (m_pJPGImage == NULL)
  327. {
  328. return;
  329. }
  330. int i;
  331. for (int nAlphaIndex = 0; nAlphaIndex < RCARRARCOUNT; nAlphaIndex++)
  332. {
  333. leng1 = leng2 = leng3 = 10000000;
  334. for (i = 0; i < m_rcarray[nAlphaIndex].GetSize(); i += 2)
  335. {
  336. curpt.x = m_rcarray[nAlphaIndex].ElementAt(i);
  337. curpt.y = m_rcarray[nAlphaIndex].ElementAt(i + 1);
  338. leng = GetLengFromPt(CPoint(0, 0), curpt); // 计算出斜边平方值,也就是两点间的距离;
  339. if (leng < leng1)
  340. {
  341. leng1 = leng;
  342. pt1 = curpt;
  343. }
  344. leng = GetLengFromPt(CPoint(m_pPNGImage->GetWidth(), 0), curpt);
  345. if (leng < leng2)
  346. {
  347. leng2 = leng;
  348. pt2 = curpt;
  349. }
  350. leng = GetLengFromPt(CPoint(0, m_pPNGImage->GetHeight()), curpt);
  351. if (leng < leng3)
  352. {
  353. leng3 = leng;
  354. pt3 = curpt;
  355. }
  356. }
  357. if (m_rcarray[nAlphaIndex].GetSize())
  358. {
  359. // m_tempscale为缩放值,点坐标要缩放;
  360. pt1.x *= m_tempscale;
  361. pt1.y *= m_tempscale;
  362. pt2.x *= m_tempscale;
  363. pt2.y *= m_tempscale;
  364. pt3.x *= m_tempscale;
  365. pt3.y *= m_tempscale;
  366. // 缩放后为实际点值;
  367. Point destinationPoints[] = {
  368. Point(pt1.x, pt1.y), // destination for upper-left point of original
  369. Point(pt2.x, pt2.y), // destination for upper-right point of original
  370. Point(pt3.x, pt3.y) // destination for lower-left point of original
  371. };
  372. // graph2.DrawImage(m_pJPGImage, destinationPoints, 3);
  373. // 计算出每个透明区域的宽、高;
  374. int lengx = GetLengFromPt2(pt1, pt2);
  375. int lengy = GetLengFromPt2(pt1, pt3);
  376. CRect rc(0, 0, m_pJPGImage->GetWidth(), m_pJPGImage->GetHeight());
  377. RectFitDes(lengx, lengy, rc);
  378. graph2.DrawImage(
  379. m_pJPGImage,
  380. destinationPoints,
  381. 3,
  382. rc.left,
  383. rc.top,
  384. rc.Width(),
  385. rc.Height(),
  386. UnitPixel,
  387. NULL,
  388. NULL,
  389. NULL);
  390. }
  391. }
  392. graph2.DrawImage(m_pImagebak, 0, 0);
  393. }
  394. Rect destinationRect(m_rc.left, m_rc.top, m_rc.Width(), m_rc.Height());
  395. graph.DrawImage(m_pPNGImage, destinationRect, 0, 0, width, height, UnitPixel);
  396. CRect rc;
  397. GetClientRect(rc);
  398. rc.left += 110;
  399. rc.right -= 110;
  400. InvalidateRect(rc);
  401. m_bLoadEffectPNGImg = FALSE;
  402. m_bLoadPhotoJPGImg = FALSE;
  403. }
  404. /************************************************************************/
  405. /* 函数:GetRgn[4/9/2017 Jeff];
  406. /* 描述:获取指定PNG图像的透明区域;
  407. /* 参数:;
  408. /* [IN] pBmpImage2:PNG图像对象;
  409. /* [OUT] :;
  410. /* [IN/OUT] :;
  411. /* 返回:void;
  412. /* 注意:RGN和RECT的区别在于,RECT的矩形,RGN是任意形状;
  413. /* 示例:;
  414. /*
  415. /* 修改:;
  416. /* 日期:;
  417. /* 内容:;
  418. /************************************************************************/
  419. void CDlgRenderings::GetRgn(Bitmap *pBmpImage2)
  420. {
  421. m_tempscale = 1.0;
  422. if (pBmpImage2 == NULL)return;
  423. // pBmpImage
  424. Bitmap *pBmpImage = NULL;
  425. BOOL bdelete = 0;
  426. // .如果PNG效果相片长宽大于800
  427. // Bitemap::GetWidth() 获取此图像的图像高度,以像素为单位;
  428. //if(pBmpImage2->GetWidth()> m_rcBackground.Width() || pBmpImage2->GetHeight()> m_rcBackground.Height())
  429. if (pBmpImage2->GetWidth() > 800 || pBmpImage2->GetHeight() > 800)
  430. {
  431. bdelete = 1;
  432. //CRect rc(0,0,m_rcBackground.Width(),m_rcBackground.Height());
  433. CRect rc(0, 0, 800, 800);//使用800的比例更快,但误差更大;
  434. // GetWidth()的长度与CRect的长度是不一样的单位值,需要转换适合图片的比例;(一个是图片的像素尺寸,一个DC的大小尺寸)
  435. RectFitDes(pBmpImage2->GetWidth(), pBmpImage2->GetHeight(), rc);
  436. // 在适合图片长宽比例的显示设备中重新装载图片;
  437. pBmpImage = new Bitmap(rc.Width(), rc.Height(), PixelFormat32bppARGB);
  438. Graphics graphic(pBmpImage);
  439. graphic.Clear(Color(0, 0, 0, 0));
  440. graphic.DrawImage(pBmpImage2, 0, 0, pBmpImage->GetWidth(), pBmpImage->GetHeight());
  441. m_tempscale = (float)pBmpImage2->GetWidth() / (float)pBmpImage->GetWidth();
  442. }
  443. else
  444. pBmpImage = pBmpImage2;
  445. // BitmapData这个类用于创建包含的信息来描述像素数据的位图图像的矩形区域的对象。
  446. BitmapData bitmapData;
  447. UINT* pixels;
  448. Rect rc(0, 0, pBmpImage->GetWidth(), pBmpImage->GetHeight());
  449. // 指定格式读取pBmpImage里的图片像素到bitmapData中;
  450. pBmpImage->LockBits(
  451. &rc, // 数据被加载到的区域;
  452. ImageLockModeRead, // 锁定方式:读或写;
  453. PixelFormat32bppARGB, // 数据保存的格式;
  454. &bitmapData); // 存储数据的对象;
  455. // Write to the temporary buffer provided by LockBits. RGB
  456. // Scan0:一个空指针到第一扫描线的开始
  457. pixels = (UINT*)bitmapData.Scan0;
  458. // Stride是指图像每一行需要占用的字节数。根据BMP格式的标准,Stride一定要是4的倍数。
  459. // Stride:
  460. // 一个INT值从位图区到下一个扫描线指定的字节数。这个值是负的自底向上的位图图像。
  461. DWORD wids = bitmapData.Stride / 4; // 除以4,刚好为Color::alpha \red \green \blue 四色(只有PNG才有alpha色);
  462. // 像素(x,y)的颜色;
  463. Color color;
  464. // 像素(x,y)四周8个方向的像素颜色:
  465. // (x-1,y-1);
  466. // (x-0,y-1);
  467. // (x+1,y-1);
  468. // (x-1,y-0);
  469. // (x+1,y-0);
  470. // (x-1,y+1);
  471. // (x-0,y+1);
  472. // (x+1,y+1);
  473. Color color1, color2, color3, color4, color5, color6, color7, color8;
  474. // m_rcarray数组,用于记录PNG图片里的alpha色小于200的像素点坐标;
  475. BYTE R, G, B;
  476. for (int nAlphaIndex = 0; nAlphaIndex < RCARRARCOUNT; nAlphaIndex++)
  477. m_rcarray[nAlphaIndex].RemoveAll(); // Jeff.清空以前的;
  478. for (INT row = 0; row < rc.Height; ++row)
  479. {
  480. for (INT col = 0; col < rc.Width - 1; ++col)
  481. {
  482. // 将PNG图片当前像素(col,row)点的颜色值填充到color对象;
  483. color.SetValue(pixels[row * wids + col]);
  484. // Color::GetA() 方法获取此Color对象的alpha分量(返回这个颜色的alpha分量)。
  485. BYTE byalpha = color.GetA();
  486. if (byalpha < 200)
  487. {
  488. CArray<int, int>*pArray = NULL;
  489. for (int nAlphaIndex = 0; nAlphaIndex < RCARRARCOUNT; nAlphaIndex++)
  490. {
  491. if (m_rcarray[nAlphaIndex].GetSize() && pArray == NULL)
  492. {
  493. // 从区域nAlphaIndex的第一个点开始,判断当前像素点(col,row)是否属于该区域的连续同色;
  494. double x = m_rcarray[nAlphaIndex].ElementAt(0);
  495. double y = m_rcarray[nAlphaIndex].ElementAt(1);
  496. // x0,y0作为原点;
  497. int x0 = (int)x;
  498. int y0 = (int)y;
  499. // col、row为当前点;
  500. // x、y为对照点;
  501. double dtx = col - x;
  502. double dty = row - y;
  503. // 求得tan或cot值;
  504. if (abs(dtx) > abs(dty))
  505. {// 左右象限(3、4、7、8);
  506. if (dty)
  507. {// 在原点下方(4、7);
  508. dtx /= abs(dty); // cot;
  509. dty /= abs(dty); // 1;
  510. }
  511. else
  512. {// 在原点上方(3、8);
  513. if (dtx > 0)
  514. {//在原点右方(3);
  515. dtx = 1;
  516. }
  517. else if (dtx < 0)
  518. {//在原点左方(8);
  519. dtx = -1;
  520. }
  521. }
  522. }
  523. else
  524. {// 上下象限(1、2、5、6);
  525. if (dtx)
  526. {//在原点右方(2、5);
  527. dty /= abs(dtx); // tan;
  528. dtx /= abs(dtx); // 1;
  529. }
  530. else
  531. {//在原点左方(1、6);
  532. if (dty > 0)
  533. {//在原点下方(6);
  534. dty = 1;
  535. }
  536. else if (dtx < 0)
  537. {//在原点上方(1);
  538. dty = -1;
  539. }
  540. }
  541. }
  542. // 此时dtx或dty已为tan或cot值,当tan或cot的值大于3时,角度是72或18度左右;
  543. if (dtx > 3)
  544. {
  545. double step = dtx / 3.0;
  546. dtx /= step;
  547. dty /= step;
  548. }
  549. if (dty > 3)
  550. {
  551. double step = dty / 3.0;
  552. dtx /= step;
  553. dty /= step;
  554. }
  555. BOOL bFind = FALSE;
  556. while (1)
  557. {
  558. x += dtx;
  559. y += dty;
  560. if (x >= (rc.Width - 1) || x < 1)break;
  561. if (y >= (rc.Height - 1) || y < 1)break;
  562. if (col > x0)
  563. {//在原点右侧;
  564. if (x < x0 || x > col)
  565. {// 新点不在原点和像素点x坐标之间;
  566. break;
  567. }
  568. }
  569. if (col < x0)
  570. {//在原点左侧;
  571. if (x > x0 || x < col)
  572. {// 新点不在原点和像素点x坐标之间;
  573. break;
  574. }
  575. }
  576. if (row > y0)
  577. {//在原点下方;
  578. if (y < y0 || y > row)
  579. {// 新点不在原点和像素点y坐标之间;
  580. break;
  581. }
  582. }
  583. if (row < y0)
  584. {//在原点上方;
  585. if (y > y0 || y < row)
  586. {// 新点不在原点和像素点y坐标之间;
  587. break;
  588. }
  589. }
  590. color.SetValue(pixels[(int)y * wids + (int)x]);
  591. if (color.GetA() == 255)
  592. {
  593. color1.SetValue(pixels[(int)(y - 1) * wids + (int)(x - 1)]);
  594. color2.SetValue(pixels[(int)(y - 1) * wids + (int)(x - 0)]);
  595. color3.SetValue(pixels[(int)(y - 1) * wids + (int)(x + 1)]);
  596. color4.SetValue(pixels[(int)(y - 0) * wids + (int)(x - 1)]);
  597. color5.SetValue(pixels[(int)(y - 0) * wids + (int)(x + 1)]);
  598. color6.SetValue(pixels[(int)(y + 1) * wids + (int)(x - 1)]);
  599. color7.SetValue(pixels[(int)(y + 1) * wids + (int)(x - 0)]);
  600. color8.SetValue(pixels[(int)(y + 1) * wids + (int)(x + 1)]);
  601. if (color1.GetA() == 255 && color2.GetA() == 255 && color3.GetA() == 255 && color4.GetA() == 255 && color5.GetA() == 255 && color6.GetA() == 255 && color7.GetA() == 255 && color8.GetA() == 255)
  602. {// 当前像素4周8个方向的像素点颜色与当前像素一致,表明为连续区域;
  603. bFind = 1;
  604. break;
  605. }
  606. }
  607. }
  608. if (!bFind)
  609. {// 不是连续点,则开一个数组保存这块新的区域;
  610. pArray = &(m_rcarray[nAlphaIndex]);
  611. }
  612. }
  613. }
  614. if (pArray == NULL)
  615. {// 未找到相邻同色的点;
  616. BOOL bcontinue = 1;
  617. for (int yy = 0; yy < RCARRARCOUNT; yy++)
  618. {
  619. if (m_rcarray[yy].GetSize() == 0)
  620. {
  621. pArray = &(m_rcarray[yy]);
  622. bcontinue = 0;
  623. break;
  624. }
  625. }
  626. if (bcontinue)
  627. continue;
  628. }
  629. pArray->Add(col); // x坐标;
  630. pArray->Add(row); // y坐标;
  631. }
  632. }
  633. }
  634. pBmpImage->UnlockBits(&bitmapData);
  635. if (bdelete)
  636. ::delete pBmpImage;
  637. }
  638. #endif
  639. BOOL CDlgRenderings::TerminateThread()
  640. {
  641. if (!m_bRunning)
  642. return TRUE;
  643. m_bTerminate = true;
  644. for (; ; )
  645. {
  646. if (::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0)
  647. break;
  648. MSG msg;
  649. while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
  650. {
  651. if (!AfxGetApp()->PumpMessage())
  652. break;
  653. }
  654. }
  655. ::CloseHandle(m_hThread);
  656. return TRUE;
  657. }
  658. BOOL CDlgRenderings::TerminateThread2()
  659. {
  660. if (!m_bRunning2)
  661. return TRUE;
  662. m_bTerminate2 = true;
  663. for (; ; )
  664. {
  665. if (::WaitForSingleObject(m_hThread2, 0) == WAIT_OBJECT_0)
  666. break;
  667. MSG msg;
  668. while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
  669. {
  670. if (!AfxGetApp()->PumpMessage())
  671. break;
  672. }
  673. }
  674. ::CloseHandle(m_hThread2);
  675. return TRUE;
  676. }
  677. #ifdef __UN_USE__
  678. void CDlgRenderings::InitRc(Image *img)
  679. {
  680. try
  681. {
  682. if (img == NULL)return;
  683. CRect rc;
  684. GetClientRect(rc);
  685. rc.right -= 220;
  686. m_rc = rc;
  687. int width, height;
  688. width = img->GetWidth();
  689. height = img->GetHeight();
  690. //////////
  691. float fscale = (float)width / (float)height;
  692. float rcscale = ((float)m_rc.Width()) / ((float)m_rc.Height());
  693. int rcwid = m_rc.Width();
  694. int rchei = m_rc.Height();
  695. int dt = 0;
  696. if (rcscale < fscale)
  697. {
  698. dt = (rchei - rcwid / fscale) / 2;
  699. m_rc.top += dt;
  700. m_rc.bottom -= dt;
  701. }
  702. else
  703. {
  704. dt = (rcwid - rchei*fscale) / 2;
  705. m_rc.left += dt;
  706. m_rc.right -= dt;
  707. }
  708. m_rcbak = m_rc;
  709. }
  710. catch (...)
  711. {
  712. }
  713. }
  714. #endif
  715. unsigned __stdcall LoadJPGThumbNail(LPVOID lpParam)
  716. {
  717. try
  718. {
  719. CDlgRenderings *pDlg = (CDlgRenderings*)lpParam;
  720. CListCtrl *ListCtrl = &pDlg->m_ListJPG;
  721. CImageList* pImgList = &pDlg->m_ImageListThumb2;
  722. // reset our image list
  723. for (int i = 0; i < pImgList->GetImageCount(); i++)
  724. pImgList->Remove(i);
  725. // remove all items from list view
  726. ListCtrl->DeleteAllItems();
  727. if (pDlg->m_vJPGFile.GetSize() == 0) // 没有效果图时,线程不处理其他;
  728. {
  729. return 0;
  730. }
  731. pImgList->SetImageCount(pDlg->m_vJPGFile.GetSize());
  732. TCHAR path[MAX_PATH];
  733. vector<CString>::iterator iter;
  734. // Set redraw to FALSE to avoid flickering during adding new items
  735. ListCtrl->SetRedraw(FALSE);
  736. int nIndex = 0;
  737. CString str, spath;
  738. //for(iter=pDlg->m_vFileName2.begin(); iter!=pDlg->m_vFileName2.end() && pDlg->m_bTerminate2!=true; iter++, nIndex++)
  739. for (INT i = 0; i < pDlg->m_vJPGFile.GetSize(); i++, nIndex++)
  740. {
  741. //str=*iter;
  742. str = pDlg->m_vJPGFile.ElementAt(i);
  743. str = str.Right(str.GetLength() - str.ReverseFind('\\') - 1);
  744. str = str.Left(str.Find("."));
  745. ListCtrl->InsertItem(nIndex, str, nIndex);
  746. }
  747. ListCtrl->SetRedraw(TRUE);
  748. ListCtrl->Invalidate();
  749. const float fRatio = (float)THUMBNAIL_HEIGHT / THUMBNAIL_WIDTH;
  750. int XDest, YDest, nDestWidth, nDestHeight;
  751. nIndex = 0;
  752. SolidBrush whitebrush(Color(255, 255, 255, 255));
  753. //for(iter=pDlg->m_vFileName2.begin(); iter!=pDlg->m_vFileName2.end() && pDlg->m_bTerminate2!=true; iter++, nIndex++)
  754. for (INT i = 0; i < pDlg->m_vJPGFile.GetSize(); i++, nIndex++)
  755. {
  756. // Load Image File
  757. Image *image = NULL;
  758. Bitmap *pBmp = NULL;
  759. str = pDlg->m_vJPGFile.ElementAt(i);
  760. spath = str.Left(str.ReverseFind('\\') + 1);
  761. spath += "s";
  762. spath += str.Right(str.GetLength() - str.ReverseFind('\\') - 1);
  763. if (PathFileExists(spath))
  764. IMGCommon::LoadImgFromFile(&image, spath);
  765. else
  766. IMGCommon::LoadImgFromFile(&image, str);
  767. if (image == NULL)
  768. {
  769. continue;
  770. }
  771. int orientation = IMGCommon::GetOrientation(image);
  772. if (orientation == 8)
  773. image->RotateFlip(Rotate270FlipNone);
  774. else if (orientation == 6)
  775. image->RotateFlip(Rotate90FlipNone);
  776. if (image->GetWidth() == 0)
  777. continue;
  778. // Calculate Rect to fit to canvas
  779. const float fImgRatio = (float)image->GetHeight() / image->GetWidth();
  780. if (fImgRatio > fRatio)
  781. {
  782. nDestWidth = THUMBNAIL_HEIGHT / fImgRatio;
  783. XDest = (THUMBNAIL_WIDTH - nDestWidth) / 2;
  784. YDest = 0;
  785. nDestHeight = THUMBNAIL_HEIGHT;
  786. }
  787. else
  788. {
  789. XDest = 0;
  790. nDestWidth = THUMBNAIL_WIDTH;
  791. nDestHeight = THUMBNAIL_WIDTH*fImgRatio;
  792. YDest = (THUMBNAIL_HEIGHT - nDestHeight) / 2;
  793. }
  794. pBmp = new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB);
  795. Graphics graph2(pBmp);
  796. graph2.Clear(Color(255, 192, 192, 192));
  797. graph2.FillRectangle(&whitebrush, 1, 1, THUMBNAIL_WIDTH - 2, THUMBNAIL_HEIGHT - 2);
  798. Graphics graph(pBmp);
  799. Rect desRect(XDest, YDest, nDestWidth, nDestHeight);
  800. // Draw Image
  801. graph.DrawImage(image, desRect, 0, 0, image->GetWidth(), image->GetHeight(), UnitPixel);
  802. delete image;
  803. // Attach to Bitmap and Replace image in CImageList
  804. CBitmap bitmap;
  805. HBITMAP hbmp;
  806. pBmp->GetHBITMAP(Color(255, 255, 255, 255), &hbmp);
  807. bitmap.Attach(hbmp);
  808. pImgList->Replace(nIndex, &bitmap, NULL);
  809. delete pBmp;
  810. // Redraw only a current item for removing flickering and fast speed.
  811. ListCtrl->RedrawItems(nIndex, nIndex);
  812. // Release used DC and Object
  813. }
  814. ListCtrl->Invalidate();
  815. pDlg->m_bRunning2 = false;
  816. pDlg->m_bTerminate2 = false;
  817. _endthreadex(0);
  818. return 0;
  819. }
  820. catch (...)
  821. {
  822. }
  823. return 0;
  824. }
  825. unsigned __stdcall LoadPNGThumbNail(LPVOID lpParam)
  826. {
  827. try
  828. {
  829. CDlgRenderings *pDlg = (CDlgRenderings*)lpParam;
  830. CListCtrl *ListCtrl = &pDlg->m_ListPNG;
  831. CImageList* pImgList = &pDlg->m_ImageListThumb;
  832. // reset our image list
  833. for (int i = 0; i < pImgList->GetImageCount(); i++)
  834. pImgList->Remove(i);
  835. // remove all items from list view
  836. ListCtrl->DeleteAllItems();
  837. if (pDlg->m_vJPGFile.GetSize() == 0 || pDlg->m_vPNGFile.GetSize() == 0) // 没有相片,线程不处理其他;
  838. {
  839. return 0;
  840. }
  841. pImgList->SetImageCount(pDlg->m_vPNGFile.GetSize());
  842. TCHAR path[MAX_PATH];
  843. //vector<CString>::iterator iter;
  844. // Set redraw to FALSE to avoid flickering during adding new items
  845. ListCtrl->SetRedraw(FALSE);
  846. int nIndex = 0;
  847. CString str, spath;
  848. for (INT i = 0; i < pDlg->m_vPNGFile.GetSize(); i++, nIndex++)
  849. {
  850. str = pDlg->m_vPNGFile.ElementAt(i);
  851. str = str.Right(str.GetLength() - str.ReverseFind('\\') - 1);
  852. str = str.Left(str.Find("."));
  853. ListCtrl->InsertItem(nIndex, str, nIndex);
  854. }
  855. ListCtrl->SetRedraw(TRUE);
  856. ListCtrl->Invalidate();
  857. const float fRatio = (float)THUMBNAIL_HEIGHT / THUMBNAIL_WIDTH;
  858. int XDest, YDest, nDestWidth, nDestHeight;
  859. nIndex = 0;
  860. SolidBrush whitebrush(Color(255, 255, 255, 255));
  861. for (INT i = 0; i < pDlg->m_vPNGFile.GetSize(); i++, nIndex++)
  862. {
  863. // Load Image File
  864. Image *image = NULL;
  865. Bitmap *pBmp = NULL;
  866. //str=*iter;
  867. str = pDlg->m_vPNGFile.ElementAt(i);
  868. spath = str.Left(str.ReverseFind('\\') + 1);
  869. spath += "s";
  870. spath += str.Right(str.GetLength() - str.ReverseFind('\\') - 1);
  871. spath.Replace("png", "jpg");
  872. if (PathFileExists(spath))
  873. {
  874. IMGCommon::LoadImgFromFile(&image, spath);
  875. }
  876. else if (PathFileExists(str))
  877. {
  878. IMGCommon::LoadImgFromFile(&image, str);
  879. }
  880. else
  881. {
  882. AfxMessageBox("没有相片");
  883. continue;
  884. }
  885. int orientation = IMGCommon::GetOrientation(image);
  886. if (orientation == 8)
  887. image->RotateFlip(Rotate270FlipNone);
  888. else if (orientation == 6)
  889. image->RotateFlip(Rotate90FlipNone);
  890. if (image->GetWidth() == 0)
  891. continue;
  892. // Calculate Rect to fit to canvas
  893. const float fImgRatio = (float)image->GetHeight() / image->GetWidth();
  894. if (fImgRatio > fRatio)
  895. {
  896. nDestWidth = THUMBNAIL_HEIGHT / fImgRatio;
  897. XDest = (THUMBNAIL_WIDTH - nDestWidth) / 2;
  898. YDest = 0;
  899. nDestHeight = THUMBNAIL_HEIGHT;
  900. }
  901. else
  902. {
  903. XDest = 0;
  904. nDestWidth = THUMBNAIL_WIDTH;
  905. nDestHeight = THUMBNAIL_WIDTH*fImgRatio;
  906. YDest = (THUMBNAIL_HEIGHT - nDestHeight) / 2;
  907. }
  908. pBmp = new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB);
  909. Graphics graph2(pBmp);
  910. graph2.Clear(Color(255, 192, 192, 192));
  911. graph2.FillRectangle(&whitebrush, 1, 1, THUMBNAIL_WIDTH - 2, THUMBNAIL_HEIGHT - 2);
  912. Graphics graph(pBmp);
  913. Rect desRect(XDest, YDest, nDestWidth, nDestHeight);
  914. // Draw Image
  915. graph.DrawImage(image, desRect, 0, 0, image->GetWidth(), image->GetHeight(), UnitPixel);
  916. delete image;
  917. // Attach to Bitmap and Replace image in CImageList
  918. CBitmap bitmap;
  919. HBITMAP hbmp;
  920. pBmp->GetHBITMAP(Color(255, 255, 255, 255), &hbmp);
  921. bitmap.Attach(hbmp);
  922. pImgList->Replace(nIndex, &bitmap, NULL);
  923. delete pBmp;
  924. // Redraw only a current item for removing flickering and fast speed.
  925. ListCtrl->RedrawItems(nIndex, nIndex);
  926. // Release used DC and Object
  927. }
  928. ListCtrl->Invalidate();
  929. pDlg->m_bRunning = false;
  930. pDlg->m_bTerminate = false;
  931. pDlg->m_nSelectPNGPosition = 0;
  932. pDlg->LoadPNGImageSuper(0);
  933. _endthreadex(0);
  934. return 0;
  935. }
  936. catch (...)
  937. {
  938. }
  939. return 0;
  940. }
  941. void CDlgRenderings::OnDestroy()
  942. {
  943. TerminateThread2();
  944. TerminateThread();
  945. if (m_pbmp)
  946. delete m_pbmp;
  947. m_pbmp = NULL;
  948. if (m_pPNGImage)
  949. delete m_pPNGImage;
  950. m_pPNGImage = NULL;
  951. if (m_pImagebak)
  952. delete m_pImagebak;
  953. m_pImagebak = NULL;
  954. if (m_pJPGImage)
  955. delete m_pJPGImage;
  956. m_pJPGImage = NULL;
  957. CDialog::OnDestroy();
  958. // TODO: 在此处添加消息处理程序代码
  959. }
  960. void CDlgRenderings::OnBnClickedAdd()
  961. {
  962. AfxMessageBox(_T("制作方法:\r\n1.图片格式为透明PNG格式.\r\n2.显示客照的位置为透明."), MB_ICONINFORMATION);
  963. CFileDialog fdlg(true, NULL, _T("openfile"), OFN_ALLOWMULTISELECT, _T("png文件(*.png)|*.png||"));
  964. TCHAR szBuffer[60000] = { 0 };
  965. fdlg.m_ofn.lpstrFile = szBuffer;
  966. fdlg.m_ofn.nMaxFile = 60000;
  967. if (fdlg.DoModal() != IDOK)
  968. {
  969. return;
  970. }
  971. POSITION pos = fdlg.GetStartPosition();
  972. CStringArray array;
  973. while (pos != NULL)
  974. {
  975. array.Add(fdlg.GetNextPathName(pos));
  976. }
  977. if (array.GetSize() == 0)
  978. return;
  979. for ( int i = 0; i < array.GetSize(); i++ )
  980. {
  981. AddNewImageItem(array.ElementAt(i));
  982. }
  983. }
  984. void CDlgRenderings::OnBnClickedDelete()
  985. {
  986. // TODO: 在此添加控件通知处理程序代码
  987. POSITION pos;
  988. pos = m_ListPNG.GetFirstSelectedItemPosition();
  989. if (pos == NULL)
  990. {
  991. return;
  992. }
  993. int selpos = m_ListPNG.GetNextSelectedItem(pos);
  994. if (AfxMessageBox(_T("确认删除吗?"), MB_YESNO | MB_ICONINFORMATION) != IDYES)
  995. return;
  996. CString str = m_AryEffectPNGImg.ElementAt(selpos);
  997. for (vector<STImageAlpha>::iterator it = m_vtPngAlphaInfo.begin(); it != m_vtPngAlphaInfo.end(); it++ )
  998. {
  999. if (str.CompareNoCase(it->PNGFileName) == 0)
  1000. {
  1001. m_vtPngAlphaInfo.erase(it);
  1002. break;
  1003. }
  1004. }
  1005. CString spath = str.Left(str.ReverseFind('\\') + 1);
  1006. spath += "s";
  1007. spath += str.Right(str.GetLength() - str.ReverseFind('\\') - 1);
  1008. spath.Replace("png", "jpg");
  1009. ::DeleteFile(str);
  1010. ::DeleteFile(spath);
  1011. m_AryEffectPNGImg.RemoveAt(selpos);
  1012. m_ListPNG.DeleteItem(selpos);
  1013. m_nSelectPNGPosition = 0;
  1014. m_nSelectJPGPosition = 0;
  1015. }
  1016. void CDlgRenderings::OnLButtonUp(UINT nFlags, CPoint point)
  1017. {
  1018. // 鼠标放开;
  1019. m_bMousePress = FALSE;
  1020. // 判断鼠标坐标点是否在透明区域内,在返回透明区域索引;
  1021. m_nCurAlphaIndex = IsPointInAlphaRect(point);
  1022. if (m_nCurAlphaIndex >= 0)
  1023. {// 鼠标点在透明区域内;
  1024. // 遍历JPG透明集合,查看该透明区域是否有加载JPG;
  1025. BOOL bExist = FALSE;
  1026. for (vector<STOffSetInfo>::iterator it = m_vtOffSetInfo.begin(); it != m_vtOffSetInfo.end(); it++)
  1027. {
  1028. if (it->nIndexOfAlpha == m_nCurAlphaIndex)
  1029. {
  1030. bExist = TRUE;
  1031. break;
  1032. }
  1033. }
  1034. if (!bExist)
  1035. {// 没有加载,新建元素;
  1036. STOffSetInfo OffSet;
  1037. OffSet.nIndexOfAlpha = m_nCurAlphaIndex;
  1038. OffSet.nXOffSet = 0;
  1039. OffSet.nYOffSet = 0;
  1040. m_vtOffSetInfo.push_back(OffSet);
  1041. }
  1042. }
  1043. // 为上一次选中的透明区域画白色边框;
  1044. DrawAlphaEdge(m_ptLastRectangle, true);
  1045. // 为当前选中的透明区域画红色边框
  1046. DrawAlphaEdge(point);
  1047. CDialog::OnLButtonUp(nFlags, point);
  1048. }
  1049. void CDlgRenderings::OnLButtonDown(UINT nFlags, CPoint point)
  1050. {
  1051. // 鼠标放开;
  1052. m_bMousePress = TRUE;
  1053. // 判断点是否在透明区域内;
  1054. CDialog::OnLButtonDown(nFlags, point);
  1055. }
  1056. void CDlgRenderings::OnMouseMove(UINT nFlags, CPoint point)
  1057. {
  1058. CRect rcList1;
  1059. CRect rcList2;
  1060. m_ListPNG.GetWindowRect(&rcList1);
  1061. m_ListJPG.GetWindowRect(&rcList2);
  1062. if ( !rcList1.PtInRect(point) || !rcList2.PtInRect(point))
  1063. {// 如果鼠标点坐标不在list1和list2,设置客户区域获取焦点;
  1064. SetFocus();
  1065. }
  1066. CDialog::OnMouseMove(nFlags, point);
  1067. }
  1068. #ifdef __UN_USE__
  1069. BOOL CDlgRenderings::LoadPNGImage(IN INT nIndex)
  1070. {
  1071. if (m_AryEffectPNGImg.GetSize() == 0 || (nIndex != 0 && (nIndex == m_nSelectPNGPosition)))
  1072. return FALSE;
  1073. m_nSelectPNGPosition = nIndex;
  1074. // m_pbmp:最终显示PNG效果图的Bimap对象(PNG+客人相片 合二为一的结果);
  1075. Graphics graph(m_pbmp);
  1076. // 将对象区域全部不透明白色填充;
  1077. graph.Clear(Color(255, 255, 255, 255));
  1078. #ifndef _DEBUG // Debug模式下删除会出错;
  1079. if (m_pPNGImage)
  1080. delete m_pPNGImage;
  1081. m_pPNGImage = NULL;
  1082. if (m_pImagebak)
  1083. delete m_pImagebak;
  1084. m_pImagebak = NULL;
  1085. #endif
  1086. // m_pPNGImage为显示的PNG效果图Bitmap对象;LoadImageFromBuf指定加载哪张PNG相片作为效果图显示;
  1087. //LoadImageFromBuf((Image**)&m_pPNGImage, m_AryEffectPNGImg.ElementAt(nIndex));
  1088. IMGCommon::LoadImgFromFile((Image**)&m_pPNGImage, m_AryEffectPNGImg.ElementAt(nIndex));
  1089. BOOL bResult = IMGCommon::ImgThumbnail(m_AryEffectPNGImg.ElementAt(nIndex), SET_PIX(m_rcPNGImage.Width(), m_rcPNGImage.Height()), "D:\\003.png", 80, TRUE);
  1090. INT nWidth = m_pPNGImage->GetWidth();
  1091. INT nHeight = m_pPNGImage->GetHeight();
  1092. if (nWidth == 0 || nHeight == 0)
  1093. return FALSE;
  1094. GetRgn(m_pPNGImage);
  1095. m_pImagebak = m_pPNGImage->Clone(0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), PixelFormat32bppARGB);
  1096. m_rcPNGImage = InitRect(m_pPNGImage);
  1097. Rect destinationRect(m_rcPNGImage.left, m_rcPNGImage.top, m_rcPNGImage.Width(), m_rcPNGImage.Height());
  1098. graph.DrawImage(m_pPNGImage, destinationRect, 0, 0, nWidth, nHeight, UnitPixel);
  1099. InvalidateRect(m_rcBackground);
  1100. return TRUE;
  1101. }
  1102. #endif
  1103. BOOL CDlgRenderings::LoadPNGImageSuper(IN INT nIndex)
  1104. {
  1105. if (m_AryEffectPNGImg.GetSize() == 0 || (nIndex != 0 && (nIndex == m_nSelectPNGPosition)))
  1106. return FALSE;
  1107. m_nSelectPNGPosition = nIndex;
  1108. // m_pbmp:最终显示PNG效果图的Bimap对象(PNG+客人相片 合二为一的结果);
  1109. Graphics graph(m_pbmp);
  1110. // 将对象区域全部不透明白色填充;
  1111. graph.Clear(Color(255, 255, 255, 255));
  1112. // 是否加载过;
  1113. BOOL bLoaded = FALSE;
  1114. CString strPNGFile = m_AryEffectPNGImg.ElementAt(nIndex);
  1115. vector<STImageAlpha>::iterator it = m_vtPngAlphaInfo.begin();
  1116. for (; it != m_vtPngAlphaInfo.end(); it++)
  1117. {
  1118. if (strPNGFile.CompareNoCase(it->PNGFileName) == 0)
  1119. {
  1120. bLoaded = TRUE;
  1121. break;
  1122. }
  1123. }
  1124. //#ifndef _DEBUG // Debug模式下删除出错;
  1125. if (m_pPNGImage)
  1126. delete m_pPNGImage;
  1127. m_pPNGImage = NULL;
  1128. if (m_pImagebak)
  1129. delete m_pImagebak;
  1130. m_pImagebak = NULL;
  1131. //#endif
  1132. IMGCommon::LoadImgFromFile((Image**)&m_pPNGImage, m_AryEffectPNGImg.ElementAt(nIndex));
  1133. if (!bLoaded)
  1134. {
  1135. AutoThreadSection cs(&m_cs);
  1136. STImageAlpha tagPngAlpha;
  1137. tagPngAlpha.PNGFileName = strPNGFile;
  1138. GetPNGAlphaRectInfo(m_pPNGImage, tagPngAlpha);
  1139. m_vtPngAlphaInfo.push_back(tagPngAlpha);
  1140. it = m_vtPngAlphaInfo.end() - 1;
  1141. }
  1142. INT nWidth = m_pPNGImage->GetWidth();
  1143. INT nHeight = m_pPNGImage->GetHeight();
  1144. if (nWidth == 0 || nHeight == 0)
  1145. return FALSE;
  1146. m_pImagebak = m_pPNGImage->Clone(0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), PixelFormat32bppARGB);
  1147. Rect destinationRect(it->rcZoom.left, it->rcZoom.top, it->rcZoom.Width(), it->rcZoom.Height());
  1148. graph.DrawImage(m_pPNGImage, destinationRect, 0, 0, nWidth, nHeight, UnitPixel);
  1149. InvalidateRect(m_rcBackground);
  1150. return TRUE;
  1151. }
  1152. CRect CDlgRenderings::InitRect(IN Image* pImage)
  1153. {
  1154. CRect rcImage(0, 0, 0, 0);
  1155. if (pImage == NULL)
  1156. return rcImage;
  1157. CRect rcClient = m_rcWndClient;
  1158. rcClient.right -= 220;
  1159. rcImage = rcClient;
  1160. INT nWidth = pImage->GetWidth();
  1161. INT nHeight = pImage->GetHeight();
  1162. float fscale = (float)nWidth / (float)nHeight;
  1163. float rcscale = ((float)rcImage.Width()) / ((float)rcImage.Height());
  1164. int rcwid = rcImage.Width();
  1165. int rchei = rcImage.Height();
  1166. int dt = 0;
  1167. if (rcscale < fscale)
  1168. {
  1169. dt = (rchei - rcwid / fscale) / 2;
  1170. rcImage.top += dt;
  1171. rcImage.bottom -= dt;
  1172. }
  1173. else
  1174. {
  1175. dt = (rcwid - rchei*fscale) / 2;
  1176. rcImage.left += dt;
  1177. rcImage.right -= dt;
  1178. }
  1179. return rcImage;
  1180. }
  1181. #ifdef __UN_USE__
  1182. BOOL CDlgRenderings::LoadJPGImage(IN INT nAlphaIndex, IN Image* pJpgImage, IN INT nXOffset, IN INT nYOffset)
  1183. {
  1184. // 判断透明区域索引是否有效;
  1185. if (nAlphaIndex == -1 || m_rcarray[nAlphaIndex].GetSize() == 0)
  1186. return FALSE;
  1187. // JPG图像是否有效;
  1188. if (pJpgImage == NULL || pJpgImage->GetWidth() == 0 || pJpgImage->GetHeight() == 0)
  1189. return FALSE;
  1190. // PNG底图是否有效;
  1191. if (m_pPNGImage == NULL)
  1192. return FALSE;
  1193. Graphics graph(m_pbmp);
  1194. CPoint pt1, pt2, pt3;
  1195. int leng1, leng2, leng3;
  1196. CPoint curpt;
  1197. int leng;
  1198. Graphics graph2(m_pPNGImage);
  1199. int i;
  1200. leng1 = leng2 = leng3 = 10000000;
  1201. for (i = 0; i < m_rcarray[nAlphaIndex].GetSize(); i += 2)
  1202. {
  1203. curpt.x = m_rcarray[nAlphaIndex].ElementAt(i);
  1204. curpt.y = m_rcarray[nAlphaIndex].ElementAt(i + 1);
  1205. leng = GetLengFromPt(CPoint(0, 0), curpt); // 计算出斜边平方值,也就是两点间的距离;
  1206. if (leng < leng1)
  1207. {
  1208. leng1 = leng;
  1209. pt1 = curpt;
  1210. }
  1211. leng = GetLengFromPt(CPoint(m_pPNGImage->GetWidth(), 0), curpt);
  1212. if (leng < leng2)
  1213. {
  1214. leng2 = leng;
  1215. pt2 = curpt;
  1216. }
  1217. leng = GetLengFromPt(CPoint(0, m_pPNGImage->GetHeight()), curpt);
  1218. if (leng < leng3)
  1219. {
  1220. leng3 = leng;
  1221. pt3 = curpt;
  1222. }
  1223. }
  1224. // m_tempscale为缩放值,点坐标要缩放;
  1225. pt1.x *= m_tempscale;
  1226. pt1.y *= m_tempscale;
  1227. pt2.x *= m_tempscale;
  1228. pt2.y *= m_tempscale;
  1229. pt3.x *= m_tempscale;
  1230. pt3.y *= m_tempscale;
  1231. // 缩放后为实际点值;
  1232. Point destinationPoints[] = {
  1233. Point(pt1.x, pt1.y), // destination for upper-left point of original
  1234. Point(pt2.x, pt2.y), // destination for upper-right point of original
  1235. Point(pt3.x, pt3.y) }; // destination for lower-left point of original
  1236. // 计算出每个透明区域的宽、高;
  1237. int lengx = GetLengFromPt2(pt1, pt2);
  1238. int lengy = GetLengFromPt2(pt1, pt3);
  1239. CRect rc(0, 0, pJpgImage->GetWidth(), pJpgImage->GetHeight());
  1240. RectFitDes(lengx, lengy, rc);
  1241. #if 1
  1242. float fx = (float)lengx / rc.Width();
  1243. float fy = (float)lengy / rc.Height();
  1244. if (nXOffset + lengx / fx >= pJpgImage->GetWidth())
  1245. {// 是否要到右边缘;
  1246. nXOffset = pJpgImage->GetWidth() - lengx / fx;
  1247. }
  1248. if (nYOffset + lengy / fy >= pJpgImage->GetHeight())
  1249. {// 是否要到底边缘;
  1250. nYOffset = pJpgImage->GetHeight() - lengy / fy;
  1251. }
  1252. #endif
  1253. #if 0
  1254. graph2.DrawImage(
  1255. m_pJPGImage,
  1256. destinationPoints,
  1257. 3,
  1258. nXOffset,
  1259. nYOffset,
  1260. rc.Width(),
  1261. rc.Height(),
  1262. UnitPixel,
  1263. NULL,
  1264. NULL,
  1265. NULL);
  1266. WriteTextLog("xoffset = %d, yoffset = %d", nXOffset, nYOffset);
  1267. #else
  1268. // 使用这种模式,可以放大缩小图片;
  1269. RectF rf(pt1.x, pt1.y, lengx + 5, lengy + 5);
  1270. graph2.DrawImage(pJpgImage, rf, nXOffset, nYOffset, rc.Width(), rc.Height(), UnitPixel);
  1271. #endif
  1272. graph2.DrawImage(m_pImagebak, 0, 0);
  1273. Rect destinationRect(m_rcPNGImage.left, m_rcPNGImage.top, m_rcPNGImage.Width(), m_rcPNGImage.Height());
  1274. graph.DrawImage(m_pPNGImage, destinationRect, 0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), UnitPixel);
  1275. #if 0
  1276. // 刷新范围太大了,用户体验不好!;
  1277. InvalidateRect(m_rcBackground);
  1278. #else
  1279. // 局部区域刷新;
  1280. float fs = (float)m_rcPNGImage.Width() / m_pPNGImage->GetWidth();
  1281. pt1.x *= fs;
  1282. pt1.y *= fs;
  1283. pt2.x *= fs;
  1284. pt2.y *= fs;
  1285. pt3.x *= fs;
  1286. pt3.y *= fs;
  1287. float fs2 = (float)1;
  1288. pt1.Offset(m_rcPNGImage.left, m_rcPNGImage.top);
  1289. pt2.Offset(m_rcPNGImage.left, m_rcPNGImage.top);
  1290. pt3.Offset(m_rcPNGImage.left, m_rcPNGImage.top);
  1291. pt1.Offset(m_rcBackground.left, m_rcBackground.top);
  1292. pt2.Offset(m_rcBackground.left, m_rcBackground.top);
  1293. pt3.Offset(m_rcBackground.left, m_rcBackground.top);
  1294. InvalidateRect(CRect(pt1.x - 6, pt1.y - 6, pt2.x + 8, pt3.y + 8));
  1295. #endif
  1296. return TRUE;
  1297. }
  1298. #endif
  1299. BOOL CDlgRenderings::LoadJPGImageSuper(STOffSetInfo *pOffSetInfo)
  1300. {
  1301. // 判断透明区域索引是否有效;
  1302. if (pOffSetInfo->nIndexOfAlpha == -1)
  1303. return FALSE;
  1304. // JPG图像是否有效;
  1305. if (pOffSetInfo->pImage == NULL || pOffSetInfo->pImage->GetWidth() == 0 || pOffSetInfo->pImage->GetHeight() == 0)
  1306. return FALSE;
  1307. // PNG底图是否有效;
  1308. if (m_pPNGImage == NULL)
  1309. return FALSE;
  1310. // 获取出透明区域的三点;
  1311. CPoint ptOrigin, ptOriginX, ptOriginY;
  1312. CString strPNGFile = m_AryEffectPNGImg.ElementAt(m_nSelectPNGPosition);
  1313. STAlphaPt *pAlphaPt = NULL;
  1314. vector<STImageAlpha>::iterator it;
  1315. for (it = m_vtPngAlphaInfo.begin(); it != m_vtPngAlphaInfo.end(); it++)
  1316. {
  1317. if (strPNGFile.CompareNoCase(it->PNGFileName) == 0)
  1318. {
  1319. for (vector<STAlphaPt>::iterator iit = it->vtAlphaPt.begin(); iit != it->vtAlphaPt.end(); iit++)
  1320. {
  1321. if (pOffSetInfo->nIndexOfAlpha == iit->nAlphaIndex)
  1322. {
  1323. pAlphaPt = (STAlphaPt*)(&*iit);
  1324. ptOrigin = iit->ptOrigin;
  1325. ptOriginX = iit->ptOriginX;
  1326. ptOriginY = iit->ptOriginY;
  1327. break;
  1328. }
  1329. }
  1330. break;
  1331. }
  1332. }
  1333. if (pAlphaPt == NULL)
  1334. return FALSE;
  1335. // 将坐标系从800*800缩放回png原图坐标系;
  1336. ptOrigin.x *= it->fScale;
  1337. ptOrigin.y *= it->fScale;
  1338. ptOriginX.x *= it->fScale;
  1339. ptOriginX.y *= it->fScale;
  1340. ptOriginY.x *= it->fScale;
  1341. ptOriginY.y *= it->fScale;
  1342. // 获取透明区域的长宽;
  1343. INT nWidth = GetLengFromPt2(ptOrigin, ptOriginX);
  1344. INT nHeight = GetLengFromPt2(ptOrigin, ptOriginY);
  1345. CRect rcZoom(0, 0, pOffSetInfo->pImage->GetWidth(), pOffSetInfo->pImage->GetHeight());
  1346. RectFitDes(nWidth, nHeight, rcZoom);
  1347. // 判断移动的点是否到达边缘;
  1348. float fsx = (float)nWidth / rcZoom.Width();
  1349. float fsy = (float)nHeight / rcZoom.Height();
  1350. //if (nXOffset + nWidth / fsx >= pJpgImage->GetWidth())
  1351. {//若到边缘,重新指定;
  1352. pOffSetInfo->nXOffSet = (pOffSetInfo->pImage->GetWidth() - nWidth / fsx)/2;
  1353. }
  1354. //if (nYOffset + nHeight / fsy >= pJpgImage->GetHeight())
  1355. {//若到边缘,重新指定;
  1356. //pOffSetInfo->nYOffSet = (pOffSetInfo->pImage->GetHeight() - nHeight / fsy)/3;
  1357. }
  1358. Graphics graph(m_pbmp);
  1359. Graphics gra_png(m_pPNGImage);
  1360. // 将JPG画到PNG原图上指定透明区域;
  1361. RectF rfAlpha(ptOrigin.x - 5, ptOrigin.y - 5, nWidth + 10, nHeight + 10);
  1362. gra_png.DrawImage(pOffSetInfo->pImage, rfAlpha, pOffSetInfo->nXOffSet, pOffSetInfo->nYOffSet, rcZoom.Width(), rcZoom.Height(), UnitPixel);
  1363. gra_png.DrawImage(m_pImagebak, 0, 0);
  1364. // 再将png画到背景图上;
  1365. Rect rcPng(it->rcZoom.left, it->rcZoom.top, it->rcZoom.Width(), it->rcZoom.Height());
  1366. graph.DrawImage(m_pPNGImage, rcPng, 0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), UnitPixel);
  1367. // 刷新透明区域;
  1368. ptOrigin = AlphaPt2WndPt(pAlphaPt->ptOrigin, it->rcZoom, it->fScale);
  1369. ptOriginX = AlphaPt2WndPt(pAlphaPt->ptOriginX, it->rcZoom, it->fScale);
  1370. ptOriginY = AlphaPt2WndPt(pAlphaPt->ptOriginY, it->rcZoom, it->fScale);
  1371. InvalidateRect(CRect(ptOrigin.x - 6, ptOrigin.y - 6, ptOriginX.x + 8, ptOriginY.y + 8));
  1372. return TRUE;
  1373. }
  1374. BOOL CDlgRenderings::LoadJPGImageSuperEx(IN INT nAlphaIndex, IN Image * pJpgImage, IN float& fs, IN INT nXOffset, IN INT nYOffset)
  1375. {
  1376. // 判断透明区域索引是否有效;
  1377. if (nAlphaIndex == -1)
  1378. return FALSE;
  1379. // JPG图像是否有效;
  1380. if (pJpgImage == NULL || pJpgImage->GetWidth() == 0 || pJpgImage->GetHeight() == 0)
  1381. return FALSE;
  1382. // PNG底图是否有效;
  1383. if (m_pPNGImage == NULL)
  1384. return FALSE;
  1385. // 获取出透明区域的三点;
  1386. CPoint ptOrigin, ptOriginX, ptOriginY;
  1387. CString strPNGFile = m_AryEffectPNGImg.ElementAt(m_nSelectPNGPosition);
  1388. STAlphaPt *pAlphaPt = NULL;
  1389. vector<STImageAlpha>::iterator it;
  1390. for (it = m_vtPngAlphaInfo.begin(); it != m_vtPngAlphaInfo.end(); it++)
  1391. {
  1392. if (strPNGFile.CompareNoCase(it->PNGFileName) == 0)
  1393. {
  1394. for (vector<STAlphaPt>::iterator iit = it->vtAlphaPt.begin(); iit != it->vtAlphaPt.end(); iit++)
  1395. {
  1396. if (nAlphaIndex == iit->nAlphaIndex)
  1397. {
  1398. pAlphaPt = (STAlphaPt*)(&*iit);
  1399. ptOrigin = iit->ptOrigin;
  1400. ptOriginX = iit->ptOriginX;
  1401. ptOriginY = iit->ptOriginY;
  1402. break;
  1403. }
  1404. }
  1405. break;
  1406. }
  1407. }
  1408. if (pAlphaPt == NULL)
  1409. return FALSE;
  1410. // 将坐标系从800*800缩放回png原图坐标系;
  1411. ptOrigin.x *= it->fScale;
  1412. ptOrigin.y *= it->fScale;
  1413. ptOriginX.x *= it->fScale;
  1414. ptOriginX.y *= it->fScale;
  1415. ptOriginY.x *= it->fScale;
  1416. ptOriginY.y *= it->fScale;
  1417. // 获取透明区域的长宽;
  1418. INT nWidth = GetLengFromPt2(ptOrigin, ptOriginX);
  1419. INT nHeight = GetLengFromPt2(ptOrigin, ptOriginY);
  1420. CRect rcZoom(0, 0, pJpgImage->GetWidth(), pJpgImage->GetHeight());
  1421. RectFitDes(nWidth, nHeight, rcZoom);
  1422. // 判断移动的点是否到达边缘;
  1423. float fsx = (float)nWidth / rcZoom.Width();
  1424. float fsy = (float)nHeight / rcZoom.Height();
  1425. if (nXOffset + nWidth / fsx *fs >= pJpgImage->GetWidth())
  1426. {//若到边缘,重新指定;
  1427. nXOffset = pJpgImage->GetWidth() - nWidth / fsx *fs;
  1428. if (nXOffset < 0)
  1429. {
  1430. nXOffset = 0;
  1431. fs = 1.0;
  1432. }
  1433. }
  1434. if (nYOffset + nHeight / fsy *fs >= pJpgImage->GetHeight())
  1435. {//若到边缘,重新指定;
  1436. nYOffset = pJpgImage->GetHeight() - nHeight / fsy *fs;
  1437. if (nYOffset < 0)
  1438. {
  1439. nYOffset = 0;
  1440. fs = 1.0;
  1441. }
  1442. }
  1443. Graphics graph(m_pbmp);
  1444. Graphics gra_png(m_pPNGImage);
  1445. // 将JPG画到PNG原图上指定透明区域;
  1446. RectF rfAlpha(ptOrigin.x - 5, ptOrigin.y - 5, nWidth + 10, nHeight + 10);
  1447. gra_png.DrawImage(pJpgImage, rfAlpha, nXOffset, nYOffset, rcZoom.Width()*fs, rcZoom.Height()*fs, UnitPixel);
  1448. gra_png.DrawImage(m_pImagebak, 0, 0);
  1449. // 再将png画到背景图上;
  1450. Rect rcPng(it->rcZoom.left, it->rcZoom.top, it->rcZoom.Width(), it->rcZoom.Height());
  1451. graph.DrawImage(m_pPNGImage, rcPng, 0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), UnitPixel);
  1452. // 刷新透明区域;
  1453. ptOrigin = AlphaPt2WndPt(pAlphaPt->ptOrigin, it->rcZoom, it->fScale);
  1454. ptOriginX = AlphaPt2WndPt(pAlphaPt->ptOriginX, it->rcZoom, it->fScale);
  1455. ptOriginY = AlphaPt2WndPt(pAlphaPt->ptOriginY, it->rcZoom, it->fScale);
  1456. InvalidateRect(CRect(ptOrigin.x - 6, ptOrigin.y - 6, ptOriginX.x + 8, ptOriginY.y + 8));
  1457. return TRUE;
  1458. }
  1459. INT CDlgRenderings::IsPointInAlphaRect(IN CPoint point)
  1460. {
  1461. INT nAlphaIndex = -1;
  1462. if (m_nSelectPNGPosition == -1)
  1463. return -1;
  1464. CString strPNGfile = m_AryEffectPNGImg.ElementAt(m_nSelectPNGPosition);
  1465. for (vector<STImageAlpha>::iterator it = m_vtPngAlphaInfo.begin(); it != m_vtPngAlphaInfo.end(); it++)
  1466. {
  1467. if (strPNGfile.CompareNoCase(it->PNGFileName) == 0)
  1468. {
  1469. // 将窗口坐标系转成透明区域坐标系;
  1470. point = WndPt2AlphaPt(point, it->rcZoom, it->fScale);
  1471. for (vector<STAlphaPt>::iterator iit = it->vtAlphaPt.begin(); iit != it->vtAlphaPt.end(); iit++)
  1472. {
  1473. CRect rcAlpha(iit->ptOrigin.x, iit->ptOrigin.y, iit->ptOriginX.x, iit->ptOriginY.y);
  1474. if (rcAlpha.PtInRect(point))
  1475. {
  1476. nAlphaIndex = iit->nAlphaIndex;
  1477. break;
  1478. }
  1479. }
  1480. break;
  1481. }
  1482. }
  1483. return nAlphaIndex;
  1484. }
  1485. BOOL CDlgRenderings::PreTranslateMessage(MSG* pMsg)
  1486. {
  1487. if (pMsg->message == WM_KEYDOWN)
  1488. {
  1489. // 获取按下时的点;
  1490. CPoint pt;
  1491. GetCursorPos(&pt);
  1492. m_nCurAlphaIndex = IsPointInAlphaRect(pt);
  1493. if (m_vtOffSetInfo.size() == 0)
  1494. return FALSE;
  1495. // 找到透明区域;
  1496. vector<STOffSetInfo>::iterator it = m_vtOffSetInfo.begin();
  1497. for (; it != m_vtOffSetInfo.end(); it++)
  1498. {
  1499. if (it->nIndexOfAlpha == m_nCurAlphaIndex)
  1500. break;
  1501. }
  1502. // 未找到退出;
  1503. if (it == m_vtOffSetInfo.end() || it->nIndexOfAlpha == -1)
  1504. return FALSE;
  1505. switch (pMsg->wParam)
  1506. {
  1507. case VK_RIGHT:
  1508. it->nXOffSet -= 120;
  1509. it->nXOffSet = it->nXOffSet < 0 ? 0 : it->nXOffSet;
  1510. LoadJPGImageSuperEx(m_nCurAlphaIndex, it->pImage, it->fScale, it->nXOffSet, it->nYOffSet);
  1511. break;
  1512. case VK_LEFT:
  1513. it->nXOffSet += 120;
  1514. LoadJPGImageSuperEx(m_nCurAlphaIndex, it->pImage, it->fScale, it->nXOffSet, it->nYOffSet);
  1515. break;
  1516. case VK_UP:
  1517. it->nYOffSet += 120;
  1518. LoadJPGImageSuperEx(m_nCurAlphaIndex, it->pImage, it->fScale, it->nXOffSet, it->nYOffSet);
  1519. break;
  1520. case VK_DOWN:
  1521. it->nYOffSet -= 120;
  1522. it->nYOffSet = it->nYOffSet < 0 ? 0 : it->nYOffSet;
  1523. LoadJPGImageSuperEx(m_nCurAlphaIndex, it->pImage, it->fScale, it->nXOffSet, it->nYOffSet);
  1524. break;
  1525. }
  1526. // 从这里返回TRUE,则其他控件就不会响应按下事件;
  1527. return TRUE;
  1528. }
  1529. return CDialog::PreTranslateMessage(pMsg);
  1530. }
  1531. /************************************************************************/
  1532. /* 函数:[4/12/2017 Jeff];
  1533. /* 描述:;
  1534. /* 参数:;
  1535. /* [IN] :;
  1536. /* [OUT] :;
  1537. /* [IN/OUT] :;
  1538. /* 返回:void;
  1539. /* 注意:;
  1540. /* 示例:;
  1541. /*
  1542. /* 修改:;
  1543. /* 日期:;
  1544. /* 内容:;
  1545. /************************************************************************/
  1546. CPoint CDlgRenderings::WndPt2AlphaPt(IN const CPoint& WndPt)
  1547. {
  1548. CPoint AlphaPt = WndPt;
  1549. // 1.首先,将窗口坐标系偏移到背景图坐标系(偏移原点坐标,背景图坐标系是m_rcBackground);
  1550. AlphaPt.Offset(-m_rcBackground.left, -m_rcBackground.top);
  1551. // 2.再次从背景图坐标系偏移到PNG显示层坐标系(偏移原点坐标,PNG显示层是m_rcPNGImage);
  1552. AlphaPt.Offset(-m_rcPNGImage.left, -m_rcPNGImage.top);
  1553. // 计算出png原图缩放显示到窗口的缩放比例;
  1554. float fScale = (float)m_rcPNGImage.Width() / m_pPNGImage->GetWidth();
  1555. // 3.再将窗口点缩放到PNG原图比例;
  1556. AlphaPt.x /= fScale;
  1557. AlphaPt.y /= fScale;
  1558. // 计算出png原图缩放到(800x800)的显示区域的比例值;
  1559. // 注:800x800的区域,是计算透明区域的缩放区域;
  1560. // 4.将点缩放到800x800的区域;
  1561. AlphaPt.x /= m_tempscale;
  1562. AlphaPt.y /= m_tempscale;
  1563. return AlphaPt;
  1564. }
  1565. CPoint CDlgRenderings::WndPt2AlphaPt(IN const CPoint & WndPt, IN CRect& rcPNGImage, IN float fScale)
  1566. {
  1567. CPoint AlphaPt = WndPt;
  1568. // 1.首先,将窗口坐标系偏移到背景图坐标系(偏移原点坐标,背景图坐标系是m_rcBackground);
  1569. AlphaPt.Offset(-m_rcBackground.left, -m_rcBackground.top);
  1570. // 2.再次从背景图坐标系偏移到PNG显示层坐标系(偏移原点坐标,PNG显示层是m_rcPNGImage);
  1571. AlphaPt.Offset(-rcPNGImage.left, -rcPNGImage.top);
  1572. // 计算出png原图缩放显示到窗口的缩放比例;
  1573. float fs = (float)rcPNGImage.Width() / m_pPNGImage->GetWidth();
  1574. // 3.再将窗口点缩放到PNG原图比例;
  1575. AlphaPt.x /= fs;
  1576. AlphaPt.y /= fs;
  1577. // 计算出png原图缩放到(800x800)的显示区域的比例值;
  1578. // 注:800x800的区域,是计算透明区域的缩放区域;
  1579. // 4.将点缩放到800x800的区域;
  1580. AlphaPt.x /= fScale;
  1581. AlphaPt.y /= fScale;
  1582. return AlphaPt;
  1583. }
  1584. /************************************************************************/
  1585. /* 函数:[4/12/2017 Jeff];
  1586. /* 描述:;
  1587. /* 参数:;
  1588. /* [IN] :;
  1589. /* [OUT] :;
  1590. /* [IN/OUT] :;
  1591. /* 返回:void;
  1592. /* 注意:;
  1593. /* 示例:;
  1594. /*
  1595. /* 修改:;
  1596. /* 日期:;
  1597. /* 内容:;
  1598. /************************************************************************/
  1599. CPoint CDlgRenderings::AlphaPt2WndPt(IN const CPoint& AlphaPt)
  1600. {
  1601. CPoint WndPt = AlphaPt;
  1602. // 计算出png原图缩放到(800x800)的显示区域的比例值;
  1603. // 注:800x800的区域,是计算透明区域的缩放区域;
  1604. // 1.缩放到png原图比例;
  1605. WndPt.x *= m_tempscale;
  1606. WndPt.y *= m_tempscale;
  1607. // 计算出png原图缩放显示到窗口的缩放比例;
  1608. float fScale = (float)m_rcPNGImage.Width() / m_pPNGImage->GetWidth();
  1609. // 2.再将窗口点缩放到PNG显示层比例;
  1610. WndPt.x *= fScale;
  1611. WndPt.y *= fScale;
  1612. // 3.PNG显示层坐标系偏移到背景图坐标系(偏移原点坐标,PNG显示层是m_rcPNGImage);
  1613. WndPt.Offset(m_rcPNGImage.left, m_rcPNGImage.top);
  1614. // 4.背景图坐标系偏移到窗口坐标系(偏移原点坐标,背景图坐标系是m_rcBackground);
  1615. WndPt.Offset(m_rcBackground.left, m_rcBackground.top);
  1616. return WndPt;
  1617. }
  1618. CPoint CDlgRenderings::AlphaPt2WndPt(IN const CPoint & AlphaPt, IN CRect & rcPNGImage, IN float fScale)
  1619. {
  1620. CPoint WndPt = AlphaPt;
  1621. // 计算出png原图缩放到(800x800)的显示区域的比例值;
  1622. // 注:800x800的区域,是计算透明区域的缩放区域;
  1623. // 1.缩放到png原图比例;
  1624. WndPt.x *= fScale;
  1625. WndPt.y *= fScale;
  1626. // 计算出png原图缩放显示到窗口的缩放比例;
  1627. float fs = (float)rcPNGImage.Width() / m_pPNGImage->GetWidth();
  1628. // 2.再将窗口点缩放到PNG显示层比例;
  1629. WndPt.x *= fs;
  1630. WndPt.y *= fs;
  1631. // 3.PNG显示层坐标系偏移到背景图坐标系(偏移原点坐标,PNG显示层是m_rcPNGImage);
  1632. WndPt.Offset(rcPNGImage.left, rcPNGImage.top);
  1633. // 4.背景图坐标系偏移到窗口坐标系(偏移原点坐标,背景图坐标系是m_rcBackground);
  1634. WndPt.Offset(m_rcBackground.left, m_rcBackground.top);
  1635. return WndPt;
  1636. }
  1637. BOOL CDlgRenderings::IsAlphaIndexExist(IN INT nIndex, vector<STAlphaPt>& vtAlpha)
  1638. {
  1639. BOOL bExist = FALSE;
  1640. for (vector<STAlphaPt>::iterator it = vtAlpha.begin(); it != vtAlpha.end(); it++)
  1641. {
  1642. if (nIndex == it->nAlphaIndex)
  1643. {
  1644. bExist = TRUE;
  1645. break;
  1646. }
  1647. }
  1648. return bExist;
  1649. }
  1650. BOOL CDlgRenderings::IsPngAlphaExist(IN CString strPng, vector<STImageAlpha>& vtPngAlpha)
  1651. {
  1652. BOOL bExist = FALSE;
  1653. for (vector<STImageAlpha>::iterator it = vtPngAlpha.begin(); it != vtPngAlpha.end(); it++)
  1654. {
  1655. if (strPng.CompareNoCase(it->PNGFileName) == 0)
  1656. {
  1657. bExist = TRUE;
  1658. break;
  1659. }
  1660. }
  1661. return bExist;
  1662. }
  1663. /************************************************************************/
  1664. /* 函数:[4/12/2017 Jeff];
  1665. /* 描述:;
  1666. /* 参数:;
  1667. /* [IN] :;
  1668. /* [OUT] :;
  1669. /* [IN/OUT] :;
  1670. /* 返回:void;
  1671. /* 注意:;
  1672. /* 示例:;
  1673. /*
  1674. /* 修改:;
  1675. /* 日期:;
  1676. /* 内容:;
  1677. /************************************************************************/
  1678. void CDlgRenderings::GetPNGAlphaRectInfo(IN Bitmap * pOriginImage, IN OUT STImageAlpha & pngAlpha)
  1679. {
  1680. if (pOriginImage == NULL || pOriginImage->GetWidth() <= 0 || pOriginImage->GetHeight() <= 0)
  1681. return;
  1682. BOOL bUseOrigin = FALSE;
  1683. Bitmap *pZoomObj = NULL;
  1684. pngAlpha.rcZoom = InitRect(pOriginImage);
  1685. if (pOriginImage->GetWidth() > 800 || pOriginImage->GetHeight() > 800)
  1686. {
  1687. // 使用800长宽的矩形来缩放png原图,并且基于该尺寸来获取透明区域;
  1688. CRect rcZoom(0, 0, 800, 800);
  1689. // 计算出适合png原图的rcZoom显示位置;
  1690. RectFitDes(pOriginImage->GetWidth(), pOriginImage->GetHeight(), rcZoom);
  1691. // 创建缩放对象;
  1692. pZoomObj = new Bitmap(rcZoom.Width(), rcZoom.Height(), PixelFormat32bppPARGB);
  1693. if (pZoomObj == NULL)
  1694. return;
  1695. // 将原图画在Zoom对象图中;
  1696. Graphics graphics(pZoomObj);
  1697. graphics.Clear(Color(0, 0, 0, 0));
  1698. graphics.DrawImage(pOriginImage, 0, 0, pZoomObj->GetWidth(), pZoomObj->GetHeight());
  1699. pngAlpha.fScale = (float)pOriginImage->GetWidth() / pZoomObj->GetWidth();
  1700. }
  1701. else
  1702. {
  1703. // 使用原图;
  1704. pZoomObj = pOriginImage->Clone(0, 0, pOriginImage->GetWidth(), pOriginImage->GetHeight(), PixelFormat32bppPARGB);
  1705. bUseOrigin = TRUE;
  1706. pngAlpha.fScale = 1.0;
  1707. }
  1708. UINT *pixels = NULL;
  1709. BitmapData bitmapdata;
  1710. Rect rcZoom(0, 0, pZoomObj->GetWidth(), pZoomObj->GetHeight());
  1711. // 获取缩放图的位图信息;
  1712. pZoomObj->LockBits(&rcZoom, ImageLockModeRead, PixelFormat32bppPARGB, &bitmapdata);
  1713. pixels = (UINT *)bitmapdata.Scan0;
  1714. DWORD dwWidth = bitmapdata.Stride / 4;
  1715. // 像素(x,y)的颜色;
  1716. Color color;
  1717. // 像素(x,y)四周8个方向的像素颜色:
  1718. // (x-1,y-1);
  1719. // (x-0,y-1);
  1720. // (x+1,y-1);
  1721. // (x-1,y-0);
  1722. // (x+1,y-0);
  1723. // (x-1,y+1);
  1724. // (x-0,y+1);
  1725. // (x+1,y+1);
  1726. Color color1, color2, color3, color4, color5, color6, color7, color8;
  1727. // m_rcarray数组,用于记录PNG图片里的alpha色小于200的像素点坐标;
  1728. CArray<int, int> AryAlphaRect[RCARRARCOUNT];
  1729. for (int nAlphaIndex = 0; nAlphaIndex < RCARRARCOUNT; nAlphaIndex++)
  1730. AryAlphaRect[nAlphaIndex].RemoveAll(); // 清空以前的;
  1731. for (INT row = 0; row < rcZoom.Height; ++row)
  1732. {
  1733. for (INT col = 0; col < rcZoom.Width - 1; ++col)
  1734. {
  1735. // 将PNG图片当前像素(col,row)点的颜色值填充到color对象;
  1736. color.SetValue(pixels[row * dwWidth + col]);
  1737. // Color::GetA() 方法获取此Color对象的alpha分量(返回这个颜色的alpha分量)。
  1738. BYTE byalpha = color.GetA();
  1739. if (byalpha < 200)
  1740. {
  1741. CArray<int, int>*pArray = NULL;
  1742. for (int nAlphaIndex = 0; nAlphaIndex < RCARRARCOUNT; nAlphaIndex++)
  1743. {
  1744. if (AryAlphaRect[nAlphaIndex].GetSize() && pArray == NULL)
  1745. {
  1746. // 从区域nAlphaIndex的第一个点开始,判断当前像素点(col,row)是否属于该区域的连续同色;
  1747. double x = AryAlphaRect[nAlphaIndex].ElementAt(0);
  1748. double y = AryAlphaRect[nAlphaIndex].ElementAt(1);
  1749. // x0,y0作为原点;
  1750. int x0 = (int)x;
  1751. int y0 = (int)y;
  1752. // col、row为当前点;
  1753. // x、y为对照点;
  1754. double dtx = col - x;
  1755. double dty = row - y;
  1756. // 求得tan或cot值;
  1757. if (abs(dtx) > abs(dty))
  1758. {// 左右象限(3、4、7、8);
  1759. if (dty)
  1760. {// 在原点下方(4、7);
  1761. dtx /= abs(dty); // cot;
  1762. dty /= abs(dty); // 1;
  1763. }
  1764. else
  1765. {// 在原点上方(3、8);
  1766. if (dtx > 0)
  1767. {//在原点右方(3);
  1768. dtx = 1;
  1769. }
  1770. else if (dtx < 0)
  1771. {//在原点左方(8);
  1772. dtx = -1;
  1773. }
  1774. }
  1775. }
  1776. else
  1777. {// 上下象限(1、2、5、6);
  1778. if (dtx)
  1779. {//在原点右方(2、5);
  1780. dty /= abs(dtx); // tan;
  1781. dtx /= abs(dtx); // 1;
  1782. }
  1783. else
  1784. {//在原点左方(1、6);
  1785. if (dty > 0)
  1786. {//在原点下方(6);
  1787. dty = 1;
  1788. }
  1789. else if (dtx < 0)
  1790. {//在原点上方(1);
  1791. dty = -1;
  1792. }
  1793. }
  1794. }
  1795. // 此时dtx或dty已为tan或cot值,当tan或cot的值大于3时,角度是72或18度左右;
  1796. if (dtx > 3)
  1797. {
  1798. double step = dtx / 3.0;
  1799. dtx /= step;
  1800. dty /= step;
  1801. }
  1802. if (dty > 3)
  1803. {
  1804. double step = dty / 3.0;
  1805. dtx /= step;
  1806. dty /= step;
  1807. }
  1808. BOOL bFind = FALSE;
  1809. while (1)
  1810. {
  1811. x += dtx;
  1812. y += dty;
  1813. if (x >= (rcZoom.Width - 1) || x < 1)break;
  1814. if (y >= (rcZoom.Height - 1) || y < 1)break;
  1815. if (col > x0)
  1816. {//在原点右侧;
  1817. if (x < x0 || x > col)
  1818. {// 新点不在原点和像素点x坐标之间;
  1819. break;
  1820. }
  1821. }
  1822. if (col < x0)
  1823. {//在原点左侧;
  1824. if (x > x0 || x < col)
  1825. {// 新点不在原点和像素点x坐标之间;
  1826. break;
  1827. }
  1828. }
  1829. if (row > y0)
  1830. {//在原点下方;
  1831. if (y < y0 || y > row)
  1832. {// 新点不在原点和像素点y坐标之间;
  1833. break;
  1834. }
  1835. }
  1836. if (row < y0)
  1837. {//在原点上方;
  1838. if (y > y0 || y < row)
  1839. {// 新点不在原点和像素点y坐标之间;
  1840. break;
  1841. }
  1842. }
  1843. color.SetValue(pixels[(int)y * dwWidth + (int)x]);
  1844. if (color.GetA() == 255)
  1845. {
  1846. color1.SetValue(pixels[(int)(y - 1) * dwWidth + (int)(x - 1)]);
  1847. color2.SetValue(pixels[(int)(y - 1) * dwWidth + (int)(x - 0)]);
  1848. color3.SetValue(pixels[(int)(y - 1) * dwWidth + (int)(x + 1)]);
  1849. color4.SetValue(pixels[(int)(y - 0) * dwWidth + (int)(x - 1)]);
  1850. color5.SetValue(pixels[(int)(y - 0) * dwWidth + (int)(x + 1)]);
  1851. color6.SetValue(pixels[(int)(y + 1) * dwWidth + (int)(x - 1)]);
  1852. color7.SetValue(pixels[(int)(y + 1) * dwWidth + (int)(x - 0)]);
  1853. color8.SetValue(pixels[(int)(y + 1) * dwWidth + (int)(x + 1)]);
  1854. if (color1.GetA() == 255 && color2.GetA() == 255 && color3.GetA() == 255 && color4.GetA() == 255 && color5.GetA() == 255 && color6.GetA() == 255 && color7.GetA() == 255 && color8.GetA() == 255)
  1855. {// 当前像素4周8个方向的像素点颜色与当前像素一致,表明为连续区域;
  1856. bFind = 1;
  1857. break;
  1858. }
  1859. }
  1860. }
  1861. if (!bFind)
  1862. {// 不是连续点,则开一个数组保存这块新的区域;
  1863. pArray = &(AryAlphaRect[nAlphaIndex]);
  1864. }
  1865. }
  1866. }
  1867. if (pArray == NULL)
  1868. {// 未找到相邻同色的点;
  1869. BOOL bcontinue = 1;
  1870. for (int yy = 0; yy < RCARRARCOUNT; yy++)
  1871. {
  1872. if (AryAlphaRect[yy].GetSize() == 0)
  1873. {
  1874. pArray = &(AryAlphaRect[yy]);
  1875. bcontinue = 0;
  1876. break;
  1877. }
  1878. }
  1879. if (bcontinue)
  1880. continue;
  1881. }
  1882. pArray->Add(col); // x坐标;
  1883. pArray->Add(row); // y坐标;
  1884. }
  1885. }
  1886. }
  1887. pZoomObj->UnlockBits(&bitmapdata);
  1888. if (pZoomObj )
  1889. delete pZoomObj;
  1890. pZoomObj = NULL;
  1891. for (int i = 0; i < RCARRARCOUNT; i++)
  1892. {
  1893. // 计算出透明区域的3个有效点;
  1894. CPoint ptCur, ptOrigin, ptOriginX, ptOriginY;
  1895. int dwLeng = 100000000, dwLengO = 100000000, dwLengX = 100000000, dwLengY = 100000000;
  1896. if (AryAlphaRect[i].GetSize() > 0)
  1897. {
  1898. for (int j = 0; j < AryAlphaRect[i].GetSize(); j += 2)
  1899. {
  1900. ptCur.x = AryAlphaRect[i].ElementAt(j);
  1901. ptCur.y = AryAlphaRect[i].ElementAt(j + 1);
  1902. // 原点;
  1903. dwLeng = GetLengFromPt(CPoint(0, 0), ptCur);
  1904. if (dwLeng < dwLengO)
  1905. {
  1906. dwLengO = dwLeng;
  1907. ptOrigin = ptCur;
  1908. }
  1909. // 右边点;
  1910. dwLeng = GetLengFromPt(CPoint(pOriginImage->GetWidth(), 0), ptCur);
  1911. if (dwLeng < dwLengX)
  1912. {
  1913. dwLengX = dwLeng;
  1914. ptOriginX = ptCur;
  1915. }
  1916. // 下边点;
  1917. dwLeng = GetLengFromPt(CPoint(0, pOriginImage->GetHeight()), ptCur);
  1918. if (dwLeng < dwLengY)
  1919. {
  1920. dwLengY = dwLeng;
  1921. ptOriginY = ptCur;
  1922. }
  1923. }
  1924. if (!IsAlphaIndexExist(i, pngAlpha.vtAlphaPt))
  1925. { // 保存透明信息;
  1926. STAlphaPt ptAlpha;
  1927. ptAlpha.nAlphaIndex = i;
  1928. ptAlpha.ptOrigin = ptOrigin;
  1929. ptAlpha.ptOriginX = ptOriginX;
  1930. ptAlpha.ptOriginY = ptOriginY;
  1931. pngAlpha.vtAlphaPt.push_back(ptAlpha);
  1932. }
  1933. }
  1934. }
  1935. }
  1936. /************************************************************************/
  1937. /* 函数:[4/12/2017 Jeff];
  1938. /* 描述:;
  1939. /* 参数:;
  1940. /* [IN] :;
  1941. /* [OUT] :;
  1942. /* [IN/OUT] :;
  1943. /* 返回:void;
  1944. /* 注意:;
  1945. /* 示例:;
  1946. /*
  1947. /* 修改:;
  1948. /* 日期:;
  1949. /* 内容:;
  1950. /************************************************************************/
  1951. DWORD CDlgRenderings::Thread_GetAllPNGInfo(IN LPVOID lpVoid)
  1952. {
  1953. if (lyfzGlobal::g_strPathOfPNG.IsEmpty() || !PathFileExists(lyfzGlobal::g_strPathOfPNG))
  1954. return 0;
  1955. filehelpImpl ffhelp;
  1956. STR_VEC vtfiles;
  1957. ffhelp.getfiles_findout_subfolder(lyfzGlobal::g_strPathOfPNG, _T("*.png"), &vtfiles);
  1958. if (vtfiles.size() == 0)
  1959. return 0;
  1960. Image *pImage = NULL;
  1961. STR_VEC::iterator it = vtfiles.begin();
  1962. for (; it != vtfiles.end(); it++)
  1963. {
  1964. // 加载png原图;
  1965. IMGCommon::LoadImgFromFile(&pImage, it->c_str());
  1966. if (pImage)
  1967. {
  1968. // 获取png透明区域详情;
  1969. if (!IsPngAlphaExist(it->c_str(), m_vtPngAlphaInfo))
  1970. {
  1971. STImageAlpha pngAlpha;
  1972. pngAlpha.PNGFileName = it->c_str();
  1973. GetPNGAlphaRectInfo((Bitmap*)pImage, pngAlpha);
  1974. m_vtPngAlphaInfo.push_back(pngAlpha);
  1975. }
  1976. delete pImage;
  1977. pImage = NULL;
  1978. }
  1979. }
  1980. return 0;
  1981. }
  1982. void CDlgRenderings::DrawAlphaEdge(IN CPoint pt, IN BOOL bWhite)
  1983. {
  1984. if (m_nSelectPNGPosition == -1)
  1985. return;
  1986. m_ptLastRectangle = pt;
  1987. // 获取png信息;
  1988. STAlphaPt *pAlphaPt = NULL;
  1989. STImageAlpha *pImageAlpha = NULL;
  1990. CString strPNGFile = m_AryEffectPNGImg.ElementAt(m_nSelectPNGPosition);
  1991. for (vector<STImageAlpha>::iterator it = m_vtPngAlphaInfo.begin(); it != m_vtPngAlphaInfo.end(); it++ )
  1992. {
  1993. if (strPNGFile.CompareNoCase(it->PNGFileName) == 0)
  1994. {
  1995. pImageAlpha = (STImageAlpha *)(&*it);
  1996. // 将窗口坐标系转成透明区域坐标系;
  1997. pt = WndPt2AlphaPt(pt, it->rcZoom, it->fScale);
  1998. for (vector<STAlphaPt>::iterator iit = it->vtAlphaPt.begin(); iit != it->vtAlphaPt.end(); iit++)
  1999. {
  2000. CRect rcAlpha(iit->ptOrigin.x, iit->ptOrigin.y, iit->ptOriginX.x, iit->ptOriginY.y);
  2001. if (rcAlpha.PtInRect(pt))
  2002. {
  2003. pAlphaPt = (STAlphaPt*)(&*iit);
  2004. break;
  2005. }
  2006. }
  2007. break;
  2008. }
  2009. }
  2010. if (pImageAlpha == NULL || pAlphaPt == NULL )
  2011. return;
  2012. // 获取出透明区域的三点;
  2013. CPoint ptOrigin = pAlphaPt->ptOrigin, ptOriginX = pAlphaPt->ptOriginX, ptOriginY = pAlphaPt->ptOriginY;
  2014. // 将坐标系从800*800缩放回png原图坐标系;
  2015. ptOrigin.x *= pImageAlpha->fScale;
  2016. ptOrigin.y *= pImageAlpha->fScale;
  2017. ptOriginX.x *= pImageAlpha->fScale;
  2018. ptOriginX.y *= pImageAlpha->fScale;
  2019. ptOriginY.x *= pImageAlpha->fScale;
  2020. ptOriginY.y *= pImageAlpha->fScale;
  2021. // 画边框;
  2022. Graphics graphics(m_pbmp);
  2023. Graphics graphics1(m_pPNGImage);
  2024. // 画到PNG原图上指定透明区域;
  2025. Rect rfAlpha(ptOrigin.x + 10, ptOrigin.y + 10, ptOriginX.x - ptOrigin.x - 20, ptOriginY.y - ptOrigin.y - 20);
  2026. // 创建画笔;
  2027. if (!bWhite)
  2028. {
  2029. Pen blackPen(Color(255, 255, 0, 0), 1);
  2030. // 画矩形到png上;
  2031. graphics1.DrawRectangle(&blackPen, rfAlpha);
  2032. }
  2033. else
  2034. {
  2035. STOffSetInfo *pOffSet = NULL;
  2036. for (vector<STOffSetInfo>::iterator it = m_vtOffSetInfo.begin(); it != m_vtOffSetInfo.end(); it++ )
  2037. {
  2038. if (pAlphaPt->nAlphaIndex == it->nIndexOfAlpha)
  2039. {
  2040. pOffSet = (STOffSetInfo *)(&*it);
  2041. break;
  2042. }
  2043. }
  2044. if (pOffSet != NULL && pOffSet->pImage)
  2045. {
  2046. LoadJPGImageSuperEx(pOffSet->nIndexOfAlpha, pOffSet->pImage, pOffSet->fScale, pOffSet->nXOffSet, pOffSet->nYOffSet);
  2047. return;
  2048. }
  2049. Pen blackPen(Color(255, 255, 255, 255), 3);
  2050. // 画矩形到png上;
  2051. graphics1.DrawRectangle(&blackPen, rfAlpha);
  2052. }
  2053. // 再将png画到背景图上;
  2054. Rect rcPng(pImageAlpha->rcZoom.left, pImageAlpha->rcZoom.top, pImageAlpha->rcZoom.Width(), pImageAlpha->rcZoom.Height());
  2055. graphics.DrawImage(m_pPNGImage, rcPng, 0, 0, m_pPNGImage->GetWidth(), m_pPNGImage->GetHeight(), UnitPixel);
  2056. // 刷新透明区域;
  2057. ptOrigin = AlphaPt2WndPt(pAlphaPt->ptOrigin, pImageAlpha->rcZoom, pImageAlpha->fScale);
  2058. ptOriginX = AlphaPt2WndPt(pAlphaPt->ptOriginX, pImageAlpha->rcZoom, pImageAlpha->fScale);
  2059. ptOriginY = AlphaPt2WndPt(pAlphaPt->ptOriginY, pImageAlpha->rcZoom, pImageAlpha->fScale);
  2060. InvalidateRect(CRect(ptOrigin.x - 6, ptOrigin.y - 6, ptOriginX.x + 8, ptOriginY.y + 8));
  2061. }
  2062. void CDlgRenderings::AddNewImageItem(IN CString strImageFile)
  2063. {
  2064. // 文件路径是否有效;
  2065. if (!PathFileExists(strImageFile))
  2066. return;
  2067. // 加载图片;
  2068. BOOL bZoom = FALSE;
  2069. Bitmap *pImage = NULL;
  2070. IMGCommon::LoadImgFromFile((Image**)&pImage, strImageFile);
  2071. if ( pImage && pImage->GetWidth() != 0 )
  2072. {
  2073. if ( pImage->GetWidth() > 2000 || pImage->GetHeight() > 2000 )
  2074. {// 尺寸大于2000的,缩放;
  2075. bZoom = TRUE;
  2076. CRect rcZoom(0, 0, 2000, 2000);
  2077. RectFitDes(pImage->GetWidth(), pImage->GetHeight(), rcZoom);
  2078. // 新建缩略对象,并替换原来的;
  2079. Bitmap *pZoomImage = new Bitmap(rcZoom.Width(), rcZoom.Height(), PixelFormat32bppARGB);
  2080. // 将原图画在缩略图对象上;
  2081. Graphics graphics(pZoomImage);
  2082. graphics.Clear(Color(0,0,0,0)); // 透明黑填充;
  2083. graphics.DrawImage(pImage, 0, 0, pZoomImage->GetWidth(), pZoomImage->GetHeight());
  2084. pImage = pZoomImage;
  2085. }
  2086. // 将效果图保存到指定目录;
  2087. CString strImageName = _T("");
  2088. CString strImageDestPath = _T("");
  2089. // 获取文件名;
  2090. INT nIndex = strImageFile.ReverseFind(_T('\\'));
  2091. strImageName = strImageFile.Mid(nIndex+1);
  2092. strImageName = strImageName.Left(strImageName.Find(_T('.')));
  2093. // 保存文件;
  2094. strImageDestPath.Format(_T("%s\\%s.png"), g_strPathOfPNG, strImageName);
  2095. IMGCommon::SaveImg2newfile(pImage, strImageDestPath, 100);
  2096. // 添加到主程序中;
  2097. m_AryEffectPNGImg.Add(strImageDestPath);
  2098. // 创建缩放区域,并适用图片比例;
  2099. CRect rcZoom(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
  2100. RectFitDes(pImage->GetWidth(), pImage->GetHeight(), rcZoom);
  2101. // 创建背景图;
  2102. Bitmap *pZoomImage = new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat32bppARGB);
  2103. Graphics grahpics(pZoomImage);
  2104. // 产生边框效果,先用灰色填充,再用白色画刷填充偏移区域;
  2105. grahpics.Clear(Color(255, 192, 192, 192));
  2106. SolidBrush brushWhite(Color(255, 255, 255, 255));
  2107. grahpics.FillRectangle(&brushWhite, 1, 1, THUMBNAIL_WIDTH - 2, THUMBNAIL_HEIGHT - 2);
  2108. // 再将png缩放到背景图中;
  2109. grahpics.DrawImage(pImage, Rect(rcZoom.left, rcZoom.top, rcZoom.Width(), rcZoom.Height()), 0, 0, pImage->GetWidth(), pImage->GetHeight(), UnitPixel);
  2110. // 往list里添加项,先创建Imagelist位图;
  2111. HBITMAP hbmp;
  2112. CBitmap bitmap;
  2113. pZoomImage->GetHBITMAP(Color(255, 255, 255, 255), &hbmp);
  2114. bitmap.Attach(hbmp);
  2115. // 将位图添加到ImageList中;
  2116. m_ImageListThumb.Add(&bitmap, RGB(255,255,255));
  2117. INT nSize = m_ListPNG.GetItemCount();
  2118. m_ListPNG.SetRedraw(FALSE);
  2119. m_ListPNG.InsertItem(nSize, strImageName, nSize);
  2120. m_ListPNG.SetRedraw(TRUE);
  2121. m_ListPNG.RedrawItems(nSize, nSize);
  2122. if (pZoomImage)
  2123. delete pZoomImage;
  2124. if (pImage)
  2125. delete pImage;
  2126. }
  2127. }
  2128. BOOL CDlgRenderings::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  2129. {
  2130. // TODO: 在此添加消息处理程序代码和/或调用默认值
  2131. if (m_nSelectPNGPosition >= 0 && m_nSelectJPGPosition >= 0)
  2132. {
  2133. ScreenToClient(&pt);
  2134. INT nAlphaIndex = IsPointInAlphaRect(pt);
  2135. if (nAlphaIndex != -1)
  2136. {
  2137. vector<STOffSetInfo>::iterator it = m_vtOffSetInfo.begin();
  2138. for (; it != m_vtOffSetInfo.end(); it++)
  2139. {
  2140. if (it->nIndexOfAlpha == nAlphaIndex)
  2141. {
  2142. break;
  2143. }
  2144. }
  2145. if (it == m_vtOffSetInfo.end())
  2146. return FALSE;
  2147. if (zDelta > 0)
  2148. {// 放大;
  2149. it->fScale -= 0.1;
  2150. }
  2151. else
  2152. {// 缩小;
  2153. it->fScale += 0.1;
  2154. }
  2155. LoadJPGImageSuperEx(nAlphaIndex, it->pImage, it->fScale, it->nXOffSet, it->nYOffSet);
  2156. }
  2157. }
  2158. return CDialog::OnMouseWheel(nFlags, zDelta, pt);
  2159. }