본문 바로가기

Dev/C++

이미지 변환 모듈 (모바일용, GDI+)

- 이미지의 포맷 변환 및 퀄러티, 크기조정(비율) 모듈 
- Windows 환경, C++, GDI+, Component


STDMETHODIMP CMobileImage::Convert(BSTR Src, BSTR Output, BSTR ImgFormat, long Quality, long Width, long Height, VARIANT_BOOL* pSuccess)
{
*pSuccess = VARIANT_FALSE;

_bstr_t bstrSrc(Src);
_bstr_t bstrOutput(Output);
_bstr_t bstrImgFormat(ImgFormat);

ULONG_PTR gdiToken;
Gdiplus::GdiplusStartupInput gdiInput;
Gdiplus::GdiplusStartup(&gdiToken, &gdiInput, NULL);

try {
std::auto_ptr<Gdiplus::Image> pImage(Gdiplus::Image::FromFile((LPCWSTR)bstrSrc));
if (!pImage.get()) {
throw -1;
}

CLSID m_pngClsid;
if (GetEncoderClsid((LPCWSTR)ImgFormat, &m_pngClsid) == -1) {
throw -1;
}
else {
Gdiplus::EncoderParameters params;
params.Count = 1;
params.Parameter[0].Guid = Gdiplus::EncoderQuality;
params.Parameter[0].Type = Gdiplus::EncoderParameterValueTypeLong;
params.Parameter[0].NumberOfValues = 1;

long Value = Quality;
params.Parameter[0].Value = &Value;

if (Width > 0 && Height > 0) {
std::auto_ptr<Gdiplus::Bitmap> pResize(new Gdiplus::Bitmap(Width, Height, PixelFormat24bppRGB));
std::auto_ptr<Gdiplus::Graphics> pGrap(Gdiplus::Graphics::FromImage(pResize.get()));
pGrap->DrawImage(pImage.get(), 0, 0, Width, Height);

pResize->Save((LPCWSTR)bstrOutput, &m_pngClsid, &params);
}
else {
pImage->Save((LPCWSTR)bstrOutput, &m_pngClsid, &params);
}
}

*pSuccess = VARIANT_TRUE;
}
catch (...) { }

Gdiplus::GdiplusShutdown(gdiToken);

if (m_spObjectContext) { m_spObjectContext->SetComplete(); }

return S_OK;
}


int CMobileImage::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
using namespace Gdiplus;

UINT  num = 0;          // number of image encoders
UINT  size = 0;         // size of the image encoder array in bytes

ImageCodecInfo* pImageCodecInfo = NULL;

GetImageEncodersSize(&num, &size);
if(size == 0)
return -1;  // Failure

pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1;  // Failure

GetImageEncoders(num, size, pImageCodecInfo);

for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;  // Success
}    
}

free(pImageCodecInfo);
return -1;  // Failure
}

'Dev > C++' 카테고리의 다른 글

boost 설치  (0) 2013.11.20
boost::asio 클라이언트 소켓 (timeout 기능)  (0) 2010.10.14
이미지 변환 모듈 (모바일용, GDI+)  (0) 2010.06.22
C++0x, RValue Reference  (0) 2009.05.27
C++0x Lambda  (0) 2009.05.20
C++0x 지원 컴파일러 목록  (0) 2009.05.20

태그