IMGCommon.cpp 39 KB

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