http://blog.csdn.net/shuilan0066/article/details/7080244
前言
CImage类是基于GDI+的,但是这里为什么要讲归于GDI?
主要是基于这样的考虑: 在GDI+环境中,我们可以直接使用GDI+ ,没多少必要再使用CImage类
但是,如果再GDI环境中,我们要想使用GDI+,有点麻烦,还得加入头文件,加入启动GDI+的代码和关闭GDI+的代码,显得太罗嗦了,GDI 的CBitmap 处理功能又有局限,只能处理BMP格式的图片。 怎么办?这时,我们便可使用CImage类,因为这个类本身封装了GDI+得使用环境,所以无需我们手动设置,简化了我们的操作。 同时,又可以利用GDI+中强大的图片处理功能,及可以简便的与CBitmap对象进行转换 ,大大方便了在GDI环境下,进行各种图片处理工作 。
其实,将其称作 GDI/ GDI+ 混合编程,这样才更确切些。
为什么引入CImage类?
CBitmap 类只能处理BMP格式的图片,非常受限。
而CImage可以处理JPGE GIF BMP PNG多种格式图片,扩展了图片处理功能 且能与CBitmap 进行转换( 因为所载入的位图句柄都是HBITMAP,所以可相互转换),因此引入CImage类进行图像处理
CImage provides enhanced bitmap support, including the ability to load and save images in JPEG, GIF, BMP, and Portable Network Graphics (PNG) formats
CImage类介绍
CImage是MFC和ATL共享的新类,它能从外部磁盘中调入一个JPEG、GIF、BMP和PNG格式的图像文件加以显示,而且这些文件格式可以相互转换。
CImage是VC.NET中定义的一种MFC/ATL共享类,也是ATL的一种工具类,它提供增强型的(DDB和DIB)位图支持,可以装入、显示、转换和保存多种格式的图像文件,包括BMP、GIF、JPG、PNG、TIF等。CImage是一个独立的类,没有基类。(CImage类是基于GDI+的,从VC.NET起引进,VC 6.0中没有。)
ATL(Active Template Library,活动模板库)是一套基于模板的 C++ 类,用以简化小而快的 COM 对象的编写。
为了在MFC程序中使用CImage类,必须包含ATL的图像头文件atlimage.h:(在VS08 SP1中不用包含)
#include <atlimage.h>
1 加载位图文件
// CImage可加载的图片文件有JPG,BMP,TIF.PNG等格式 而CBitmap只能加载BMP图片文件 if(!PathFileExists(imgFilePath)) return NULL; CImage nImage; nImage.Load(imgFilePath); return nImage.Detach(); //返回HBITMAP 可用CBitmap 处理 也可用CImage处理
2 与CBitmap转换
CImage nImage; nImage.Load(imgFilePath); HBITMAP hBitmap=nImage.Detach(); // 获得位图句柄 用以转换 // 转换方式一: CBitmap bmp; bmp.DeleteObject(); bmp.Attach(hBitmap); // 转换为CBitmap对象 // 转换方式二: CBitmap *pBitmap=CBitmap::FromHandle(nImage.m_hBitmap);
3 获得CImage对象的cdc
CDC *pDC=CDC::FromHandle(nImage.GetDC()); // Use pDC here nImage.ReleaseDC();
4 显示位图
思路: 将CImage对象 绘制在对应的DC中
所使用的函数 BitBlt StretchBlt Draw等
以Draw举例:
BOOL Draw( HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight ) const throw( ); BOOL Draw( HDC hDestDC, const RECT& rectDest, const RECT& rectSrc ) const throw( ); BOOL Draw( HDC hDestDC, int xDest, int yDest ) const throw( ); BOOL Draw( HDC hDestDC, const POINT& pointDest ) const throw( ); BOOL Draw( HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight ) const throw( ); BOOL Draw( HDC hDestDC, const RECT& rectDest ) const throw( );
Draw performs the same operation as StretchBlt, unless the image contains a transparent color or alpha channel. In that case,Draw performs the same operation as eitherTransparentBlt orAlphaBlend as required.
For versions of Draw that do not specify a source rectangle, the entire source image is the default. For the version ofDraw that does not specify a size for the destination rectangle, the size of the source image is the default and no stretching or shrinking occurs.
EXAMPLE 1:
CImage img; img.Load("1.jpg"); if (!img.IsNull()) { img.Draw(pDC->m_hDC,CRect(0,0,100,100)); }
EXAMPLE 2: 画在另一个位图中
CImage img; img.Load(filePath); // 获得CImage对象的 CDC HDC hDC=img.GetDC(); CDC *pDC=CDC::FromHandle(hDC); CBitmap bmp;// 只是创建了位图对象,但还没有将位图对象与位图资源联系起来 bmp.CreateCompatibleBitmap(pDC,nWidth,nHeight); // 创建新的位图资源 CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap *pOld=memDC.SelectObject(&bmp); // 将img图像绘制到bmp中 ::SetStretchBltMode(memDC.m_hDC,HALFTONE); ::SetBrushOrgEx(memDC.m_hDC,0,0,NULL); img.StretchBlt(memDC.m_hDC,CRect(0,0,nWidth,nHeight)/*DestRect*/,CRect(0,0,nWidth,nHeight)/*SourceRect*/,SRCCOPY); HBITMAP hBitmap=(HBITMAP)memDC.SelectObject(pOld->m_hObject); // 获得新创建的位图资源句柄 img.ReleaseDC();
5 将位图资源与对象进行分离
inline HBITMAP CImage::Detach() throw() { HBITMAP hBitmap; ATLASSUME( m_hBitmap != NULL ); ATLASSUME( m_hDC == NULL ); hBitmap = m_hBitmap; m_hBitmap = NULL; m_pBits = NULL; m_nWidth = 0; m_nHeight = 0; m_nBPP = 0; m_nPitch = 0; m_iTransparentColor = -1; m_bHasAlphaChannel = false; m_bIsDIBSection = false; return( hBitmap ); }
6 释放资源
CBitmap 使用DeleteObject()来主动释放掉位图资源
CImage 没有DeleteObject()函数 ,而是用Destroy()函数来主动释放位图资源
inline void CImage::Destroy() throw() { HBITMAP hBitmap; if( m_hBitmap != NULL ) { hBitmap = Detach(); ::DeleteObject( hBitmap ); //释放位图资源 } }
CBitmap 析构时,会自动释放掉所占用的位图资源
CImage 析构时,也会自动释放掉所占用的位图资源
inline CImage::~CImage() throw() { Destroy(); //释放掉所占用的位图资源 s_initGDIPlus.DecreaseCImageCount(); }
7 读写图像数据
主要用到3个函数 :
1 )GetBits() 获得数据区的指针
Retrieves a pointer to the actual bit values of a given pixel in a bitmap.
void* GetBits( ) throw( ); |
inline void* CImage::GetBits() throw() { ATLASSUME( m_hBitmap != NULL ); ATLASSERT( IsDIBSection() ); return( m_pBits ); }
A pointer to the bitmap buffer. If the bitmap is a bottom-up DIB, the pointer points near the end of the buffer. If the bitmap is a top-down DIB, the pointer points to the first byte of the buffer.
Using this pointer, along with the value returned by GetPitch, you can locate and change individual pixels in an image.
注意: 由GetBits()取得的指针不一定是图片数据的起始行,必须结合GetPitch()的值来确定起始行位置
2)GetPitch()
inline int CImage::GetPitch() const throw() { ATLASSUME( m_hBitmap != NULL ); ATLASSERT( IsDIBSection() ); return( m_nPitch ); }
获得图像数据每一行的字节数
The pitch of the image. If the return value is negative, the bitmap is a bottom-up DIB and its origin is the lower left corner. If the return value is positive, the bitmap is a top-down DIB and its origin is the upper left corner.
GetBits 与 GetPitch 关系:
当GetPitch()<0时,GetBits()获得的指针指向最后一行
当GetPitch()>0时,GetBits()获得的指针指向第一行
图像数据首行地址:
BYTE *pData=NULL; if(img.GetPitch()<0) pData=(BYTE*)img.GetBits()+(img.GetPitch()*(img.GetHeight()-1)); else pData=(BYTE*)img.GetBits();
或
BYTE *pData=NULL; if(img.GetPitch()<0) pData=(BYTE *)img.GetPixelAddress(img.GetHeight()-1,0); else pData=(BYTE *)img.GetPixelAddress(0,0);
3)GetBPP() 返回每个像素所占的bit数
inline int CImage::GetBPP() const throw() { ATLASSUME( m_hBitmap != NULL ); return( m_nBPP ); }
The number of bits per pixel.
This value determines the number of bits that define each pixel and the maximum number of colors in the bitmap
一个综合例子:
void CMyImage::Negatives(void) { int i, j; //图像每一行的字节数 int nRowBytes = GetPitch(); int nWidth = GetWidth(); int nHeight = GetHeight(); //每个像素所占的字节数 int nClrCount = GetBPP() / 8; LPBYTE p; for(int index = 0; index < nClrCount; index++) { p = (LPBYTE)GetBits(); for(i = 0; i < nHeight; i++) { for(j = 0; j < nWidth; j++) { p[j*nClrCount + index] = 255 - p[j*nClrCount + index]; } //如果nRowBytes>0 则从开始到结尾 //如果nRowBytes<0, 则从结尾到开始 p += nRowBytes; } } }
8 保存到图像文件中
Saves an image as the specified file name and type.
HRESULT Save( IStream* pStream, REFGUID guidFileType ) const throw(); HRESULT Save( LPCTSTR pszFileName, REFGUID guidFileType= GUID_NULL ) |
inline HRESULT CImage::Save( LPCTSTR pszFileName, REFGUID guidFileType ) const throw() { if( !InitGDIPlus() ) { return( E_FAIL ); } UINT nEncoders; UINT nBytes; Gdiplus::Status status; status = Gdiplus::GetImageEncodersSize( &nEncoders, &nBytes ); if( status != Gdiplus::Ok ) { return( E_FAIL ); } USES_CONVERSION_EX; Gdiplus::ImageCodecInfo* pEncoders = static_cast< Gdiplus::ImageCodecInfo* >( _ATL_SAFE_ALLOCA(nBytes, _ATL_SAFE_ALLOCA_DEF_THRESHOLD) ); if( pEncoders == NULL ) return E_OUTOFMEMORY; status = Gdiplus::GetImageEncoders( nEncoders, nBytes, pEncoders ); if( status != Gdiplus::Ok ) { return( E_FAIL ); } CLSID clsidEncoder = CLSID_NULL; if( guidFileType == GUID_NULL ) { // Determine clsid from extension clsidEncoder = FindCodecForExtension( ::PathFindExtension( pszFileName ), pEncoders, nEncoders ); } else { // Determine clsid from file type clsidEncoder = FindCodecForFileType( guidFileType, pEncoders, nEncoders ); } if( clsidEncoder == CLSID_NULL ) { return( E_FAIL ); } LPCWSTR pwszFileName = T2CW_EX( pszFileName, _ATL_SAFE_ALLOCA_DEF_THRESHOLD ); #ifndef _UNICODE if( pwszFileName == NULL ) return E_OUTOFMEMORY; #endif // _UNICODE if( m_bHasAlphaChannel ) { ATLASSUME( m_nBPP == 32 ); Gdiplus::Bitmap bm( m_nWidth, m_nHeight, m_nPitch, PixelFormat32bppARGB, static_cast< BYTE* >( m_pBits ) ); status = bm.Save( pwszFileName, &clsidEncoder, NULL ); if( status != Gdiplus::Ok ) { return( E_FAIL ); } } else { Gdiplus::Bitmap bm( m_hBitmap, NULL ); status = bm.Save( pwszFileName, &clsidEncoder, NULL ); if( status != Gdiplus::Ok ) { return( E_FAIL ); } } return( S_OK ); }
- pStream
-
A pointer to a stream containing the file name for the image.
- pszFileName
-
A pointer to the file name for the image.
- guidFileType
-
The file type to save the image as. Can be one of the following:- ImageFormatBMP An uncompressed bitmap image.
- ImageFormatPNG A Portable Network Graphic (PNG) compressed image.
- ImageFormatJPEG A JPEG compressed image.
- ImageFormatGIF A GIF compressed image.
Call this function to save the image using a specified name and type. If the guidFileType parameter is not included, the file name‘s file extension will be used to determine the image format. If no extension is provided, the image will be saved in BMP format.
MSDN例子:
Copy Code // Demonstrating saving various file formats int _tmain(int argc, _TCHAR* argv[]) { CImage myimage; // load existing image myimage.Load("image.bmp"); // save an image in BMP format myimage.Save("c:\image1.bmp"); // save an image in BMP format myimage.Save("c:\image2",ImageFormatBMP); // save an image in JPEG format myimage.Save("c:\image3.jpg"); // save an image in BMP format, even though jpg file extension is used myimage.Save("c:\image4.jpg",ImageFormatBMP); return 0; }
9 应用实例: 将两个图像合并为一个新的图像
//图像路径 CString img1Path; CString img2Path; CString img3Path; img1Path=_T("1.bmp"); img2Path=_T("2.bmp"); img3Path=_T("3.bmp"); // 将 图片1、2 合并成图片3 CImage img1,img2,img3; img1.Load(img1Path); img2.Load(img2Path); CBitmap bmp; CDC memDC; HDC hDC=NULL; CDC *pDC=NULL; CBitmap *pOld=NULL; HBITMAP hBitmap=NULL; //创建位图 hDC=img1.GetDC(); pDC=CDC::FromHandle(hDC); bmp.DeleteObject(); bmp.CreateCompatibleBitmap(pDC,img1.GetWidth()/2,img1.GetHeight()); memDC.DeleteDC(); memDC.CreateCompatibleDC(pDC); pOld=memDC.SelectObject(&bmp); ::SetStretchBltMode(memDC.m_hDC,HALFTONE); ::SetBrushOrgEx(memDC.m_hDC,0,0,NULL); // 背景置白色 CRgn rectRgn; rectRgn.CreateRectRgn(0,0,img1.GetWidth()/2,img1.GetHeight()); CBrush brush; brush.CreateSolidBrush(RGB(255,255,255)); memDC.FillRgn(&rectRgn,&brush); //画图 img1.StretchBlt(memDC.m_hDC,CRect(0,0,img1.GetWidth()/2,img1.GetHeight()/2),CRect(0,0,img1.GetWidth(),img1.GetHeight()),SRCCOPY); img2.StretchBlt(memDC.m_hDC,CRect(0,img1.GetHeight()/2,img1.GetWidth()/2,img1.GetHeight()),CRect(0,0,img2.GetWidth(),img2.GetHeight()),SRCCOPY); hBitmap=(HBITMAP)memDC.SelectObject(pOld->m_hObject); img3.Attach(hBitmap);// 载入位图资源 img3.Save(img3Path); // 保存新的位图 img1.ReleaseDC(); img1.Destroy(); img2.Destroy(); img3.Destroy();