123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #include "stdafx.h"
- #include "ImageCompress.h"
- ULONG_PTR ImageCompress::m_gdiplusToken = NULL;
- ImageCompress::ImageCompress(void)
- {
- }
- ImageCompress::~ImageCompress(void)
- {
- }
- int ImageCompress::GetEncoderClsid(IN const WCHAR* format, OUT CLSID* pClsid)
- {
- UINT nNumberOfImageEncoders = 0;
- UINT nSizeOfImageEndcoderArray = 0;
- ImageCodecInfo* pImageCodecInfo = NULL;
- GetImageEncodersSize(&nNumberOfImageEncoders, &nSizeOfImageEndcoderArray);
- if (nSizeOfImageEndcoderArray == 0)
- return -1;
- pImageCodecInfo = (ImageCodecInfo*)(malloc(nSizeOfImageEndcoderArray));
- if (pImageCodecInfo == NULL)
- return -1;
- GetImageEncoders(nNumberOfImageEncoders, nSizeOfImageEndcoderArray, pImageCodecInfo);
- for (UINT i = 0; i < nNumberOfImageEncoders; ++i)
- {
- if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[i].Clsid;
- free(pImageCodecInfo);
- return i;
- }
- }
- free(pImageCodecInfo);
- return -1;
- }
- void ImageCompress::ScaleZoombySpecificHeight(IN const int &nWidth, IN const int &nHeight, IN OUT CRect &rc)
- {
- try
- {
- if (nWidth == 0 || nHeight == 0)return;
-
- float fscale = (float)nWidth / (float)nHeight;
-
- float rcscale = ((float)rc.Width()) / ((float)rc.Height());
- int rcwid = rc.Width();
- int rchei = rc.Height();
- int dt = 0;
-
- if (rcscale < fscale)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
- dt = (rchei - rcwid / fscale) / 2;
- rc.top += dt;
- rc.bottom -= dt;
- }
- else
- {
- dt = (rcwid - rchei*fscale) / 2;
- rc.left += dt;
- rc.right -= dt;
- }
- }
- catch (...)
- {
- }
- }
- void ImageCompress::ScaleZoombySpecificWidth(IN const int &nWidth, IN const int &nHeight, IN OUT CRect &rc)
- {
- try
- {
- if (nWidth == 0 || nHeight == 0)return;
-
- float fscale = (float)nWidth / (float)nHeight;
-
- float rcscale = ((float)rc.Width()) / ((float)rc.Height());
- int rcwid = rc.Width();
- int rchei = rc.Height();
- int dt = 0;
-
- if (rcscale < fscale)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
- dt = (rcwid - rchei*fscale) / 2;
- rc.left += dt;
- rc.right -= dt;
- }
- else
- {
- dt = (rchei - rcwid / fscale) / 2;
- rc.top += dt;
- rc.bottom -= dt;
- }
- }
- catch (...)
- {
- }
- }
- void ImageCompress::SaveImageToFile(IN Image *img, IN CString &path, IN ULONG quality)
- {
- try
- {
- if (img == NULL)return;
- CLSID encoderClsid;
- BSTR bstr = path.AllocSysString();
- path.MakeLower();
- if (path.Right(3) == _T("bmp"))
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- img->Save(bstr, &encoderClsid, NULL);
- }
- else if (path.Right(3) == _T("png"))
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- img->Save(bstr, &encoderClsid, NULL);
- }
- else
- {
- 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;
-
- encoderParameters.Parameter[0].Value = &quality;
- img->Save(bstr, &encoderClsid, &encoderParameters);
- }
- SysFreeString(bstr);
- }
- catch (...)
- {
- }
- }
- BOOL ImageCompress::ToCompressImage(IN const TCHAR *pImageSource, IN const TCHAR *pImageDest, IN const int &nXPiexel, IN const int &nYPiexel)
- {
- Image *pImage = NULL;
- ::DeleteFile(pImageDest);
-
- CFile cf;
- if (cf.Open(pImageSource, CFile::modeRead))
- {
- DWORD dwLength = cf.GetLength();
- BYTE *pData = new BYTE[dwLength];
- if (pData == NULL)
- return FALSE;
- cf.Read(pData, dwLength);
- cf.Close();
-
- HGLOBAL taghMem = GlobalAlloc(GMEM_MOVEABLE, dwLength);
- if (taghMem == NULL)
- return FALSE;
- BYTE *pMem = (BYTE*)GlobalLock(taghMem);
- memcpy(pMem, pData, dwLength);
-
- IStream *pStream;
- CreateStreamOnHGlobal(taghMem, TRUE, &pStream);
- pImage = Gdiplus::Image::FromStream(pStream);
- GlobalUnlock(taghMem);
- pStream->Release();
- delete[]pData;
- }
-
- if (pImage == NULL)
- return FALSE;
-
- if (pImage->GetWidth() > nXPiexel || pImage->GetHeight() > nYPiexel)
- {
- CRect tagRect(0, 0, nXPiexel, nYPiexel);
- ScaleZoombySpecificWidth(pImage->GetWidth(), pImage->GetHeight(), tagRect);
- Bitmap bp(tagRect.Width(), tagRect.Height());
- Graphics *graphic = Graphics::FromImage(&bp);
- graphic->Clear(Color(255, 255, 255, 255));
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- graphic->SetSmoothingMode(SmoothingModeHighQuality);
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- graphic->SetCompositingMode(CompositingModeSourceOver);
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- graphic->DrawImage(pImage, RectF(0, 0, tagRect.Width(), tagRect.Height()), 0, 0, pImage->GetWidth(), pImage->GetHeight(), UnitPixel);
- delete pImage;
- try
- {
- CString path(pImageDest);
- CLSID encoderClsid;
- BSTR bstr = path.AllocSysString();
- path.MakeLower();
- if (path.Right(3) == _T("bmp"))
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- bp.Save(bstr, &encoderClsid, NULL);
- }
- else if (path.Right(3) == _T("png"))
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- bp.Save(bstr, &encoderClsid, NULL);
- }
- else
- {
- ULONG quality = 95;
- 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;
-
- encoderParameters.Parameter[0].Value = &quality;
- bp.Save(bstr, &encoderClsid, &encoderParameters);
- }
- SysFreeString(bstr);
- }
- catch (...)
- {
- return FALSE;
- }
- }
- return TRUE;
- }
|