if (NULL == pBuffer2)
{
::GlobalFree(hGlobal2);
::FreeResource(hGlobal);
return FALSE;
}
memcpy(pBuffer2, pBuffer, dwSize);
::GlobalUnlock(hGlobal2);
LPSTREAM pStream = NULL;
HRESULT hr = ::CreateStreamOnHGlobal(hGlobal2, TRUE, &pStream);
if (hr != S_OK)
{
::GlobalFree(hGlobal2);
::FreeResource(hGlobal);
return FALSE;
}
BOOL bRet = LoadFromIStream(pStream);
if (pStream)
pStream->Release();
::FreeResource(hGlobal);
return bRet;
}
BOOL CImageEx::LoadFromResource(HINSTANCE hInstance, UINT nIDResource, LPCTSTR pszResType)
{
return LoadFromResource(hInstance, MAKEINTRESOURCE(nIDResource), pszResType);
}
void CImageEx::SetNinePart(const RECT * lpNinePart)
{
if ((NULL == lpNinePart) || (0 == lpNinePart->left && 0 == lpNinePart->top
&& 0 == lpNinePart->right && 0 == lpNinePart->bottom))
{
m_bIsNinePart = FALSE;
::SetRectEmpty(&m_rcNinePart);
}
else
{
m_bIsNinePart = TRUE;
m_rcNinePart = *lpNinePart;
}
}
BOOL CImageEx::Draw2(HDC hDestDC, const RECT& rectDest)
{
if (m_bIsNinePart)
{
int nWidth = rectDest.right - rectDest.left;
int nHeight = rectDest.bottom - rectDest.top;
if (GetWidth() != nWidth || GetHeight() != nHeight)
{
return DrawNinePartImage(hDestDC, rectDest.left, rectDest.top, nWidth, nHeight,
m_rcNinePart.left, m_rcNinePart.top, m_rcNinePart.right, m_rcNinePart.bottom);
}
}
return Draw(hDestDC, rectDest);
}
// 图像灰度化
void CImageEx::GrayScale()
{
int nWidth = GetWidth();
int nHeight = GetHeight();
BYTE* pArray = (BYTE*)GetBits();
int nPitch = GetPitch();
int nBitCount = GetBPP() / 8;
for (int i = 0; i < nHeight; i++)
{
for (int j = 0; j < nWidth; j++)
{
int grayVal = (BYTE)(((*(pArray + nPitch * i + j * nBitCount) * 306)
+ (*(pArray + nPitch * i + j * nBitCount + 1) * 601)
+ (*(pArray + nPitch * i + j * nBitCount + 2) * 117) + 512 ) >> 10); // 计算灰度值
*(pArray + nPitch * i + j * nBitCount) = grayVal; // 赋灰度值
*(pArray + nPitch * i + j * nBitCount + 1) = grayVal;
*(pArray + nPitch * i + j * nBitCount + 2) = grayVal;
}
}
}
// Alpha预乘
BOOL CImageEx::AlphaPremultiplication()
{
LPVOID pBitsSrc = NULL;
BYTE * psrc = NULL;
BITMAP stBmpInfo;
HBITMAP hBmp = (HBITMAP)*this;
::GetObject(hBmp, sizeof(BITMAP), &stBmpInfo);
// Only support 32bit DIB section
if (32 != stBmpInfo.bmBitsPixel || NULL == stBmpInfo.bmBits)
return FALSE;
psrc = (BYTE *) stBmpInfo.bmBits;
// Just mix it
for (int nPosY = 0; nPosY < abs(stBmpInfo.bmHeight); nPosY++)
{
for (int nPosX = stBmpInfo.bmWidth; nPosX > 0; nPosX--)
{
BYTE alpha = psrc[3];
psrc[0] = (BYTE)((psrc[0] * alpha) / 255);
psrc[1] = (BYTE)((psrc[1] * alpha) / 255);
psrc[2] = (BYTE)((psrc[2] * alpha) / 255);
psrc += 4;
}
}
return TRUE;
}
BOOL CImageEx::DrawNinePartImage(int pleft, int ptop, int pright, int pbottom,
HDC hDC, int height, int width, int left, int top, int right, int bottom)
{
// 左上
{
CRect rcDest(pleft, ptop, pleft+left, ptop+top);
CRect rcSrc(0, 0, left, top);
if (!rcDest.IsRectEmpty() && !rcSrc.IsRectEmpty())
Draw(hDC, rcDest, rcSrc);
}
// 左边
{
CRect rcDest(pleft, top+ptop, pleft+left, top+(height-top-bottom-ptop-pbottom));
CRect rcSrc(0, top, left, top+(GetHeight()-top-bottom));
if (!rcDest.IsRectEmpty() && !rcSrc.IsRectEmpty())
Draw(hDC, rcDest, rcSrc);
}
// 上边
{
CRect rcDest(left+pleft, ptop, (left+pleft)+(width-left-right-pleft-pright), ptop+top);
CRect rcSrc(left, 0, left+(G |