123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425 |
- #include "StdAfx.h"
- #include "IMGCommon.h"
- #include <strsafe.h>
- #include <Shlwapi.h>
- // _bstr_t头文件;
- #include <comutil.h>
- #pragma comment(lib, "comsuppw.lib")
- const ColorMatrix _ClrMatrix =
- {
- 1.0, 0.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0, 0.0,
- 0.0, 0.0, 0.3, 0.0, 0.0,
- 0.0, 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 0.0, 0.0, 1.0,
- };
- ULONG_PTR IMGCommon::m_gdiplusToken;
- IMGCommon::IMGCommon(void)
- {
- GdiplusStartupInput gdiplusStartupInput;
- GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- }
- IMGCommon::~IMGCommon(void)
- {
- Gdiplus::GdiplusShutdown(m_gdiplusToken);
- }
- /************************************************************************/
- /* 函数:[4/1/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL IMGCommon::LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath, IN BOOL useEmbeddedColorManagement /* = FALSE */)
- {
- if ( !PathFileExists(lpPath) )
- return FALSE;
- if ( *pImg )
- delete *pImg;
- *pImg = NULL;
- #ifdef UNICODE
- *pImg = Image::FromFile(lpPath, TRUE);
- #else
- BSTR strtmp = _bstr_t(lpPath);
- *pImg = Image::FromFile(strtmp, TRUE);
- SysFreeString(strtmp);
- #endif
- return (*pImg ? TRUE : FALSE);
- }
- BOOL IMGCommon::LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen)
- {
- if ( pBuffer == NULL )
- return FALSE;
- if ( *pImg )
- delete *pImg;
- *pImg = NULL;
- HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
- if ( hMemery == NULL )
- return FALSE;
- BYTE *pMem = (BYTE*)GlobalLock(hMemery);
- memcpy(pMem, pBuffer, nBufLen);
- IStream *pstream = NULL;
- CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
- *pImg = Image::FromStream(pstream);
- GlobalUnlock(hMemery);
- pstream->Release();
- return (*pImg ? TRUE : FALSE);
- }
- // 先以只读方式从文件中读取二进制流出来,可以做到不独占文件;
- BOOL IMGCommon::LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath, IN BOOL useEmbeddedColorManagement /* = FALSE */)
- {
- if ( !PathFileExists(lpPath) )
- return FALSE;
- if ( *pImg )
- delete *pImg;
- *pImg = NULL;
- CFile fp;
- CFileException e;
- BOOL bRet = FALSE;
- if ( fp.Open(lpPath, CFile::modeRead, &e))
- {
- DWORD dwLength = (DWORD)fp.GetLength();
- BYTE *pData = new BYTE[dwLength];
- fp.Read(pData,dwLength);
- fp.Close();
- bRet = LoadImgFromBuffer(pImg, pData, dwLength);
- if( pData )
- delete []pData;
- }
- return bRet;
- }
- /************************************************************************/
- /* 函数:LoadImgFromResource[9/21/2016 IT];
- /* 描述:从资源中加载;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- Image* IMGCommon::LoadImgFromResource(IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType)
- {
- HGLOBAL hGlobal = NULL;
- HRSRC hSource = NULL;
- LPVOID lpBuffer = NULL;
- DWORD dwSize = 0;
- // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可
- hSource = FindResource(hModule, lpName, lpType);
- if (hSource == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // 2.获取资源的大小;
- dwSize = (UINT)SizeofResource(NULL, hSource);
- // 3.加载资源;
- hGlobal = LoadResource(NULL, hSource);
- if (hGlobal == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // 4.锁定资源,获取buffer;
- lpBuffer = LockResource(hGlobal);
- if (lpBuffer == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // lpFileFullName需要先判断文件是否存在??不需要;
- Image *pImg = NULL;
- LoadImgFromBuffer(&pImg, (BYTE*)lpBuffer, dwSize);
- UnlockResource(hGlobal);
- FreeResource(hGlobal);
- return pImg;
- }
- BOOL IMGCommon::GetOrientation(IN Image *pImg)
- {
- if (pImg == NULL) return FALSE;
- UINT totalBufferSize;
- UINT numProperties;
- pImg->GetPropertySize(&totalBufferSize, &numProperties);
- // Allocate the buffer that will receive the property items.
- PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
- // Fill the buffer.
- pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
- // Print the id data member of each property item.
- for (UINT j = 0; j < numProperties; ++j)
- {
- if (PropertyTagOrientation == pAllItems[j].id)
- {
- short* ptrLong = (short*)(pAllItems[j].value);
- int ret = (int)*ptrLong;
- free(pAllItems);
- return ret;
- }
- }
- free(pAllItems);
- return TRUE;
- }
- /************************************************************************/
- /* 函数:GetEncoderClsid[4/1/2017 Jeff];
- /* 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
- /* 参数:;
- /* [IN] format:要获取的图像格式;
- /* [OUT] pClsid:返回符合条件的图像编码器信息;
- /* [IN/OUT] :;
- /* 返回:成功返回编码器索引,否则返回-1;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int IMGCommon::GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
- {
- // GDI+支持的图像编码器数量;
- UINT numEncoders = 0;
- // GDI+所有图像格式编码器详细信息所需要的空间大小;
- UINT nSize = 0;
- ImageCodecInfo* pImageCodecInfo = NULL;
- // 2.获取GDI+支持的所有图像格式编码器详细信息所需要的空间大小;
- GetImageEncodersSize(&numEncoders, &nSize);
- if (nSize == 0)
- return -1;
- //3.为ImageCodecInfo数组分配足额空间;
- pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
- if (pImageCodecInfo == NULL)
- return -1;
- //4.获取所有的图像编码器信息;
- GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
- //5.查找符合的图像编码器的Clsid;
- for (UINT i = 0; i < numEncoders; ++i)
- {
- if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[i].Clsid;
- free(pImageCodecInfo);
- return i; // Success
- }
- }
- //6.释放步骤3分配的内存;
- free(pImageCodecInfo);
- return -1;
- }
- /************************************************************************/
- /* 函数:SaveImg2newfile[4/1/2017 Jeff];
- /* 描述:将Image对象另存为其他格式的文件;
- /* 参数:;
- /* [IN] pImg:GDI+ Image对象指针,需要保存的图像对象;
- /* [IN] lpnewfile:其他格式的新文件名(jpg,bmp,png,jpeg);
- /* [IN] uQuality:另存为新文件时的图像质量值;
- /* 返回:Save 若文件存在,会覆盖存在的文件;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL IMGCommon::SaveImg2newfile(IN Image* pImg, IN CString strNewfile, IN ULONG uQuality)
- {
- if (pImg == NULL) return FALSE;
- // 需要判断路径是否存在,不存在创建目录;
- int nIndex = strNewfile.ReverseFind(_T('\\'));
- if (nIndex == -1)
- return FALSE;
- if (!PathFileExists(strNewfile.Left(nIndex)))
- {
- // 如果文件夹不存在,创建;
- SHCreateDirectoryEx(NULL, strNewfile.Left(nIndex), NULL);
- }
- nIndex = strNewfile.ReverseFind(_T('.'));
- if (nIndex == -1)
- return FALSE;
- Status stat = GenericError;
- CLSID encoderClsid = { 0 };
- BSTR newfile = strNewfile.AllocSysString();
- strNewfile = strNewfile.Mid(nIndex + 1);
- if (strNewfile.CompareNoCase(_T("bmp")) == 0)
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else if (strNewfile.CompareNoCase(_T("png")) == 0)
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- encoderParameters.Parameter[0].Value = &uQuality;
- stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
- }
- SysFreeString(newfile);
- return stat == Ok ? TRUE : FALSE;
- }
- BOOL IMGCommon::SaveImg2newfile(IN Image* pImg, IN TString strNewfile, IN ULONG uQuality)
- {
- if (pImg == NULL) return FALSE;
- // 需要判断路径是否存在,不存在创建目录;
- int nIndex = strNewfile.find_last_of(_T('\\'));
- if (nIndex == TString::npos)
- return FALSE;
- if (!PathFileExists(strNewfile.substr(0, nIndex).c_str()))
- {
- // 如果文件夹不存在,创建;
- SHCreateDirectoryEx(NULL, strNewfile.substr(0, nIndex).c_str(), NULL);
- }
- nIndex = strNewfile.find_last_of(_T('.'));
- if (nIndex == TString::npos)
- return FALSE;
- Status stat = GenericError;
- CLSID encoderClsid = { 0 };
- BSTR newfile = _bstr_t(strNewfile.c_str());
- strNewfile = strNewfile.substr(nIndex + 1);
- if (strNewfile.find(_T("bmp")) == 0)
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else if (strNewfile.find(_T("png")) == 0)
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- encoderParameters.Parameter[0].Value = &uQuality;
- stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
- }
- SysFreeString(newfile);
- return stat == Ok ? TRUE : FALSE;
- }
- /************************************************************************/
- /* 函数:ZoomImg[4/1/2017 Jeff];
- /* 描述:缩放到指定大小的区域中,返回缩放后的尺寸;
- /* 参数:;
- /* [IN] Imgrc:图像的大小;
- /* [OUT] dwDisplayPix:图像要显示的区域的大小;
- /* [IN/OUT] dwZoomPix:图像在显示区域内缩放后的尺寸;
- /* 返回:void;
- /* 注意:dwZoomPix的尺寸必须在dwDisplayPix内;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int IMGCommon::ZoomImg(IN CRect &Imgrc, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
- {
- double fDisplayWidth = GET_XPIX(dwDisplayPix);
- double fDisplayHeight = GET_YPIX(dwDisplayPix);
- // 显示区域长宽比;
- double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
- // 图片长宽比;
- double fImgAspectRatio = ((double)Imgrc.Width()) / ((double)Imgrc.Height());
- double fZoomWidth;
- double fZoomHeight;
- if (fDisplayAspectRatio > fImgAspectRatio)
- {
- fZoomWidth = fDisplayHeight*fImgAspectRatio;
- fZoomHeight = fDisplayHeight;
- }
- else
- {
- fZoomWidth = fDisplayWidth;
- fZoomHeight = fDisplayWidth / fImgAspectRatio;
- }
- dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
- //////////////////////////////////////////////////////////////////////////
- int nRetval = 0;
- if ((fDisplayWidth == Imgrc.Width()) && (fDisplayHeight == Imgrc.Height()))
- {
- nRetval = ZoomNull;
- }
- else if ((fDisplayWidth > Imgrc.Width()) && (fDisplayHeight > Imgrc.Height()))
- {
- nRetval = ZoomOut;
- }
- else
- {
- nRetval = ZoomIn;
- }
- return nRetval;
- }
- int IMGCommon::ZoomImg(IN CONST DWORD &dwImgPix, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
- {
- double fDisplayWidth = GET_XPIX(dwDisplayPix);
- double fDisplayHeight = GET_YPIX(dwDisplayPix);
- // 显示区域长宽比;
- double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
- // 图片长宽比;
- double fImgAspectRatio = ((double)GET_XPIX(dwImgPix)) / ((double)GET_YPIX(dwImgPix));
- double fZoomWidth;
- double fZoomHeight;
- if (fDisplayAspectRatio > fImgAspectRatio)
- {
- fZoomWidth = fDisplayHeight*fImgAspectRatio;
- fZoomHeight = fDisplayHeight;
- }
- else
- {
- fZoomWidth = fDisplayWidth;
- fZoomHeight = fDisplayWidth / fImgAspectRatio;
- }
- dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
- //////////////////////////////////////////////////////////////////////////
- int nRetval = 0;
- if ((fDisplayWidth == GET_XPIX(dwImgPix)) && (fDisplayHeight == GET_YPIX(dwImgPix)))
- {
- nRetval = ZoomNull;
- }
- else if ((fDisplayWidth > GET_XPIX(dwImgPix)) && (fDisplayHeight > GET_YPIX(dwImgPix)))
- {
- nRetval = ZoomOut;
- }
- else
- {
- nRetval = ZoomIn;
- }
- return nRetval;
- }
- /************************************************************************/
- /* 函数:ImgThumbnail[4/1/2017 Jeff];
- /* 描述:生成高质量的图像缩略图;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:为了健壮性,strNewfile的路径可以在函数内再次判断是否有效,防止在函数外没有进行经判断;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL IMGCommon::ImgThumbnail(IN Image* pImg, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
- {
- if (pImg == NULL) return FALSE;
- DWORD dwZoomPix;
- int retval = ZoomImg(SET_PIX(pImg->GetWidth(), pImg->GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(pImg, strNewfile, uQuality);
- }
- // 绘制缩略图;
- Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- Graphics *graphic = Graphics::FromImage(&bitmap);
- //graphic->Clear(Color(255, 255, 255, 255));//如果png包含有透明区域,会无效;
- // 设置缩放或旋转图片时的算法(图片质量);
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- // 设置渲染图形对象的质量;
- graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
- // 设置图形对象的像素偏移模式;
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- // 设置图形对象的合成模式;
- graphic->SetCompositingMode(CompositingModeSourceOver);
- // 设置图形对象的合成质量;
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- // 设置图形对象的文本渲染模式;
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- graphic->DrawImage(pImg, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- delete graphic;
- // 生成缩略图;
- return SaveImg2newfile(&bitmap, strNewfile, uQuality);
- }
- BOOL IMGCommon::ImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality, IN BOOL useEmbeddedColorManagement /* = FALSE */)
- {
- if (!PathFileExists(lpImgpath))
- return FALSE;
- #ifdef UNICODE
- Image Img(lpImgpath, useEmbeddedColorManagement);
- #else
- BSTR strtmp = _bstr_t(lpImgpath);
- Image Img(strtmp,useEmbeddedColorManagement);
- SysFreeString(strtmp);
- #endif
- DWORD dwZoomPix;
- int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(&Img, strNewfile, uQuality);
- }
- // 绘制缩略图;
- Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- Graphics *graphic = Graphics::FromImage(&bitmap);
- //graphic->Clear(Color(255, 255, 255, 255));//如果png包含有透明区域,会无效;
- // 设置缩放或旋转图片时的算法(图片质量);
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- // 设置渲染图形对象的质量;
- graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
- // 设置图形对象的像素偏移模式;
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- // 设置图形对象的合成模式;
- graphic->SetCompositingMode(CompositingModeSourceOver);
- // 设置图形对象的合成质量;
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- // 设置图形对象的文本渲染模式;
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- Status st = graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- if ( st != Ok )
- {
- delete graphic;
- return FALSE;
- }
- delete graphic;
- // 生成缩略图;
- return SaveImg2newfile(&bitmap, strNewfile, uQuality);
- }
- BOOL IMGCommon::ImgThumbnail(IN CONST TString &strImgpath, IN CONST DWORD &dwDisplayPix, IN TString strNewfile, IN ULONG uQuality, IN BOOL useEmbeddedColorManagement /* = FALSE */)
- {
- if (!PathFileExists(strImgpath.c_str()))
- {
- SetLastError(GetLastError());
- return FALSE;
- }
- #ifdef UNICODE
- BSTR strtmp = _bstr_t(strImgpath.c_str());
- Image Img(strtmp, useEmbeddedColorManagement);
- SysFreeString(strtmp);
- //Image Img(strImgpath);
- #else
- BSTR strtmp = _bstr_t(strImgpath.c_str());
- Image Img(strtmp, useEmbeddedColorManagement);
- SysFreeString(strtmp);
- #endif
- DWORD dwZoomPix;
- int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(&Img, strNewfile, uQuality);
- }
- // 绘制缩略图;
- Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- Graphics *graphic = Graphics::FromImage(&bitmap);
- //graphic->Clear(Color(255, 255, 255, 255));//如果png包含有透明区域,会无效;
- // 设置缩放或旋转图片时的算法(图片质量);
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- // 设置渲染图形对象的质量;
- graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
- // 设置图形对象的像素偏移模式;
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- // 设置图形对象的合成模式;
- graphic->SetCompositingMode(CompositingModeSourceOver);
- // 设置图形对象的合成质量;
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- // 设置图形对象的文本渲染模式;
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- Status st = graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- if ( st != Ok )
- {
- delete graphic;
- return FALSE;
- }
- delete graphic;
- // 生成缩略图;
- return SaveImg2newfile(&bitmap, strNewfile, uQuality);
- }
- BOOL IMGCommon::SimpleImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality, IN BOOL useEmbeddedColorManagement /* = FALSE */)
- {
- if (!PathFileExists(lpImgpath))
- {
- OutputDebugString(_T("文件不存在:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
- return FALSE;
- }
- DWORD dwZoomPix;
- Image *pImg = NULL;
- LoadImgFromFile(&pImg, lpImgpath, useEmbeddedColorManagement);
- if ( pImg == NULL )
- {
- OutputDebugString(_T("加载文件失败:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
- return FALSE;
- }
- int retval = ZoomImg(SET_PIX(pImg->GetWidth(), pImg->GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- if (pImg) delete pImg;
- OutputDebugString(_T("缩放无效:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- BOOL bRet = SaveImg2newfile(pImg, strNewfile, uQuality);
- if (pImg) delete pImg;
- OutputDebugString(_T("缩放无效:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
- return bRet;
- }
- Image *pThumbnailImg = pImg->GetThumbnailImage(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- if (pThumbnailImg)
- {
- // 提高缩略图质量;
- Graphics graphic(pThumbnailImg);
- graphic.DrawImage(pImg, 0, 0, pThumbnailImg->GetWidth(), pThumbnailImg->GetHeight());
- INT nRientation = GetOrientation(pImg);
- if ( nRientation == 8 )
- {
- pThumbnailImg->RotateFlip(Rotate270FlipNone);
- }
- else if (nRientation == 6)
- {
- pThumbnailImg->RotateFlip(Rotate90FlipNone);
- }
- SaveImg2newfile(pThumbnailImg, strNewfile, uQuality);
- }
- else
- {
- if (pImg) delete pImg;
- OutputDebugString(_T("无效缩略图:"));OutputDebugString(lpImgpath);OutputDebugString(_T("\n"));
- return FALSE;
- }
- if (pImg) delete pImg;
- if (pThumbnailImg) delete pThumbnailImg;
- return TRUE;
- }
- #ifdef _WATER_MARK_
- Image* IMGCommon::AppendImgWatermark(IN Image* pImg, IN Image* pWatemarkImg)
- {
- if ( pImg == NULL || pWatemarkImg == NULL )
- return NULL;
- // Status st = GenericError;
- // 创建图像显示属性对象;
- ImageAttributes ImgAttr;
- // 创建新位图;
- Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
- // 创建水印图显示位置;
- RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
- // 创建画布对象;
- Graphics *grap = Graphics::FromImage(pBitmap);
- // 绘制画布底色;
- if ( Ok != grap->Clear(Color(255,255,255,255)) )
- goto clear;
- // 设置图片显示质量;
- if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
- goto clear;
- // 设置文本质量;
- if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
- goto clear;
- // 在画布上画原始图片;
- if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
- goto clear;
- // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
- if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
- goto clear;
- // 设置水印图片透明底为0.3;
- if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
- goto clear;
- // 在画布左上角画水印;
- if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
- goto clear;
- if ( grap )
- delete grap;
- return pBitmap;
- clear:
- if ( grap )
- delete grap;
- if ( pBitmap )
- delete pBitmap;
- return NULL;
- }
- Image* IMGCommon::AppendImgWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkImgpath)
- {
- if ( !PathFileExists(lpWatemarkImgpath) || pImg == NULL )
- return FALSE;
- #ifdef UNICODE
- Image *pWatemarkImg = Image::FromFile(lpWatemarkImgpath);
- #else
- BSTR str = _bstr_t(lpWatemarkImgpath);
- Image* pWatemarkImg = Image::FromFile(str);
- SysFreeString(str);
- #endif
- if ( pImg == NULL )
- return NULL;
- //Status st = GenericError;
- // 创建图像显示属性对象;
- ImageAttributes ImgAttr;
- // 创建新位图;
- Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
- // 创建水印图显示位置;
- RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
- // 创建画布对象;
- Graphics *grap = Graphics::FromImage(pBitmap);
- // 绘制画布底色;
- if ( Ok != grap->Clear(Color(255,255,255,255)) )
- goto clear;
- // 设置图片显示质量;
- if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
- goto clear;
- // 设置文本质量;
- if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
- goto clear;
- // 在画布上画原始图片;
- if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
- goto clear;
- // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
- if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
- goto clear;
- // 设置水印图片透明底为0.3;
- if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
- goto clear;
- // 在画布左上角画水印;
- if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
- goto clear;
- if ( grap )
- delete grap;
- return pBitmap;
- clear:
- if ( grap )
- delete grap;
- if ( pBitmap )
- delete pBitmap;
- if ( pWatemarkImg )
- delete pWatemarkImg;
- return NULL;
- }
- Image* IMGCommon::AppendImgWatermark(IN LPCTSTR lpImgpath, IN Image* pWatemarkImg)
- {
- if ( !PathFileExists(lpImgpath) || pWatemarkImg == NULL )
- return FALSE;
- #ifdef UNICODE
- Image *pImg = Image::FromFile(lpImgpath);
- #else
- BSTR str = _bstr_t(lpImgpath);
- Image* pImg = Image::FromFile(str);
- SysFreeString(str);
- #endif
- if ( pImg == NULL )
- return NULL;
- //Status st = GenericError;
- // 创建图像显示属性对象;
- ImageAttributes ImgAttr;
- // 创建新位图;
- Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
- // 创建水印图显示位置;
- RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
- // 创建画布对象;
- Graphics *grap = Graphics::FromImage(pBitmap);
- // 绘制画布底色;
- if ( Ok != grap->Clear(Color(255,255,255,255)) )
- goto clear;
- // 设置图片显示质量;
- if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
- goto clear;
- // 设置文本质量;
- if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
- goto clear;
- // 在画布上画原始图片;
- if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
- goto clear;
- // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
- if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
- goto clear;
- // 设置水印图片透明底为0.3;
- if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
- goto clear;
- // 在画布左上角画水印;
- if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
- goto clear;
- if ( grap )
- delete grap;
- return pBitmap;
- clear:
- if ( grap )
- delete grap;
- if ( pBitmap )
- delete pBitmap;
- if ( pImg )
- delete pImg;
- return NULL;
- }
- Image* IMGCommon::AppendImgWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkImgpath)
- {
- if ( !PathFileExists(lpImgpath) || !PathFileExists(lpWatemarkImgpath) )
- return FALSE;
- #ifdef UNICODE
- Image *pImg = Image::FromFile(lpImgpath);
- Image *pWatemarkImg = Image::FromFile(lpWatemarkImgpath);
- #else
- BSTR str = _bstr_t(lpImgpath);
- Image* pImg = Image::FromFile(str);
- SysFreeString(str);
- BSTR str2 = _bstr_t(lpWatemarkImgpath);
- Image* pWatemarkImg = Image::FromFile(str2);
- SysFreeString(str2);
- #endif
- if ( pImg == NULL || pWatemarkImg == NULL )
- return NULL;
- //Status st = GenericError;
- // 创建图像显示属性对象;
- ImageAttributes ImgAttr;
- // 创建新位图;
- Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
- // 创建水印图显示位置;
- RectF layWatemarkRect(10.0, 10.0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight());
- // 创建画布对象;
- Graphics *grap = Graphics::FromImage(pBitmap);
- // 绘制画布底色;
- if ( Ok != grap->Clear(Color(255,255,255,255)) )
- goto clear;
- // 设置图片显示质量;
- if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
- goto clear;
- // 设置文本质量;
- if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
- goto clear;
- // 在画布上画原始图片;
- if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
- goto clear;
- // 设置透明颜色为水印图片四角底色,水印图显示为圆角;
- if ( Ok != ImgAttr.SetColorKey(0xFF00FF00, 0xFF00FF00, ColorAdjustTypeBitmap))
- goto clear;
- // 设置水印图片透明底为0.3;
- if ( Ok != ImgAttr.SetColorMatrix(&_ClrMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap) )
- goto clear;
- // 在画布左上角画水印;
- if ( Ok != grap->DrawImage(pWatemarkImg, layWatemarkRect, 0, 0, pWatemarkImg->GetWidth(), pWatemarkImg->GetHeight(), UnitPixel, &ImgAttr) )
- goto clear;
- if ( grap )
- delete grap;
- return pBitmap;
- clear:
- if ( grap )
- delete grap;
- if ( pBitmap )
- delete pBitmap;
- if ( pImg )
- delete pImg;
- if ( pWatemarkImg )
- delete pWatemarkImg;
- return NULL;
- }
- BOOL IMGCommon::AppendImgWatermark(IN Image* pImg, IN Image* pWatemarkImg, IN LPCTSTR lpSaveImgpath)
- {
- Image* pSaveImg = AppendImgWatermark(pImg, pWatemarkImg);
- if ( pSaveImg == NULL )
- return FALSE;
- // 创建新的目录;
- CString strImgs = lpSaveImgpath;
- INT nIndex = strImgs.ReverseFind(_T('\\'));
- if ( nIndex != -1 )
- {
- strImgs = strImgs.Mid(0,nIndex+1);
- if ( !PathFileExists(strImgs) )
- {
- INT nRet = SHCreateDirectoryEx(NULL, strImgs, NULL);
- if ( ERROR_SUCCESS == nRet || ERROR_ALREADY_EXISTS == nRet )
- {
- if (SaveImg2newfile(pSaveImg, (CString)lpSaveImgpath, 100))
- {
- if ( pSaveImg )
- delete pSaveImg;
- pSaveImg = NULL;
- return TRUE;
- }
- return FALSE;
- }
- }
- }
- return FALSE;
- }
- BOOL IMGCommon::AppendImgWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkImgpath, IN LPCTSTR lpSaveImgpath)
- {
- Image *pSaveImg = AppendImgWatermark(lpImgpath, lpWatemarkImgpath);
- if ( pSaveImg == NULL)
- return FALSE;
- // 创建新的目录;
- CString strImgs = lpSaveImgpath;
- INT nIndex = strImgs.ReverseFind(_T('\\'));
- if ( nIndex != -1 )
- {
- strImgs = strImgs.Mid(0,nIndex+1);
- if ( !PathFileExists(strImgs) )
- {
- INT nRet = SHCreateDirectoryEx(NULL, strImgs, NULL);
- if ( ERROR_SUCCESS == nRet || ERROR_ALREADY_EXISTS == nRet )
- {
- if (SaveImg2newfile(pSaveImg, (CString)lpSaveImgpath, 100))
- {
- if ( pSaveImg )
- delete pSaveImg;
- pSaveImg = NULL;
- return TRUE;
- }
- return FALSE;
- }
- }
- }
- return FALSE;
- }
- Image* IMGCommon::AppendWordWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkString)
- {
- if ( pImg == NULL || lpWatemarkString == NULL || lpWatemarkString[0] == _T('\0'))
- return NULL;
- #ifndef UNICODE
- WCHAR* pText = StringProcess::ascii2unicode(lpWatemarkString);
- #endif
- // 创建新位图;
- Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
- // 创建字体;
- FontFamily ffly(L"宋体");
- Gdiplus::Font ft(&ffly, 24, FontStyleRegular, UnitPixel);
- // 创建画刷;
- SolidBrush brush(0x99000000);
- // 创建文本格式;
- StringFormat sf;
- sf.SetAlignment(StringAlignmentCenter); // 水平居中;
- // 创建水印文本显示位置;
- RectF layWatemarkRect(pImg->GetWidth(), pImg->GetHeight() - 25, 0.0, 0.0); // 文字阴影;
- RectF layWatemarkRect2(pImg->GetWidth(), pImg->GetHeight() - 26, 0.0, 0.0); // 文字原体;
- // 创建画布对象;
- Graphics *grap = Graphics::FromImage(pBitmap);
- // 绘制画布底色;
- if ( Ok != grap->Clear(Color(255,255,255,255)) )
- goto clear;
- // 设置图片显示质量;
- if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
- goto clear;
- // 设置文本质量;
- if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
- goto clear;
- // 在画布上画原始图片;
- if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
- goto clear;
- // 画水印阴影;
- #ifdef UNICODE
- if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect, &sf, &brush) )
- #else
- if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
- #endif
- goto clear;
- // 画水印文本;
- brush.SetColor(0x99ffffff);
- #ifdef UNICODE
- if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect2, &sf, &brush) )
- #else
- if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
- #endif
- goto clear;
- if ( grap )
- delete grap;
- return pBitmap;
- clear:
- if ( grap )
- delete grap;
- if ( pBitmap )
- delete pBitmap;
- if ( pImg )
- delete pImg;
- #ifndef UNICODE
- if (pText)
- delete []pText;
- #endif
- return NULL;
- }
- Image* IMGCommon::AppendWordWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkString)
- {
- if ( !PathFileExists(lpImgpath) || lpWatemarkString == NULL || lpWatemarkString[0] == _T('\0'))
- return NULL;
- #ifdef UNICODE
- Image *pImg = Image::FromFile(lpImgpath);
- #else
- BSTR str = _bstr_t(lpImgpath);
- Image* pImg = Image::FromFile(str);
- SysFreeString(str);
- #endif
- #ifndef UNICODE
- WCHAR* pText = StringProcess::ascii2unicode(lpWatemarkString);
- #endif
- // 创建新位图;
- Bitmap *pBitmap = new Bitmap(pImg->GetWidth(), pImg->GetHeight(), PixelFormat32bppPARGB);
- // 创建字体;
- FontFamily ffly(L"宋体");
- Gdiplus::Font ft(&ffly, 24, FontStyleRegular, UnitPixel);
- // 创建画刷;
- SolidBrush brush(0x99000000);
- // 创建文本格式;
- StringFormat sf;
- sf.SetAlignment(StringAlignmentCenter); // 水平居中;
- // 创建水印文本显示位置;
- RectF layWatemarkRect(pImg->GetWidth(), pImg->GetHeight() - 25, 0.0, 0.0); // 文字阴影;
- RectF layWatemarkRect2(pImg->GetWidth(), pImg->GetHeight() - 26, 0.0, 0.0); // 文字原体;
- // 创建画布对象;
- Graphics *grap = Graphics::FromImage(pBitmap);
- // 绘制画布底色;
- if ( Ok != grap->Clear(Color(255,255,255,255)) )
- goto clear;
- // 设置图片显示质量;
- if ( Ok != grap->SetSmoothingMode(SmoothingModeAntiAlias) )
- goto clear;
- // 设置文本质量;
- if ( Ok != grap->SetTextRenderingHint(TextRenderingHintAntiAlias))
- goto clear;
- // 在画布上画原始图片;
- if ( Ok != grap->DrawImage(pImg, 0, 0, pImg->GetWidth(), pImg->GetHeight()) )
- goto clear;
- // 画水印阴影;
- #ifdef UNICODE
- if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect, &sf, &brush) )
- #else
- if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
- #endif
- goto clear;
- // 画水印文本;
- brush.SetColor(0x99ffffff);
- #ifdef UNICODE
- if ( Ok != grap->DrawString(lpWatemarkString, wcslen(lpWatemarkString), &ft, layWatemarkRect2, &sf, &brush) )
- #else
- if ( Ok != grap->DrawString(pText, wcslen(pText), &ft, layWatemarkRect, &sf, &brush) )
- #endif
- goto clear;
- if ( grap )
- delete grap;
- return pBitmap;
- clear:
- if ( grap )
- delete grap;
- if ( pBitmap )
- delete pBitmap;
- if ( pImg )
- delete pImg;
- #ifndef UNICODE
- if (pText)
- delete []pText;
- #endif
- return NULL;
- }
- BOOL IMGCommon::SaveBitmap(IN CDC *pDC, IN CBitmap* pBitmap, IN CONST INT& nBitdepth, IN const INT& nWidth, IN const INT& nHeight, IN LPCTSTR lpSavepath)
- {
- // 计算出位图大小 = ((宽 * 位宽 + 31)/ 32)* 4 * 高;
- DWORD dwBmpSize = ((nWidth*nBitdepth + 31) / 32) * 4 * nHeight;
- HANDLE hFile = CreateFile(lpSavepath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- //WriteLog(_T("二维码位图创建失败"));
- return FALSE;
- }
- HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE | SEC_COMMIT, 0,
- sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + dwBmpSize, NULL);
- LPBYTE lpbyMapView = (LPBYTE)MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
- ZeroMemory(lpbyMapView, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + dwBmpSize);
- //结构体地址割付;
- LPBITMAPFILEHEADER pbfh = (LPBITMAPFILEHEADER)lpbyMapView;
- LPBITMAPINFO pbmi = (LPBITMAPINFO)(lpbyMapView + sizeof(BITMAPFILEHEADER));
- LPVOID pbdt = lpbyMapView + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2;
- // BITMAPFILEHEADER
- // pbfh->bfType = (WORD) (('M' << 8) | 'B'); // "BM"
- // BMP类型,与上面同值;
- pbfh->bfType = 0x4D42;
- pbfh->bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + dwBmpSize; // 文件总长;
- pbfh->bfReserved1 = 0;
- pbfh->bfReserved2 = 0;
- pbfh->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2; // 位图数据相对于文件头的偏移量;
- // LPBITMAPINFO->BITMAPINFOHEADER
- pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- pbmi->bmiHeader.biWidth = nWidth;
- pbmi->bmiHeader.biHeight = nHeight;
- pbmi->bmiHeader.biPlanes = 1;
- pbmi->bmiHeader.biBitCount = nBitdepth; // 位深度 1;
- pbmi->bmiHeader.biCompression = BI_RGB;
- pbmi->bmiHeader.biSizeImage = dwBmpSize;
- pbmi->bmiHeader.biXPelsPerMeter = 3780;
- pbmi->bmiHeader.biYPelsPerMeter = 3780;
- pbmi->bmiHeader.biClrUsed = 0;
- pbmi->bmiHeader.biClrImportant = 0;
- // 放大到指定的像素宽高;
- CDC* pWorkDC = new CDC;
- pWorkDC->CreateCompatibleDC(NULL);
- CBitmap* pWorkBitmap = new CBitmap;
- pWorkBitmap->CreateBitmap(nWidth, nHeight, 1, 1, NULL);
- CBitmap* pOldBitmap = pWorkDC->SelectObject(pWorkBitmap);
- pWorkDC->StretchBlt(0, 0, nWidth, nHeight, pDC, 0, 0, nWidth, nHeight, SRCCOPY);
- GetDIBits(pWorkDC->m_hDC, (HBITMAP)*pWorkBitmap, 0, nHeight, pbdt, pbmi, DIB_RGB_COLORS);
- pWorkDC->SelectObject(pOldBitmap);
- delete pWorkBitmap;
- delete pWorkDC;
- UnmapViewOfFile(lpbyMapView);
- CloseHandle(hFileMapping);
- CloseHandle(hFile);
- return TRUE;
- }
- /************************************************************************/
- /* 函数:SaveBitmapEx[10/17/2016 IT];
- /* 描述:将CDC上的位图保存成文件;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:位图大小 = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 调色板大小(16、24、32位深的调色板大小为0) + 位图数据大小;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL IMGCommon::SaveBitmapEx(IN CDC *pDC, IN CBitmap* pBitmap, IN CONST INT& nBitdepth, IN const INT& nWidth, IN const INT& nHeight, IN LPCTSTR lpSavepath)
- {
- DWORD dwPaletteSize = 0; // 调色板大小;
- DWORD dwBmBitsSize = 0; // 位图中像素字节大小;
- DWORD dwDIBSize = 0; // 位图文件大小;
- WORD wBitsCount = 0; // 当前显示分辨率下每个像素所占字节数;
- if( nBitdepth <= 1 )
- wBitsCount = 1;
- else if( nBitdepth <= 4 )
- wBitsCount = 4;
- else if( nBitdepth <= 8 )
- wBitsCount = 8;
- else if( nBitdepth <= 16 )
- wBitsCount = 16;
- else if( nBitdepth <= 24 )
- wBitsCount = 24;
- else
- wBitsCount = 32;
- // 计算出调色板大小;
- if( wBitsCount <= 8 )
- dwPaletteSize = (1 << wBitsCount) * sizeof(RGBQUAD);
- // 计算出位图数据大小 = ((宽 * 位宽 + 31)/ 32)* 4 * 高;
- dwBmBitsSize = ((nWidth*wBitsCount + 31) / 8) * nHeight;
- // 计算出位图文件大小;
- dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
- //////////////////////////////////////////////////////////////////////////
- // 根据新位图来创建位图文件;
- HANDLE hFile = CreateFile(lpSavepath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- return FALSE;
- }
- HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, dwDIBSize, NULL);
- LPBYTE lpbyMapView = (LPBYTE)MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
- ZeroMemory(lpbyMapView, dwDIBSize);
- //结构体地址割付;
- LPBITMAPFILEHEADER pbfh = (LPBITMAPFILEHEADER)lpbyMapView;
- LPBITMAPINFO pbmi = (LPBITMAPINFO)(lpbyMapView + sizeof(BITMAPFILEHEADER));
- LPVOID pbdt = lpbyMapView + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize;
- // BITMAPFILEHEADER
- // pbfh->bfType = (WORD) (('M' << 8) | 'B'); // "BM"
- // BMP类型,与上面同值;
- pbfh->bfType = 0x4D42;
- pbfh->bfSize = dwDIBSize; // 文件总长;
- pbfh->bfReserved1 = 0;
- pbfh->bfReserved2 = 0;
- pbfh->bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize; // 位图数据相对于文件头的偏移量;
- // LPBITMAPINFO->BITMAPINFOHEADER
- pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- pbmi->bmiHeader.biWidth = nWidth;
- pbmi->bmiHeader.biHeight = nHeight;
- pbmi->bmiHeader.biPlanes = 1;
- pbmi->bmiHeader.biBitCount = wBitsCount; // 位深度;
- pbmi->bmiHeader.biCompression = BI_RGB;
- pbmi->bmiHeader.biSizeImage = dwBmBitsSize; // 位图数据大小;
- pbmi->bmiHeader.biXPelsPerMeter = 3780;
- pbmi->bmiHeader.biYPelsPerMeter = 3780;
- pbmi->bmiHeader.biClrUsed = 0;
- pbmi->bmiHeader.biClrImportant = 0;
- //////////////////////////////////////////////////////////////////////////
- // 拉伸到指定的像素宽高;
- CDC* pWorkDC = new CDC;
- pWorkDC->CreateCompatibleDC(NULL);
- CBitmap* pWorkBitmap = new CBitmap;
- pWorkBitmap->CreateBitmap(nWidth, nHeight, 1, 1, NULL);
- CBitmap* pOldBitmap = pWorkDC->SelectObject(pWorkBitmap);
- pWorkDC->StretchBlt(0, 0, nWidth, nHeight, pDC, 0, 0, nWidth, nHeight, SRCCOPY);
- GetDIBits(pWorkDC->GetSafeHdc(), (HBITMAP)*pWorkBitmap, 0, nHeight, pbdt, pbmi, DIB_RGB_COLORS);
- pWorkDC->SelectObject(pOldBitmap);
- delete pWorkBitmap;
- delete pWorkDC;
- UnmapViewOfFile(lpbyMapView);
- CloseHandle(hFileMapping);
- CloseHandle(hFile);
- return TRUE;
- }
- void IMGCommon::DrawString(IN CDC* pdc, IN CONST INT& nFontSize, IN LPCTSTR lpFontName, IN LPCTSTR lpString, IN OUT CRect &rcString)
- {
- if ( !pdc || !lpString )
- return;
- LOGFONT log;
- // 设置字体字号;
- log.lfHeight = -MulDiv(nFontSize ? nFontSize : 9, GetDeviceCaps(pdc->GetSafeHdc(), LOGPIXELSY), 72);
- log.lfWidth = 0;
- log.lfEscapement = 0;
- log.lfOrientation = 0;
- log.lfWeight = FW_REGULAR;
- log.lfItalic = false;
- log.lfUnderline = false;
- log.lfStrikeOut = 0;
- log.lfCharSet = ANSI_CHARSET;
- log.lfOutPrecision = OUT_DEFAULT_PRECIS;
- log.lfClipPrecision = CLIP_DEFAULT_PRECIS;
- log.lfQuality = DEFAULT_QUALITY;
- log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
- // 设置字体名称;
- _tcscpy_s(log.lfFaceName, (lpFontName && (lpFontName[0] != _T('\0'))) ? lpFontName : _T("Arial"));
- CFont cf;
- cf.CreateFontIndirect(&log);
- CFont *oldf = pdc->SelectObject(&cf);
- SIZE Size;
- GetTextExtentPoint32(pdc->GetSafeHdc(), lpString, _tcslen(lpString), &Size);
- rcString.SetRect(rcString.left, rcString.top, Size.cx + rcString.left, Size.cy + rcString.top);
- pdc->TextOut(rcString.left, rcString.top, lpString);
- }
- #endif
- HICON IMGCommon::GetFileIcon(IN const CString& strFilePath, IN BOOL bLarge)
- {
- SHFILEINFO SHFI;
- ZeroMemory(&SHFI, sizeof(SHFI));
- DWORD_PTR ret = ::SHGetFileInfo(strFilePath, 0, &SHFI, sizeof(SHFI),
- SHGFI_ICON | (bLarge ? SHGFI_LARGEICON : SHGFI_SMALLICON));
- if (ret != 0)
- {
- return SHFI.hIcon;
- }
- return NULL;
- }
|