CImage 加载透明PNG图片

来源:互联网 发布:350淘宝装修平台app 编辑:程序博客网 时间:2024/05/17 21:52
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;
}

}


0 0
原创粉丝点击