在做界面时候,经常需要通过加载图片来达到美化的效果,毕竟PS还是更专业些。但是VS对于图片格式的支持还是么有那么好,导入资源中会有格式限制。以前用CxImage类做相关的转换,但是那个是在VC6上弄得,现在项目改成VS2010,发现MFC中已经有了对相关内容支持的CImage/CPngImage类。
由于我这里常用到Load(file),仔细的看了下相关的实现细节,其主要实现是依靠Gdiplus来实现的。里面也间接演示了图像数据创建Bitmap,有兴趣的可以研究下。
inline HRESULT CImage::Load(_In_z_ LPCTSTR pszFileName) throw()
{
if( !InitGDIPlus() )
{
return( E_FAIL );
}
Gdiplus::Bitmap bmSrc( (CT2W)pszFileName );
if( bmSrc.GetLastStatus() != Gdiplus::Ok )
{
return( E_FAIL );
}
return( CreateFromGdiplusBitmap( bmSrc ) );
}
inline HRESULT CImage::CreateFromGdiplusBitmap(_Inout_ Gdiplus::Bitmap& bmSrc) throw()
{
Gdiplus::PixelFormat eSrcPixelFormat = bmSrc.GetPixelFormat();
UINT nBPP = 32;
DWORD dwFlags = 0;
Gdiplus::PixelFormat eDest