您的位置:首页 > 其它

CImage 加载透明PNG图片

2015-04-15 15:37 405 查看
CImage m_img;

void LoadImage();

void SetAlphaBits();

void CceshipngView::LoadImage()
{
//第1步 寻找资源

HRSRC hrsrc_resource=::FindResource(NULL,MAKEINTRESOURCE(IDB_NARUTO),"PNG");
if(!hrsrc_resource)
{
MessageBox("FindResource failed");
return;
}

//第2步 加载资源
HGLOBAL hglobal_resource=::LoadResource(NULL,hrsrc_resource);
if(!hglobal_resource)
{
MessageBox("LoadResource failed");
return;
}

//第3步 加载资源
LPVOID pImgData=::LockResource(hglobal_resource);
if(!pImgData)
{
MessageBox("LockResource failed");
return;
}

//第4步 获取图片的大小
DWORD ImgSize=::SizeofResource(NULL,hrsrc_resource);

//第5步 创建IStream
IStream *pStream=NULL;

if(S_OK!=CreateStreamOnHGlobal(NULL,TRUE,&pStream))
{
MessageBox("CreateStreamOnHGlobal failed");
return;
}

ULONG pcbWritten;
pStream->Write(pImgData,ImgSize,&pcbWritten);

m_img.Destroy();
if(S_OK!=m_img.Load(pStream))
{
MessageBox("Load Image failed");
return;
}

pStream->Release();
pStream=NULL;

}

//设置为透明
void CceshipngView::SetAlphaBits()
{
//获取图片的HBITMAP
HBITMAP hbitmap=(HBITMAP)m_img;

BITMAP bitmapInfo;

::GetObject( hbitmap, sizeof(BITMAP), &bitmapInfo);

if(32!=bitmapInfo.bmBitsPixel||NULL==bitmapInfo.bmBits)
{
MessageBox("not a png Image or no Image Data ");
return;
}

BYTE *ptr=(BYTE *)bitmapInfo.bmBits;
for(int i=0;i<bitmapInfo.bmWidth;i++)
for(int j=0;j<bitmapInfo.bmHeight;j++)
{
BYTE alpha=ptr[3];
ptr[0]=(ptr[0]*alpha)/255;
ptr[1]=(ptr[1]*alpha)/255;
ptr[2]=(ptr[2]*alpha)/255;
ptr+=4;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: