CImage转换Gdiplus::Bitmap

来源:互联网 发布:java 有序集合 编辑:程序博客网 时间:2024/05/18 02:59
Gdiplus::Bitmap* CImage2Image(const CImage* pImage )
{
if(!pImage)
return NULL;


Gdiplus::Bitmap* image = new Gdiplus::Bitmap( pImage->GetWidth(), pImage->GetHeight() );
Gdiplus::Rect bound( 0, 0, pImage->GetWidth(), pImage->GetHeight() );
Gdiplus::BitmapData lockedBitmapData;
int bpp = pImage->GetBPP();
int imageRowSize = pImage->GetWidth() * (bpp/8);


if ( bpp == 24 )
{
image->LockBits( &bound, Gdiplus::ImageLockModeWrite, PixelFormat24bppRGB, &lockedBitmapData );
}
else if ( bpp == 32 )
{
image->LockBits( &bound, Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &lockedBitmapData );
}
else
{
// we shouldn't be getting here
AfxDebugBreak();
return NULL;
}


BYTE* pSrcPointer = (BYTE*)pImage->GetBits();
BYTE* pDstPointer = (BYTE*)lockedBitmapData.Scan0;


for ( int i=0; i<pImage->GetHeight(); i++ )
{
memcpy( pDstPointer, pSrcPointer, imageRowSize );
pSrcPointer += pImage->GetPitch();
pDstPointer += lockedBitmapData.Stride;
}


image->UnlockBits( &lockedBitmapData );
return image;
}
原创粉丝点击