gdiplus Bitmap 转 gdi BITMAP

来源:互联网 发布:linux挂载新硬盘 编辑:程序博客网 时间:2024/05/22 10:57

1,通过GetHBITMAP()实现
在笔者的机子上,不管PixelFormat是8bit,24bit,还是32bit,通过GetHBITMAP得到的HBITMAP,永远是32bit.
通过以下方法查看BITMAP位深度:
HBitmap to BITMAP:

BITMAP bmp; ::GetObject(hbmp,sizeof(BITMAP),&bmp);

分析原因,可能是HBIMAP是DDB,与设备相关,所以图像的位深度是跟显卡的位数相关的。


2,通过LockBits()实现

LPBYTE ImgUtl::GetBitmapData(void *bmpDst){Bitmap *bitmap = (Bitmap*)bmpDst;Gdiplus::BitmapData bitmapData;Gdiplus::Rect rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());BYTE* buffer = NULL;//get the bitmap dataif(Gdiplus::Ok == bitmap->LockBits(&rect, //A rectangle structure that specifies the portion of the Bitmap to lock.Gdiplus::ImageLockModeRead, //ImageLockMode values that specifies the access level (read) for the Bitmap.           bitmap->GetPixelFormat(),// PixelFormat values that specifies the data format of the Bitmap.&bitmapData //BitmapData that will contain the information about the lock operation.)){//get the length of the bitmap data in bytesint len = bitmapData.Height * abs(bitmapData.Stride);//new bufferbuffer = new BYTE[len];//copy it to an array of BYTEsmemcpy(buffer, bitmapData.Scan0, len);//cleanupbitmap->UnlockBits(&bitmapData);       }return buffer;}



 

0 0