IMGCommon.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. #include "StdAfx.h"
  2. #include "IMGCommon.h"
  3. #include <strsafe.h>
  4. #include <Shlwapi.h>
  5. // _bstr_t头文件;
  6. #include <comutil.h>
  7. #pragma comment(lib, "comsuppw.lib")
  8. const ColorMatrix _ClrMatrix =
  9. {
  10. 1.0, 0.0, 0.0, 0.0, 0.0,
  11. 0.0, 1.0, 0.0, 0.0, 0.0,
  12. 0.0, 0.0, 0.3, 0.0, 0.0,
  13. 0.0, 0.0, 0.0, 1.0, 0.0,
  14. 0.0, 0.0, 0.0, 0.0, 1.0,
  15. };
  16. ULONG_PTR IMGCommon::m_gdiplusToken;
  17. IMGCommon::IMGCommon(void)
  18. {
  19. GdiplusStartupInput gdiplusStartupInput;
  20. GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  21. }
  22. IMGCommon::~IMGCommon(void)
  23. {
  24. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  25. }
  26. /************************************************************************/
  27. /* 函数:[4/1/2017 Jeff];
  28. /* 描述:;
  29. /* 参数:;
  30. /* [IN] :;
  31. /* [OUT] :;
  32. /* [IN/OUT] :;
  33. /* 返回:void;
  34. /* 注意:;
  35. /* 示例:;
  36. /*
  37. /* 修改:;
  38. /* 日期:;
  39. /* 内容:;
  40. /************************************************************************/
  41. BOOL IMGCommon::LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath, IN BOOL useEmbeddedColorManagement /* = FALSE */)
  42. {
  43. if ( !PathFileExists(lpPath) )
  44. return FALSE;
  45. if ( *pImg )
  46. delete *pImg;
  47. *pImg = NULL;
  48. #ifdef UNICODE
  49. *pImg = Image::FromFile(lpPath, TRUE);
  50. #else
  51. BSTR strtmp = _bstr_t(lpPath);
  52. *pImg = Image::FromFile(strtmp, TRUE);
  53. SysFreeString(strtmp);
  54. #endif
  55. return (*pImg ? TRUE : FALSE);
  56. }
  57. BOOL IMGCommon::LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen)
  58. {
  59. if ( pBuffer == NULL )
  60. return FALSE;
  61. if ( *pImg )
  62. delete *pImg;
  63. *pImg = NULL;
  64. HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
  65. if ( hMemery == NULL )
  66. return FALSE;
  67. BYTE *pMem = (BYTE*)GlobalLock(hMemery);
  68. memcpy(pMem, pBuffer, nBufLen);
  69. IStream *pstream = NULL;
  70. CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
  71. *pImg = Image::FromStream(pstream);
  72. GlobalUnlock(hMemery);
  73. pstream->Release();
  74. return (*pImg ? TRUE : FALSE);
  75. }
  76. // 先以只读方式从文件中读取二进制流出来,可以做到不独占文件;
  77. BOOL IMGCommon::LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath, IN BOOL useEmbeddedColorManagement /* = FALSE */)
  78. {
  79. if ( !PathFileExists(lpPath) )
  80. return FALSE;
  81. if ( *pImg )
  82. delete *pImg;
  83. *pImg = NULL;
  84. CFile fp;
  85. CFileException e;
  86. BOOL bRet = FALSE;
  87. if ( fp.Open(lpPath, CFile::modeRead, &e))
  88. {
  89. DWORD dwLength = (DWORD)fp.GetLength();
  90. BYTE *pData = new BYTE[dwLength];
  91. fp.Read(pData,dwLength);
  92. fp.Close();
  93. bRet = LoadImgFromBuffer(pImg, pData, dwLength);
  94. if( pData )
  95. delete []pData;
  96. }
  97. return bRet;
  98. }
  99. /************************************************************************/
  100. /* 函数:LoadImgFromResource[9/21/2016 IT];
  101. /* 描述:从资源中加载;
  102. /* 参数:;
  103. /* [IN] :;
  104. /* [OUT] :;
  105. /* [IN/OUT] :;
  106. /* 返回:void;
  107. /* 注意:;
  108. /* 示例:;
  109. /*
  110. /* 修改:;
  111. /* 日期:;
  112. /* 内容:;
  113. /************************************************************************/
  114. Image* IMGCommon::LoadImgFromResource(IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType)
  115. {
  116. HGLOBAL hGlobal = NULL;
  117. HRSRC hSource = NULL;
  118. LPVOID lpBuffer = NULL;
  119. DWORD dwSize = 0;
  120. // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可
  121. hSource = FindResource(hModule, lpName, lpType);
  122. if (hSource == NULL)
  123. {
  124. _tprintf(_T("载入资源失败:%s"), lpName);
  125. return NULL;
  126. }
  127. // 2.获取资源的大小;
  128. dwSize = (UINT)SizeofResource(NULL, hSource);
  129. // 3.加载资源;
  130. hGlobal = LoadResource(NULL, hSource);
  131. if (hGlobal == NULL)
  132. {
  133. _tprintf(_T("载入资源失败:%s"), lpName);
  134. return NULL;
  135. }
  136. // 4.锁定资源,获取buffer;
  137. lpBuffer = LockResource(hGlobal);
  138. if (lpBuffer == NULL)
  139. {
  140. _tprintf(_T("载入资源失败:%s"), lpName);
  141. return NULL;
  142. }
  143. // lpFileFullName需要先判断文件是否存在??不需要;
  144. Image *pImg = NULL;
  145. LoadImgFromBuffer(&pImg, (BYTE*)lpBuffer, dwSize);
  146. UnlockResource(hGlobal);
  147. FreeResource(hGlobal);
  148. return pImg;
  149. }
  150. BOOL IMGCommon::GetOrientation(IN Image *pImg)
  151. {
  152. if (pImg == NULL) return FALSE;
  153. UINT totalBufferSize;
  154. UINT numProperties;
  155. pImg->GetPropertySize(&totalBufferSize, &numProperties);
  156. // Allocate the buffer that will receive the property items.
  157. PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
  158. // Fill the buffer.
  159. pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
  160. // Print the id data member of each property item.
  161. for (UINT j = 0; j < numProperties; ++j)
  162. {
  163. if (PropertyTagOrientation == pAllItems[j].id)
  164. {
  165. short* ptrLong = (short*)(pAllItems[j].value);
  166. int ret = (int)*ptrLong;
  167. free(pAllItems);
  168. return ret;
  169. }
  170. }
  171. free(pAllItems);
  172. return TRUE;
  173. }
  174. /************************************************************************/
  175. /* 函数:GetEncoderClsid[4/1/2017 Jeff];
  176. /* 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
  177. /* 参数:;
  178. /* [IN] format:要获取的图像格式;
  179. /* [OUT] pClsid:返回符合条件的图像编码器信息;
  180. /* [IN/OUT] :;
  181. /* 返回:成功返回编码器索引,否则返回-1;
  182. /* 注意:;
  183. /* 示例:;
  184. /*
  185. /* 修改:;
  186. /* 日期:;
  187. /* 内容:;
  188. /************************************************************************/
  189. int IMGCommon::GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
  190. {
  191. // GDI+支持的图像编码器数量;
  192. UINT numEncoders = 0;
  193. // GDI+所有图像格式编码器详细信息所需要的空间大小;
  194. UINT nSize = 0;
  195. ImageCodecInfo* pImageCodecInfo = NULL;
  196. // 2.获取GDI+支持的所有图像格式编码器详细信息所需要的空间大小;
  197. GetImageEncodersSize(&numEncoders, &nSize);
  198. if (nSize == 0)
  199. return -1;
  200. //3.为ImageCodecInfo数组分配足额空间;
  201. pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
  202. if (pImageCodecInfo == NULL)
  203. return -1;
  204. //4.获取所有的图像编码器信息;
  205. GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
  206. //5.查找符合的图像编码器的Clsid;
  207. for (UINT i = 0; i < numEncoders; ++i)
  208. {
  209. if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
  210. {
  211. *pClsid = pImageCodecInfo[i].Clsid;
  212. free(pImageCodecInfo);
  213. return i; // Success
  214. }
  215. }
  216. //6.释放步骤3分配的内存;
  217. free(pImageCodecInfo);
  218. return -1;
  219. }
  220. /************************************************************************/
  221. /* 函数:SaveImg2newfile[4/1/2017 Jeff];
  222. /* 描述:将Image对象另存为其他格式的文件;
  223. /* 参数:;
  224. /* [IN] pImg:GDI+ Image对象指针,需要保存的图像对象;
  225. /* [IN] lpnewfile:其他格式的新文件名(jpg,bmp,png,jpeg);
  226. /* [IN] uQuality:另存为新文件时的图像质量值;
  227. /* 返回:Save 若文件存在,会覆盖存在的文件;
  228. /* 注意:;
  229. /* 示例:;
  230. /*
  231. /* 修改:;
  232. /* 日期:;
  233. /* 内容:;
  234. /************************************************************************/
  235. BOOL IMGCommon::SaveImg2newfile(IN Image* pImg, IN CString strNewfile, IN ULONG uQuality)
  236. {
  237. if (pImg == NULL) return FALSE;
  238. // 需要判断路径是否存在,不存在创建目录;
  239. int nIndex = strNewfile.ReverseFind(_T('\\'));
  240. if (nIndex == -1)
  241. return FALSE;
  242. if (!PathFileExists(strNewfile.Left(nIndex)))
  243. {
  244. // 如果文件夹不存在,创建;
  245. SHCreateDirectoryEx(NULL, strNewfile.Left(nIndex), NULL);
  246. }
  247. nIndex = strNewfile.ReverseFind(_T('.'));
  248. if (nIndex == -1)
  249. return FALSE;
  250. Status stat = GenericError;
  251. CLSID encoderClsid = { 0 };
  252. BSTR newfile = strNewfile.AllocSysString();
  253. strNewfile = strNewfile.Mid(nIndex + 1);
  254. if (strNewfile.CompareNoCase(_T("bmp")) == 0)
  255. {
  256. GetEncoderClsid(L"image/bmp", &encoderClsid);
  257. stat = pImg->Save(newfile, &encoderClsid, NULL);
  258. }
  259. else if (strNewfile.CompareNoCase(_T("png")) == 0)
  260. {
  261. GetEncoderClsid(L"image/png", &encoderClsid);
  262. stat = pImg->Save(newfile, &encoderClsid, NULL);
  263. }
  264. else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
  265. {
  266. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  267. EncoderParameters encoderParameters;
  268. encoderParameters.Count = 1;
  269. encoderParameters.Parameter[0].Guid = EncoderQuality;
  270. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  271. encoderParameters.Parameter[0].NumberOfValues = 1;
  272. // Save the image as a JPEG with quality level 100.
  273. encoderParameters.Parameter[0].Value = &uQuality;
  274. stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
  275. }
  276. SysFreeString(newfile);
  277. return stat == Ok ? TRUE : FALSE;
  278. }
  279. BOOL IMGCommon::SaveImg2newfile(IN Image* pImg, IN TString strNewfile, IN ULONG uQuality)
  280. {
  281. if (pImg == NULL) return FALSE;
  282. // 需要判断路径是否存在,不存在创建目录;
  283. int nIndex = strNewfile.find_last_of(_T('\\'));
  284. if (nIndex == TString::npos)
  285. return FALSE;
  286. if (!PathFileExists(strNewfile.substr(0, nIndex).c_str()))
  287. {
  288. // 如果文件夹不存在,创建;
  289. SHCreateDirectoryEx(NULL, strNewfile.substr(0, nIndex).c_str(), NULL);
  290. }
  291. nIndex = strNewfile.find_last_of(_T('.'));
  292. if (nIndex == TString::npos)
  293. return FALSE;
  294. Status stat = GenericError;
  295. CLSID encoderClsid = { 0 };
  296. BSTR newfile = _bstr_t(strNewfile.c_str());
  297. strNewfile = strNewfile.substr(nIndex + 1);
  298. if (strNewfile.find(_T("bmp")) == 0)
  299. {
  300. GetEncoderClsid(L"image/bmp", &encoderClsid);
  301. stat = pImg->Save(newfile, &encoderClsid, NULL);
  302. }
  303. else if (strNewfile.find(_T("png")) == 0)
  304. {
  305. GetEncoderClsid(L"image/png", &encoderClsid);
  306. stat = pImg->Save(newfile, &encoderClsid, NULL);
  307. }
  308. else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
  309. {
  310. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  311. EncoderParameters encoderParameters;
  312. encoderParameters.Count = 1;
  313. encoderParameters.Parameter[0].Guid = EncoderQuality;
  314. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  315. encoderParameters.Parameter[0].NumberOfValues = 1;
  316. // Save the image as a JPEG with quality level 100.
  317. encoderParameters.Parameter[0].Value = &uQuality;
  318. stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
  319. }
  320. SysFreeString(newfile);
  321. return stat == Ok ? TRUE : FALSE;
  322. }
  323. /************************************************************************/
  324. /* 函数:ZoomImg[4/1/2017 Jeff];
  325. /* 描述:缩放到指定大小的区域中,返回缩放后的尺寸;
  326. /* 参数:;
  327. /* [IN] Imgrc:图像的大小;
  328. /* [OUT] dwDisplayPix:图像要显示的区域的大小;
  329. /* [IN/OUT] dwZoomPix:图像在显示区域内缩放后的尺寸;
  330. /* 返回:void;
  331. /* 注意:dwZoomPix的尺寸必须在dwDisplayPix内;
  332. /* 示例:;
  333. /*
  334. /* 修改:;
  335. /* 日期:;
  336. /* 内容:;
  337. /************************************************************************/
  338. int IMGCommon::ZoomImg(IN CRect &Imgrc, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
  339. {
  340. double fDisplayWidth = GET_XPIX(dwDisplayPix);
  341. double fDisplayHeight = GET_YPIX(dwDisplayPix);
  342. // 显示区域长宽比;
  343. double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
  344. // 图片长宽比;
  345. double fImgAspectRatio = ((double)Imgrc.Width()) / ((double)Imgrc.Height());
  346. double fZoomWidth;
  347. double fZoomHeight;
  348. if (fDisplayAspectRatio > fImgAspectRatio)
  349. {
  350. fZoomWidth = fDisplayHeight*fImgAspectRatio;
  351. fZoomHeight = fDisplayHeight;
  352. }
  353. else
  354. {
  355. fZoomWidth = fDisplayWidth;
  356. fZoomHeight = fDisplayWidth / fImgAspectRatio;
  357. }
  358. dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
  359. //////////////////////////////////////////////////////////////////////////
  360. int nRetval = 0;
  361. if ((fDisplayWidth == Imgrc.Width()) && (fDisplayHeight == Imgrc.Height()))
  362. {
  363. nRetval = ZoomNull;
  364. }
  365. else if ((fDisplayWidth > Imgrc.Width()) && (fDisplayHeight > Imgrc.Height()))
  366. {
  367. nRetval = ZoomOut;
  368. }
  369. else
  370. {
  371. nRetval = ZoomIn;
  372. }
  373. return nRetval;
  374. }
  375. int IMGCommon::ZoomImg(IN CONST DWORD &dwImgPix, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
  376. {
  377. double fDisplayWidth = GET_XPIX(dwDisplayPix);
  378. double fDisplayHeight = GET_YPIX(dwDisplayPix);
  379. // 显示区域长宽比;
  380. double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
  381. // 图片长宽比;
  382. double fImgAspectRatio = ((double)GET_XPIX(dwImgPix)) / ((double)GET_YPIX(dwImgPix));
  383. double fZoomWidth;
  384. double fZoomHeight;
  385. if (fDisplayAspectRatio > fImgAspectRatio)
  386. {
  387. fZoomWidth = fDisplayHeight*fImgAspectRatio;
  388. fZoomHeight = fDisplayHeight;
  389. }
  390. else
  391. {
  392. fZoomWidth = fDisplayWidth;
  393. fZoomHeight = fDisplayWidth / fImgAspectRatio;
  394. }
  395. dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
  396. //////////////////////////////////////////////////////////////////////////
  397. int nRetval = 0;
  398. if ((fDisplayWidth == GET_XPIX(dwImgPix)) && (fDisplayHeight == GET_YPIX(dwImgPix)))
  399. {
  400. nRetval = ZoomNull;
  401. }
  402. else if ((fDisplayWidth > GET_XPIX(dwImgPix)) && (fDisplayHeight > GET_YPIX(dwImgPix)))
  403. {
  404. nRetval = ZoomOut;
  405. }
  406. else
  407. {
  408. nRetval = ZoomIn;
  409. }
  410. return nRetval;
  411. }
  412. /************************************************************************/
  413. /* 函数:ImgThumbnail[4/1/2017 Jeff];
  414. /* 描述:生成高质量的图像缩略图;
  415. /* 参数:;
  416. /* [IN] :;
  417. /* [OUT] :;
  418. /* [IN/OUT] :;
  419. /* 返回:void;
  420. /* 注意:为了健壮性,strNewfile的路径可以在函数内再次判断是否有效,防止在函数外没有进行经判断;
  421. /* 示例:;
  422. /*
  423. /* 修改:;
  424. /* 日期:;
  425. /* 内容:;
  426. /************************************************************************/
  427. BOOL IMGCommon::ImgThumbnail(IN Image* pImg, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
  428. {
  429. if (pImg == NULL) return FALSE;
  430. DWORD dwZoomPix;
  431. int retval = ZoomImg(SET_PIX(pImg->GetWidth(), pImg->GetHeight()), dwDisplayPix, dwZoomPix);
  432. if (retval == ZoomOut)
  433. {
  434. return FALSE;
  435. }
  436. else if (retval == ZoomNull)
  437. {
  438. return SaveImg2newfile(pImg, strNewfile, uQuality);
  439. }
  440. // 绘制缩略图;
  441. Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  442. Graphics *graphic = Graphics::FromImage(&bitmap);
  443. //graphic->Clear(Color(255, 255, 255, 255));//如果png包含有透明区域,会无效;
  444. // 设置缩放或旋转图片时的算法(图片质量);
  445. graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
  446. // 设置渲染图形对象的质量;
  447. graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
  448. // 设置图形对象的像素偏移模式;
  449. graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
  450. // 设置图形对象的合成模式;
  451. graphic->SetCompositingMode(CompositingModeSourceOver);
  452. // 设置图形对象的合成质量;
  453. graphic->SetCompositingQuality(CompositingQualityHighQuality);
  454. // 设置图形对象的文本渲染模式;
  455. graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
  456. graphic->DrawImage(pImg, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  457. delete graphic;
  458. // 生成缩略图;
  459. return SaveImg2newfile(&bitmap, strNewfile, uQuality);
  460. }
  461. BOOL IMGCommon::ImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality, IN BOOL useEmbeddedColorManagement /* = FALSE */)
  462. {
  463. if (!PathFileExists(lpImgpath))
  464. return FALSE;
  465. #ifdef UNICODE
  466. Image Img(lpImgpath, useEmbeddedColorManagement);
  467. #else
  468. BSTR strtmp = _bstr_t(lpImgpath);
  469. Image Img(strtmp,useEmbeddedColorManagement);
  470. SysFreeString(strtmp);
  471. #endif
  472. DWORD dwZoomPix;
  473. int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
  474. if (retval == ZoomOut)
  475. {
  476. return FALSE;
  477. }
  478. else if (retval == ZoomNull)
  479. {
  480. return SaveImg2newfile(&Img, strNewfile, uQuality);
  481. }
  482. // 绘制缩略图;
  483. Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  484. Graphics *graphic = Graphics::FromImage(&bitmap);
  485. //graphic->Clear(Color(255, 255, 255, 255));//如果png包含有透明区域,会无效;
  486. // 设置缩放或旋转图片时的算法(图片质量);
  487. graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
  488. // 设置渲染图形对象的质量;
  489. graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
  490. // 设置图形对象的像素偏移模式;
  491. graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
  492. // 设置图形对象的合成模式;
  493. graphic->SetCompositingMode(CompositingModeSourceOver);
  494. // 设置图形对象的合成质量;
  495. graphic->SetCompositingQuality(CompositingQualityHighQuality);
  496. // 设置图形对象的文本渲染模式;
  497. graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
  498. Status st = graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  499. if ( st != Ok )
  500. {
  501. delete graphic;
  502. return FALSE;
  503. }
  504. delete graphic;
  505. // 生成缩略图;
  506. return SaveImg2newfile(&bitmap, strNewfile, uQuality);
  507. }
  508. BOOL IMGCommon::ImgThumbnail(IN CONST TString &strImgpath, IN CONST DWORD &dwDisplayPix, IN TString strNewfile, IN ULONG uQuality, IN BOOL useEmbeddedColorManagement /* = FALSE */)
  509. {
  510. if (!PathFileExists(strImgpath.c_str()))
  511. {
  512. SetLastError(GetLastError());
  513. return FALSE;
  514. }
  515. #ifdef UNICODE
  516. BSTR strtmp = _bstr_t(strImgpath.c_str());
  517. Image Img(strtmp, useEmbeddedColorManagement);
  518. SysFreeString(strtmp);
  519. //Image Img(strImgpath);
  520. #else
  521. BSTR strtmp = _bstr_t(strImgpath.c_str());
  522. Image Img(strtmp, useEmbeddedColorManagement);
  523. SysFreeString(strtmp);
  524. #endif
  525. DWORD dwZoomPix;
  526. int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
  527. if (retval == ZoomOut)
  528. {
  529. return FALSE;
  530. }
  531. else if (retval == ZoomNull)
  532. {
  533. return SaveImg2newfile(&Img, strNewfile, uQuality);
  534. }
  535. // 绘制缩略图;
  536. Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  537. Graphics *graphic = Graphics::FromImage(&bitmap);
  538. //graphic->Clear(Color(255, 255, 255, 255));//如果png包含有透明区域,会无效;
  539. // 设置缩放或旋转图片时的算法(图片质量);
  540. graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
  541. // 设置渲染图形对象的质量;
  542. graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
  543. // 设置图形对象的像素偏移模式;
  544. graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
  545. // 设置图形对象的合成模式;
  546. graphic->SetCompositingMode(CompositingModeSourceOver);
  547. // 设置图形对象的合成质量;
  548. graphic->SetCompositingQuality(CompositingQualityHighQuality);
  549. // 设置图形对象的文本渲染模式;
  550. graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
  551. Status st = graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  552. if ( st != Ok )
  553. {
  554. delete graphic;
  555. return FALSE;
  556. }
  557. delete graphic;
  558. // 生成缩略图;
  559. return SaveImg2newfile(&bitmap, strNewfile, uQuality);
  560. }
  561. BOOL IMGCommon::SimpleImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality, IN BOOL useEmbeddedColorManagement /* = FALSE */)
  562. {
  563. if (!PathFileExists(lpImgpath))
  564. {
  565. OutputDebugString(_T("文件不存在:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
  566. return FALSE;
  567. }
  568. DWORD dwZoomPix;
  569. Image *pImg = NULL;
  570. LoadImgFromFile(&pImg, lpImgpath, useEmbeddedColorManagement);
  571. if ( pImg == NULL )
  572. {
  573. OutputDebugString(_T("加载文件失败:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
  574. return FALSE;
  575. }
  576. int retval = ZoomImg(SET_PIX(pImg->GetWidth(), pImg->GetHeight()), dwDisplayPix, dwZoomPix);
  577. if (retval == ZoomOut)
  578. {
  579. if (pImg) delete pImg;
  580. OutputDebugString(_T("缩放无效:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
  581. return FALSE;
  582. }
  583. else if (retval == ZoomNull)
  584. {
  585. BOOL bRet = SaveImg2newfile(pImg, strNewfile, uQuality);
  586. if (pImg) delete pImg;
  587. OutputDebugString(_T("缩放无效:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
  588. return bRet;
  589. }
  590. Image *pThumbnailImg = pImg->GetThumbnailImage(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  591. if (pThumbnailImg)
  592. {
  593. // 提高缩略图质量;
  594. Graphics graphic(pThumbnailImg);
  595. graphic.DrawImage(pImg, 0, 0, pThumbnailImg->GetWidth(), pThumbnailImg->GetHeight());
  596. INT nRientation = GetOrientation(pImg);
  597. if ( nRientation == 8 )
  598. {
  599. pThumbnailImg->RotateFlip(Rotate270FlipNone);
  600. }
  601. else if (nRientation == 6)
  602. {
  603. pThumbnailImg->RotateFlip(Rotate90FlipNone);
  604. }
  605. SaveImg2newfile(pThumbnailImg, strNewfile, uQuality);
  606. }
  607. else
  608. {
  609. if (pImg) delete pImg;
  610. OutputDebugString(_T("无效缩略图:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
  611. return FALSE;
  612. }
  613. if (pImg) delete pImg;
  614. if (pThumbnailImg) delete pThumbnailImg;
  615. return TRUE;
  616. }
  617. #ifdef _WATER_MARK_
  618. Image* IMGCommon::AppendImgWatermark(IN Image* pImg, IN Image* pWatemarkImg)
  619. {
  620. if ( pImg == NULL || pWatemarkImg == NULL )
  621. return NULL;
  622. // Status st = GenericError;
  623. // 创建图像显示属性对象;
  624. ImageAttributes ImgAttr;
  625. // 创建新位图;
  626. Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
  627. // 创建水印图显示位置;
  628. RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
  629. // 创建画布对象;
  630. Graphics *grap = Graphics::FromImage(pBitmap);
  631. // 绘制画布底色;
  632. if ( Ok != grap->Clear(Color(255,255,255,255)) )
  633. goto clear;
  634. // 设置图片显示质量;
  635. if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
  636. goto clear;
  637. // 设置文本质量;
  638. if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
  639. goto clear;
  640. // 在画布上画原始图片;
  641. if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
  642. goto clear;
  643. // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
  644. if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
  645. goto clear;
  646. // 设置水印图片透明底为0.3;
  647. if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
  648. goto clear;
  649. // 在画布左上角画水印;
  650. if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
  651. goto clear;
  652. if ( grap )
  653. delete grap;
  654. return pBitmap;
  655. clear:
  656. if ( grap )
  657. delete grap;
  658. if ( pBitmap )
  659. delete pBitmap;
  660. return NULL;
  661. }
  662. Image* IMGCommon::AppendImgWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkImgpath)
  663. {
  664. if ( !PathFileExists(lpWatemarkImgpath) || pImg == NULL )
  665. return FALSE;
  666. #ifdef UNICODE
  667. Image *pWatemarkImg = Image::FromFile(lpWatemarkImgpath);
  668. #else
  669. BSTR str = _bstr_t(lpWatemarkImgpath);
  670. Image* pWatemarkImg = Image::FromFile(str);
  671. SysFreeString(str);
  672. #endif
  673. if ( pImg == NULL )
  674. return NULL;
  675. //Status st = GenericError;
  676. // 创建图像显示属性对象;
  677. ImageAttributes ImgAttr;
  678. // 创建新位图;
  679. Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
  680. // 创建水印图显示位置;
  681. RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
  682. // 创建画布对象;
  683. Graphics *grap = Graphics::FromImage(pBitmap);
  684. // 绘制画布底色;
  685. if ( Ok != grap->Clear(Color(255,255,255,255)) )
  686. goto clear;
  687. // 设置图片显示质量;
  688. if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
  689. goto clear;
  690. // 设置文本质量;
  691. if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
  692. goto clear;
  693. // 在画布上画原始图片;
  694. if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
  695. goto clear;
  696. // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
  697. if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
  698. goto clear;
  699. // 设置水印图片透明底为0.3;
  700. if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
  701. goto clear;
  702. // 在画布左上角画水印;
  703. if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
  704. goto clear;
  705. if ( grap )
  706. delete grap;
  707. return pBitmap;
  708. clear:
  709. if ( grap )
  710. delete grap;
  711. if ( pBitmap )
  712. delete pBitmap;
  713. if ( pWatemarkImg )
  714. delete pWatemarkImg;
  715. return NULL;
  716. }
  717. Image* IMGCommon::AppendImgWatermark(IN LPCTSTR lpImgpath, IN Image* pWatemarkImg)
  718. {
  719. if ( !PathFileExists(lpImgpath) || pWatemarkImg == NULL )
  720. return FALSE;
  721. #ifdef UNICODE
  722. Image *pImg = Image::FromFile(lpImgpath);
  723. #else
  724. BSTR str = _bstr_t(lpImgpath);
  725. Image* pImg = Image::FromFile(str);
  726. SysFreeString(str);
  727. #endif
  728. if ( pImg == NULL )
  729. return NULL;
  730. //Status st = GenericError;
  731. // 创建图像显示属性对象;
  732. ImageAttributes ImgAttr;
  733. // 创建新位图;
  734. Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
  735. // 创建水印图显示位置;
  736. RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
  737. // 创建画布对象;
  738. Graphics *grap = Graphics::FromImage(pBitmap);
  739. // 绘制画布底色;
  740. if ( Ok != grap->Clear(Color(255,255,255,255)) )
  741. goto clear;
  742. // 设置图片显示质量;
  743. if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
  744. goto clear;
  745. // 设置文本质量;
  746. if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
  747. goto clear;
  748. // 在画布上画原始图片;
  749. if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
  750. goto clear;
  751. // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
  752. if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
  753. goto clear;
  754. // 设置水印图片透明底为0.3;
  755. if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
  756. goto clear;
  757. // 在画布左上角画水印;
  758. if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
  759. goto clear;
  760. if ( grap )
  761. delete grap;
  762. return pBitmap;
  763. clear:
  764. if ( grap )
  765. delete grap;
  766. if ( pBitmap )
  767. delete pBitmap;
  768. if ( pImg )
  769. delete pImg;
  770. return NULL;
  771. }
  772. Image* IMGCommon::AppendImgWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkImgpath)
  773. {
  774. if ( !PathFileExists(lpImgpath) || !PathFileExists(lpWatemarkImgpath) )
  775. return FALSE;
  776. #ifdef UNICODE
  777. Image *pImg = Image::FromFile(lpImgpath);
  778. Image *pWatemarkImg = Image::FromFile(lpWatemarkImgpath);
  779. #else
  780. BSTR str = _bstr_t(lpImgpath);
  781. Image* pImg = Image::FromFile(str);
  782. SysFreeString(str);
  783. BSTR str2 = _bstr_t(lpWatemarkImgpath);
  784. Image* pWatemarkImg = Image::FromFile(str2);
  785. SysFreeString(str2);
  786. #endif
  787. if ( pImg == NULL || pWatemarkImg == NULL )
  788. return NULL;
  789. //Status st = GenericError;
  790. // 创建图像显示属性对象;
  791. ImageAttributes ImgAttr;
  792. // 创建新位图;
  793. Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
  794. // 创建水印图显示位置;
  795. RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
  796. // 创建画布对象;
  797. Graphics *grap = Graphics::FromImage(pBitmap);
  798. // 绘制画布底色;
  799. if ( Ok != grap->Clear(Color(255,255,255,255)) )
  800. goto clear;
  801. // 设置图片显示质量;
  802. if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
  803. goto clear;
  804. // 设置文本质量;
  805. if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
  806. goto clear;
  807. // 在画布上画原始图片;
  808. if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
  809. goto clear;
  810. // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
  811. if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
  812. goto clear;
  813. // 设置水印图片透明底为0.3;
  814. if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
  815. goto clear;
  816. // 在画布左上角画水印;
  817. if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
  818. goto clear;
  819. if ( grap )
  820. delete grap;
  821. return pBitmap;
  822. clear:
  823. if ( grap )
  824. delete grap;
  825. if ( pBitmap )
  826. delete pBitmap;
  827. if ( pImg )
  828. delete pImg;
  829. if ( pWatemarkImg )
  830. delete pWatemarkImg;
  831. return NULL;
  832. }
  833. BOOL IMGCommon::AppendImgWatermark(IN Image* pImg, IN Image* pWatemarkImg, IN LPCTSTR lpSaveImgpath)
  834. {
  835. Image* pSaveImg = AppendImgWatermark(pImg, pWatemarkImg);
  836. if ( pSaveImg == NULL )
  837. return FALSE;
  838. // 创建新的目录;
  839. CString strImgs = lpSaveImgpath;
  840. INT nIndex = strImgs.ReverseFind(_T('\\'));
  841. if ( nIndex != -1 )
  842. {
  843. strImgs = strImgs.Mid(0,nIndex+1);
  844. if ( !PathFileExists(strImgs) )
  845. {
  846. INT nRet = SHCreateDirectoryEx(NULL, strImgs, NULL);
  847. if ( ERROR_SUCCESS == nRet || ERROR_ALREADY_EXISTS == nRet )
  848. {
  849. if (SaveImg2newfile(pSaveImg, (CString)lpSaveImgpath, 100))
  850. {
  851. if ( pSaveImg )
  852. delete pSaveImg;
  853. pSaveImg = NULL;
  854. return TRUE;
  855. }
  856. return FALSE;
  857. }
  858. }
  859. }
  860. return FALSE;
  861. }
  862. BOOL IMGCommon::AppendImgWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkImgpath, IN LPCTSTR lpSaveImgpath)
  863. {
  864. Image *pSaveImg = AppendImgWatermark(lpImgpath, lpWatemarkImgpath);
  865. if ( pSaveImg == NULL)
  866. return FALSE;
  867. // 创建新的目录;
  868. CString strImgs = lpSaveImgpath;
  869. INT nIndex = strImgs.ReverseFind(_T('\\'));
  870. if ( nIndex != -1 )
  871. {
  872. strImgs = strImgs.Mid(0,nIndex+1);
  873. if ( !PathFileExists(strImgs) )
  874. {
  875. INT nRet = SHCreateDirectoryEx(NULL, strImgs, NULL);
  876. if ( ERROR_SUCCESS == nRet || ERROR_ALREADY_EXISTS == nRet )
  877. {
  878. if (SaveImg2newfile(pSaveImg, (CString)lpSaveImgpath, 100))
  879. {
  880. if ( pSaveImg )
  881. delete pSaveImg;
  882. pSaveImg = NULL;
  883. return TRUE;
  884. }
  885. return FALSE;
  886. }
  887. }
  888. }
  889. return FALSE;
  890. }
  891. Image* IMGCommon::AppendWordWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkString)
  892. {
  893. if ( pImg == NULL || lpWatemarkString == NULL || lpWatemarkString[0] == _T('\0'))
  894. return NULL;
  895. #ifndef UNICODE
  896. WCHAR* pText = StringProcess::ascii2unicode(lpWatemarkString);
  897. #endif
  898. // 创建新位图;
  899. Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
  900. // 创建字体;
  901. FontFamily ffly(L"宋体");
  902. Gdiplus::Font ft(&ffly, 24, FontStyleRegular, UnitPixel);
  903. // 创建画刷;
  904. SolidBrush brush(0x99000000);
  905. // 创建文本格式;
  906. StringFormat sf;
  907. sf.SetAlignment(StringAlignmentCenter); // 水平居中;
  908. // 创建水印文本显示位置;
  909. RectF layWatemarkRect(pImg->GetWidth(), pImg->GetHeight() - 25, 0.0, 0.0); // 文字阴影;
  910. RectF layWatemarkRect2(pImg->GetWidth(), pImg->GetHeight() - 26, 0.0, 0.0); // 文字原体;
  911. // 创建画布对象;
  912. Graphics *grap = Graphics::FromImage(pBitmap);
  913. // 绘制画布底色;
  914. if ( Ok != grap->Clear(Color(255,255,255,255)) )
  915. goto clear;
  916. // 设置图片显示质量;
  917. if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
  918. goto clear;
  919. // 设置文本质量;
  920. if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
  921. goto clear;
  922. // 在画布上画原始图片;
  923. if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
  924. goto clear;
  925. // 画水印阴影;
  926. #ifdef UNICODE
  927. if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect, &sf, &brush) )
  928. #else
  929. if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
  930. #endif
  931. goto clear;
  932. // 画水印文本;
  933. brush.SetColor(0x99ffffff);
  934. #ifdef UNICODE
  935. if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect2, &sf, &brush) )
  936. #else
  937. if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
  938. #endif
  939. goto clear;
  940. if ( grap )
  941. delete grap;
  942. return pBitmap;
  943. clear:
  944. if ( grap )
  945. delete grap;
  946. if ( pBitmap )
  947. delete pBitmap;
  948. if ( pImg )
  949. delete pImg;
  950. #ifndef UNICODE
  951. if (pText)
  952. delete []pText;
  953. #endif
  954. return NULL;
  955. }
  956. Image* IMGCommon::AppendWordWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkString)
  957. {
  958. if ( !PathFileExists(lpImgpath) || lpWatemarkString == NULL || lpWatemarkString[0] == _T('\0'))
  959. return NULL;
  960. #ifdef UNICODE
  961. Image *pImg = Image::FromFile(lpImgpath);
  962. #else
  963. BSTR str = _bstr_t(lpImgpath);
  964. Image* pImg = Image::FromFile(str);
  965. SysFreeString(str);
  966. #endif
  967. #ifndef UNICODE
  968. WCHAR* pText = StringProcess::ascii2unicode(lpWatemarkString);
  969. #endif
  970. // 创建新位图;
  971. Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
  972. // 创建字体;
  973. FontFamily ffly(L"宋体");
  974. Gdiplus::Font ft(&ffly, 24, FontStyleRegular, UnitPixel);
  975. // 创建画刷;
  976. SolidBrush brush(0x99000000);
  977. // 创建文本格式;
  978. StringFormat sf;
  979. sf.SetAlignment(StringAlignmentCenter); // 水平居中;
  980. // 创建水印文本显示位置;
  981. RectF layWatemarkRect(pImg->GetWidth(), pImg->GetHeight() - 25, 0.0, 0.0); // 文字阴影;
  982. RectF layWatemarkRect2(pImg->GetWidth(), pImg->GetHeight() - 26, 0.0, 0.0); // 文字原体;
  983. // 创建画布对象;
  984. Graphics *grap = Graphics::FromImage(pBitmap);
  985. // 绘制画布底色;
  986. if ( Ok != grap->Clear(Color(255,255,255,255)) )
  987. goto clear;
  988. // 设置图片显示质量;
  989. if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
  990. goto clear;
  991. // 设置文本质量;
  992. if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
  993. goto clear;
  994. // 在画布上画原始图片;
  995. if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
  996. goto clear;
  997. // 画水印阴影;
  998. #ifdef UNICODE
  999. if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect, &sf, &brush) )
  1000. #else
  1001. if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
  1002. #endif
  1003. goto clear;
  1004. // 画水印文本;
  1005. brush.SetColor(0x99ffffff);
  1006. #ifdef UNICODE
  1007. if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect2, &sf, &brush) )
  1008. #else
  1009. if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
  1010. #endif
  1011. goto clear;
  1012. if ( grap )
  1013. delete grap;
  1014. return pBitmap;
  1015. clear:
  1016. if ( grap )
  1017. delete grap;
  1018. if ( pBitmap )
  1019. delete pBitmap;
  1020. if ( pImg )
  1021. delete pImg;
  1022. #ifndef UNICODE
  1023. if (pText)
  1024. delete []pText;
  1025. #endif
  1026. return NULL;
  1027. }
  1028. BOOL IMGCommon::SaveBitmap(IN CDC *pDC, IN CBitmap* pBitmap, IN CONST INT& nBitdepth, IN const INT& nWidth, IN const INT& nHeight, IN LPCTSTR lpSavepath)
  1029. {
  1030. // 计算出位图大小 = ((宽 * 位宽 + 31)/ 32)* 4 * 高;
  1031. DWORD dwBmpSize = ((nWidth*nBitdepth + 31) / 32) * 4 * nHeight;
  1032. HANDLE hFile = CreateFile(lpSavepath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1033. if (hFile == INVALID_HANDLE_VALUE)
  1034. {
  1035. //WriteLog(_T("二维码位图创建失败"));
  1036. return FALSE;
  1037. }
  1038. HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE | SEC_COMMIT, 0,
  1039. sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + dwBmpSize, NULL);
  1040. LPBYTE lpbyMapView = (LPBYTE)MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
  1041. ZeroMemory(lpbyMapView, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + dwBmpSize);
  1042. //结构体地址割付;
  1043. LPBITMAPFILEHEADER pbfh = (LPBITMAPFILEHEADER)lpbyMapView;
  1044. LPBITMAPINFO pbmi = (LPBITMAPINFO)(lpbyMapView + sizeof(BITMAPFILEHEADER));
  1045. LPVOID pbdt = lpbyMapView + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2;
  1046. // BITMAPFILEHEADER
  1047. // pbfh->bfType = (WORD) (('M' << 8) | 'B'); // "BM"
  1048. // BMP类型,与上面同值;
  1049. pbfh->bfType = 0x4D42;
  1050. pbfh->bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + dwBmpSize; // 文件总长;
  1051. pbfh->bfReserved1 = 0;
  1052. pbfh->bfReserved2 = 0;
  1053. pbfh->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2; // 位图数据相对于文件头的偏移量;
  1054. // LPBITMAPINFO->BITMAPINFOHEADER
  1055. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  1056. pbmi->bmiHeader.biWidth = nWidth;
  1057. pbmi->bmiHeader.biHeight = nHeight;
  1058. pbmi->bmiHeader.biPlanes = 1;
  1059. pbmi->bmiHeader.biBitCount = nBitdepth; // 位深度 1;
  1060. pbmi->bmiHeader.biCompression = BI_RGB;
  1061. pbmi->bmiHeader.biSizeImage = dwBmpSize;
  1062. pbmi->bmiHeader.biXPelsPerMeter = 3780;
  1063. pbmi->bmiHeader.biYPelsPerMeter = 3780;
  1064. pbmi->bmiHeader.biClrUsed = 0;
  1065. pbmi->bmiHeader.biClrImportant = 0;
  1066. // 放大到指定的像素宽高;
  1067. CDC* pWorkDC = new CDC;
  1068. pWorkDC->CreateCompatibleDC(NULL);
  1069. CBitmap* pWorkBitmap = new CBitmap;
  1070. pWorkBitmap->CreateBitmap(nWidth, nHeight, 1, 1, NULL);
  1071. CBitmap* pOldBitmap = pWorkDC->SelectObject(pWorkBitmap);
  1072. pWorkDC->StretchBlt(0, 0, nWidth, nHeight, pDC, 0, 0, nWidth, nHeight, SRCCOPY);
  1073. GetDIBits(pWorkDC->m_hDC, (HBITMAP)*pWorkBitmap, 0, nHeight, pbdt, pbmi, DIB_RGB_COLORS);
  1074. pWorkDC->SelectObject(pOldBitmap);
  1075. delete pWorkBitmap;
  1076. delete pWorkDC;
  1077. UnmapViewOfFile(lpbyMapView);
  1078. CloseHandle(hFileMapping);
  1079. CloseHandle(hFile);
  1080. return TRUE;
  1081. }
  1082. /************************************************************************/
  1083. /* 函数:SaveBitmapEx[10/17/2016 IT];
  1084. /* 描述:将CDC上的位图保存成文件;
  1085. /* 参数:;
  1086. /* [IN] :;
  1087. /* [OUT] :;
  1088. /* [IN/OUT] :;
  1089. /* 返回:void;
  1090. /* 注意:位图大小 = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 调色板大小(16、24、32位深的调色板大小为0) + 位图数据大小;
  1091. /* 示例:;
  1092. /*
  1093. /* 修改:;
  1094. /* 日期:;
  1095. /* 内容:;
  1096. /************************************************************************/
  1097. BOOL IMGCommon::SaveBitmapEx(IN CDC *pDC, IN CBitmap* pBitmap, IN CONST INT& nBitdepth, IN const INT& nWidth, IN const INT& nHeight, IN LPCTSTR lpSavepath)
  1098. {
  1099. DWORD dwPaletteSize = 0; // 调色板大小;
  1100. DWORD dwBmBitsSize = 0; // 位图中像素字节大小;
  1101. DWORD dwDIBSize = 0; // 位图文件大小;
  1102. WORD wBitsCount = 0; // 当前显示分辨率下每个像素所占字节数;
  1103. if( nBitdepth <= 1 )
  1104. wBitsCount = 1;
  1105. else if( nBitdepth <= 4 )
  1106. wBitsCount = 4;
  1107. else if( nBitdepth <= 8 )
  1108. wBitsCount = 8;
  1109. else if( nBitdepth <= 16 )
  1110. wBitsCount = 16;
  1111. else if( nBitdepth <= 24 )
  1112. wBitsCount = 24;
  1113. else
  1114. wBitsCount = 32;
  1115. // 计算出调色板大小;
  1116. if( wBitsCount <= 8 )
  1117. dwPaletteSize = (1 << wBitsCount) * sizeof(RGBQUAD);
  1118. // 计算出位图数据大小 = ((宽 * 位宽 + 31)/ 32)* 4 * 高;
  1119. dwBmBitsSize = ((nWidth*wBitsCount + 31) / 8) * nHeight;
  1120. // 计算出位图文件大小;
  1121. dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
  1122. //////////////////////////////////////////////////////////////////////////
  1123. // 根据新位图来创建位图文件;
  1124. HANDLE hFile = CreateFile(lpSavepath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1125. if (hFile == INVALID_HANDLE_VALUE)
  1126. {
  1127. return FALSE;
  1128. }
  1129. HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, dwDIBSize, NULL);
  1130. LPBYTE lpbyMapView = (LPBYTE)MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
  1131. ZeroMemory(lpbyMapView, dwDIBSize);
  1132. //结构体地址割付;
  1133. LPBITMAPFILEHEADER pbfh = (LPBITMAPFILEHEADER)lpbyMapView;
  1134. LPBITMAPINFO pbmi = (LPBITMAPINFO)(lpbyMapView + sizeof(BITMAPFILEHEADER));
  1135. LPVOID pbdt = lpbyMapView + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize;
  1136. // BITMAPFILEHEADER
  1137. // pbfh->bfType = (WORD) (('M' << 8) | 'B'); // "BM"
  1138. // BMP类型,与上面同值;
  1139. pbfh->bfType = 0x4D42;
  1140. pbfh->bfSize = dwDIBSize; // 文件总长;
  1141. pbfh->bfReserved1 = 0;
  1142. pbfh->bfReserved2 = 0;
  1143. pbfh->bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize; // 位图数据相对于文件头的偏移量;
  1144. // LPBITMAPINFO->BITMAPINFOHEADER
  1145. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  1146. pbmi->bmiHeader.biWidth = nWidth;
  1147. pbmi->bmiHeader.biHeight = nHeight;
  1148. pbmi->bmiHeader.biPlanes = 1;
  1149. pbmi->bmiHeader.biBitCount = wBitsCount; // 位深度;
  1150. pbmi->bmiHeader.biCompression = BI_RGB;
  1151. pbmi->bmiHeader.biSizeImage = dwBmBitsSize; // 位图数据大小;
  1152. pbmi->bmiHeader.biXPelsPerMeter = 3780;
  1153. pbmi->bmiHeader.biYPelsPerMeter = 3780;
  1154. pbmi->bmiHeader.biClrUsed = 0;
  1155. pbmi->bmiHeader.biClrImportant = 0;
  1156. //////////////////////////////////////////////////////////////////////////
  1157. // 拉伸到指定的像素宽高;
  1158. CDC* pWorkDC = new CDC;
  1159. pWorkDC->CreateCompatibleDC(NULL);
  1160. CBitmap* pWorkBitmap = new CBitmap;
  1161. pWorkBitmap->CreateBitmap(nWidth, nHeight, 1, 1, NULL);
  1162. CBitmap* pOldBitmap = pWorkDC->SelectObject(pWorkBitmap);
  1163. pWorkDC->StretchBlt(0, 0, nWidth, nHeight, pDC, 0, 0, nWidth, nHeight, SRCCOPY);
  1164. GetDIBits(pWorkDC->GetSafeHdc(), (HBITMAP)*pWorkBitmap, 0, nHeight, pbdt, pbmi, DIB_RGB_COLORS);
  1165. pWorkDC->SelectObject(pOldBitmap);
  1166. delete pWorkBitmap;
  1167. delete pWorkDC;
  1168. UnmapViewOfFile(lpbyMapView);
  1169. CloseHandle(hFileMapping);
  1170. CloseHandle(hFile);
  1171. return TRUE;
  1172. }
  1173. void IMGCommon::DrawString(IN CDC* pdc, IN CONST INT& nFontSize, IN LPCTSTR lpFontName, IN LPCTSTR lpString, IN OUT CRect &rcString)
  1174. {
  1175. if ( !pdc || !lpString )
  1176. return;
  1177. LOGFONT log;
  1178. // 设置字体字号;
  1179. log.lfHeight = -MulDiv(nFontSize ? nFontSize : 9, GetDeviceCaps(pdc->GetSafeHdc(), LOGPIXELSY), 72);
  1180. log.lfWidth = 0;
  1181. log.lfEscapement = 0;
  1182. log.lfOrientation = 0;
  1183. log.lfWeight = FW_REGULAR;
  1184. log.lfItalic = false;
  1185. log.lfUnderline = false;
  1186. log.lfStrikeOut = 0;
  1187. log.lfCharSet = ANSI_CHARSET;
  1188. log.lfOutPrecision = OUT_DEFAULT_PRECIS;
  1189. log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  1190. log.lfQuality = DEFAULT_QUALITY;
  1191. log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
  1192. // 设置字体名称;
  1193. _tcscpy_s(log.lfFaceName, (lpFontName && (lpFontName[0] != _T('\0'))) ? lpFontName : _T("Arial"));
  1194. CFont cf;
  1195. cf.CreateFontIndirect(&log);
  1196. CFont *oldf = pdc->SelectObject(&cf);
  1197. SIZE Size;
  1198. GetTextExtentPoint32(pdc->GetSafeHdc(), lpString, _tcslen(lpString), &Size);
  1199. rcString.SetRect(rcString.left, rcString.top, Size.cx + rcString.left, Size.cy + rcString.top);
  1200. pdc->TextOut(rcString.left, rcString.top, lpString);
  1201. }
  1202. #endif
  1203. HICON IMGCommon::GetFileIcon(IN const CString& strFilePath, IN BOOL bLarge)
  1204. {
  1205. SHFILEINFO SHFI;
  1206. ZeroMemory(&SHFI, sizeof(SHFI));
  1207. DWORD_PTR ret = ::SHGetFileInfo(strFilePath, 0, &SHFI, sizeof(SHFI),
  1208. SHGFI_ICON | (bLarge ? SHGFI_LARGEICON : SHGFI_SMALLICON));
  1209. if (ret != 0)
  1210. {
  1211. return SHFI.hIcon;
  1212. }
  1213. return NULL;
  1214. }