Gdi+ 还是相当好用的.
1> Image保存图像,需要一个CLSID的参数,它可以这样获得:
[cpp]class="Apple-converted-space"> view plaincopy
- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
- {
- UINT num = 0;
- UINT size = 0;
- ImageCodecInfo* pImageCodecInfo = NULL;
- GetImageEncodersSize(&num, &size);
- if(size == 0)
- return -1;
- pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
- if(pImageCodecInfo == NULL)
- return -1;
- 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;
- }
- }
- free(pImageCodecInfo);
- return -1;
- }
2> Image::Save的另外一个参数EncoderParameters可用于图像的压缩*(这是从网上抄下来的)
使用img/jpeg配合encoderParameters.Parameter[0].Value设置 可以大幅度的减小图像文件所占磁盘空间
[cpp] view plaincopy
- EncoderParameters encoderParameters;
-
-
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
-
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
-
- encoderParameters.Parameter[0].NumberOfValues = 1;
- ULONG quality;
-
- quality = 80;
- encoderParameters.Parameter[0].Value = &quality;
3> 关于缩略图
我使用了一下Image的GetThumbnailImage,发现对于某些图像效果很不理想,(颜色较鲜艳的缩略图效果好点,但是对于那些色差不大整体又暗的图像效果就差劲了). 这个时候使用Graphic配合Bitmap直接画缩略尺寸的图像效果挺好
[cpp] view plaincopy
- BOOL QImgProcess::CreateThumb( int cx ,QBuf &out)
- {
- ASSERT(m_pImg != NULL);
-
- int nWidth = m_pImg->GetWidth();
- if (cx >= nWidth)
- {
- return TRUE;
- }
- int nHeight = m_pImg->GetHeight();
- int nThumbHeight = nHeight * cx / m_pImg->GetWidth() ;
- Bitmap bitmap(cx, nThumbHeight, PixelFormat24bppRGB);
- Graphics graph(&bitmap);
- graph.DrawImage(m_pImg, Rect(0,0,cx,nThumbHeight));
- ......
- }