贡献一个刚刚调试成功的功能,希望能对大家贡献一点点微薄的技术。同样的问题从网上也可以搜索出来很多相关帖子和解决技术,但是没有一个具体而详细的实现步骤。我就此给汇总一下吧。
功能简介:使用 CImageList 类的 Add 方法将图片资源加入,而后将资源读取出来,图片资源缩放。
1. 生成 Bitmap 资源。
2. 创建一个 CImageList 对象。
3. 将 Bitmap 图片资源加入到 CImageList 中。
4. 得到 CImageList 中的图片资源。
5. 缩放图片。
//
生成 Bitmap 资源
CBitmap m_Bitmap;
m_Bitmap.LoadBitmap (IDB_BITMAP1);
CBitmap m_Bitmap;
m_Bitmap.LoadBitmap (IDB_BITMAP1);
//
严重注意 Create 函数的前三个参数,宽度和高度必须是要加入图片的宽和高,否则如果加入后读取时也会有问题
CImageList ImgList ;
ImgList.Create( 300 , 300 , ILC_COLOR32, 1 , 0 );
CImageList ImgList ;
ImgList.Create( 300 , 300 , ILC_COLOR32, 1 , 0 );
//
将图片添加到 CImageList 中
ImgList.Add( & m_Bitmap,RGB( 0 , 0 , 0 )) ;
ImgList.Add( & m_Bitmap,RGB( 0 , 0 , 0 )) ;
//
得到 CImageList 中的资源
void GetListImage(CImageList & ImageList,CBitmap & Bitmap , int nImageIdx)
... {
CClientDC dcClient(NULL);
IMAGEINFO ImageInfo;
ImageList.GetImageInfo(nImageIdx, &ImageInfo);
int nWidth = abs(ImageInfo.rcImage.right - ImageInfo.rcImage.left);
int nHeight = abs(ImageInfo.rcImage.bottom - ImageInfo.rcImage.top);
Bitmap.CreateCompatibleBitmap(&dcClient, nWidth, nHeight);
CDC dcMemory;
dcMemory.CreateCompatibleDC((CDC*)NULL) ;
CBitmap * pBmpOld = dcMemory.SelectObject(&Bitmap);
dcMemory.FillSolidRect(0, 0, nWidth, nHeight, RGB(255,255,255));
ImageList.Draw(&dcMemory, nImageIdx, CPoint(0,0), ILD_NORMAL);
dcMemory.SelectObject(pBmpOld);
dcMemory.DeleteDC();
::DeleteObject( ImageInfo.hbmImage );
::DeleteObject( ImageInfo.hbmMask );
}
void GetListImage(CImageList & ImageList,CBitmap & Bitmap , int nImageIdx)
... {
CClientDC dcClient(NULL);
IMAGEINFO ImageInfo;
ImageList.GetImageInfo(nImageIdx, &ImageInfo);
int nWidth = abs(ImageInfo.rcImage.right - ImageInfo.rcImage.left);
int nHeight = abs(ImageInfo.rcImage.bottom - ImageInfo.rcImage.top);
Bitmap.CreateCompatibleBitmap(&dcClient, nWidth, nHeight);
CDC dcMemory;
dcMemory.CreateCompatibleDC((CDC*)NULL) ;
CBitmap * pBmpOld = dcMemory.SelectObject(&Bitmap);
dcMemory.FillSolidRect(0, 0, nWidth, nHeight, RGB(255,255,255));
ImageList.Draw(&dcMemory, nImageIdx, CPoint(0,0), ILD_NORMAL);
dcMemory.SelectObject(pBmpOld);
dcMemory.DeleteDC();
::DeleteObject( ImageInfo.hbmImage );
::DeleteObject( ImageInfo.hbmMask );
}
//
缩放图片
void ScaleBitmap(CBitmap * pBitmap,CBitmap & BitmapNew, int nWidth, int nHeight)
... {
CDC dcSrc,dcDst ;
int nWidthOld, nHeightOld ;
int nWidthNew, nHeightNew ;
BITMAP pBitMap ;
pBitmap->GetBitmap(&pBitMap) ;
nWidthOld = pBitMap.bmWidth ;
nHeightOld = pBitMap.bmHeight ;
nWidthNew = nWidth ;
nHeightNew = nHeight ;
// Create DC
dcSrc.CreateCompatibleDC((CDC*)NULL);
dcDst.CreateCompatibleDC((CDC*)NULL);
// Source Bitmap
dcSrc.SelectObject(pBitmap);
// New Bitmap
BitmapNew.CreateCompatibleBitmap(&dcSrc,nWidthNew,nHeightNew);
// Scale Bitmap
dcDst.SelectObject(&BitmapNew);
//Maps pixels from the source rectangle into blocks of pixels
//in the destination rectangle. The average color over
//the destination block of pixels approximates the color of the source pixels.
dcDst.SetStretchBltMode(HALFTONE) ;
dcDst.StretchBlt(0, 0, nWidthNew, nHeightNew, &dcSrc, 0, 0, nWidthOld, nHeightOld, SRCCOPY);
// Free Resource
dcSrc.DeleteDC() ;
dcDst.DeleteDC() ;
}
void ScaleBitmap(CBitmap * pBitmap,CBitmap & BitmapNew, int nWidth, int nHeight)
... {
CDC dcSrc,dcDst ;
int nWidthOld, nHeightOld ;
int nWidthNew, nHeightNew ;
BITMAP pBitMap ;
pBitmap->GetBitmap(&pBitMap) ;
nWidthOld = pBitMap.bmWidth ;
nHeightOld = pBitMap.bmHeight ;
nWidthNew = nWidth ;
nHeightNew = nHeight ;
// Create DC
dcSrc.CreateCompatibleDC((CDC*)NULL);
dcDst.CreateCompatibleDC((CDC*)NULL);
// Source Bitmap
dcSrc.SelectObject(pBitmap);
// New Bitmap
BitmapNew.CreateCompatibleBitmap(&dcSrc,nWidthNew,nHeightNew);
// Scale Bitmap
dcDst.SelectObject(&BitmapNew);
//Maps pixels from the source rectangle into blocks of pixels
//in the destination rectangle. The average color over
//the destination block of pixels approximates the color of the source pixels.
dcDst.SetStretchBltMode(HALFTONE) ;
dcDst.StretchBlt(0, 0, nWidthNew, nHeightNew, &dcSrc, 0, 0, nWidthOld, nHeightOld, SRCCOPY);
// Free Resource
dcSrc.DeleteDC() ;
dcDst.DeleteDC() ;
}