CxImage* ScaleImageBy9PNG(CxImage *pRawImage, int nDstWidth,int nDstHeight) { if(NULL == pRawImage) return NULL; CDC *pDC = CDC::FromHandle(::GetDC(NULL)); CDC memDC; memDC.CreateCompatibleDC(pDC); const UINT nBMPWidth = nDstWidth; const UINT nBMPHeight= nDstHeight; const UINT nRawWidth = pRawImage->GetWidth(); const UINT nRawHeight = pRawImage->GetHeight(); BITMAPINFO bitmapinfo; bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bitmapinfo.bmiHeader.biBitCount = 32; bitmapinfo.bmiHeader.biHeight = nBMPHeight; bitmapinfo.bmiHeader.biWidth = nBMPWidth; bitmapinfo.bmiHeader.biPlanes = 1; bitmapinfo.bmiHeader.biCompression=BI_RGB; bitmapinfo.bmiHeader.biXPelsPerMeter=0; bitmapinfo.bmiHeader.biYPelsPerMeter=0; bitmapinfo.bmiHeader.biClrUsed=0; bitmapinfo.bmiHeader.biClrImportant=0; bitmapinfo.bmiHeader.biSizeImage = bitmapinfo.bmiHeader.biWidth * bitmapinfo.bmiHeader.biHeight * bitmapinfo.bmiHeader.biBitCount / 8; HBITMAP hBitmap = ::CreateDIBSection (memDC.m_hDC,&bitmapinfo, 0,NULL, 0, 0); CxImage *newImage = new CxImage(); newImage->CreateFromHBITMAP(hBitmap); if (hBitmap != NULL) { DeleteObject(hBitmap); } ::ReleaseDC(NULL, pDC->m_hDC); if(memDC.m_hDC) ::DeleteDC(memDC.m_hDC); int newImageHeight = newImage->GetHeight(); int newImageWidth = newImage->GetWidth(); if(FALSE == newImage->AlphaIsValid()) { newImage->AlphaCreate(); } if(newImageHeight > nRawHeight && newImageWidth > nRawWidth) { for(int iH=0;iH<newImageHeight;++iH) for(int iW=0;iW<newImageWidth;++iW) { int rawH = iH,rawW = iW; if(rawH >= (newImageHeight-nRawHeight/2)) { rawH = rawH - (newImageHeight - nRawHeight); } if(rawW >= (newImageWidth-nRawWidth/2)) { rawW = rawW - (newImageWidth - nRawWidth); } if((iH < nRawHeight/2 || iH>=(newImageHeight-nRawHeight/2))&& (iW < nRawWidth/2 || iW>= (newImageWidth-nRawWidth/2))) { //4块源 RGBQUAD argb = pRawImage->GetPixelColor(rawW,rawH,true); newImage->SetPixelColor(iW,iH,RGB(argb.rgbRed,argb.rgbGreen,argb.rgbBlue)); newImage->AlphaSet(iW,iH,argb.rgbReserved); } else { if((iH < nRawHeight/2 || iH>=(newImageHeight-nRawHeight/2))&&iW-1>=0) { RGBQUAD argb = newImage->GetPixelColor(iW-1,iH,true); newImage->SetPixelColor(iW,iH,RGB(argb.rgbRed,argb.rgbGreen,argb.rgbBlue)); newImage->AlphaSet(iW,iH,argb.rgbReserved); } else { if(iH - 1 >= 0) { RGBQUAD argb = newImage->GetPixelColor(iW,iH-1,true); newImage->SetPixelColor(iW,iH,RGB(argb.rgbRed,argb.rgbGreen,argb.rgbBlue)); newImage->AlphaSet(iW,iH,argb.rgbReserved); } } } } } return newImage; }
9PNG的意思就是绘制时按9块区域绘制,左上左下右上右下是源,其他是拉伸的部分。CxImage主要使用像素点的方式来进行拉伸,并且每个像素都有copy透明信息。