位图分为两种DDB和DIB
DIB:即兼容位图,独立于设备存在,有描述颜色格式的信息头,如256色1字节、真色彩argb。存在于内存中,是我们写程序的时候能控制的有位图数据格式的数据
DDB:即设备位图,已经被gdi考到显存后的图像格式,没有信息头,显示格式就是当前的显示设备支持的格式。是我们不能控制的,我们只能通知gdi将某个DIB转化为DDB拷贝到显存显示,是不能直接控制DDB的
获得DIB的数据:
直接用GetBitmapBits
获得DDB的数据:
将位图画到一个自己创建的兼容DC,再创建一个兼容DIB位图,用BitBlt把数据从当前DC拷贝到兼容位图,再用GetBitmapBits得到兼容位图数据。这种方法是能得到a(同明度的值的),只是性能不是很高,更好的办法还不知道,不过gdi好像做不到性能更高的办法
用过GDI画图函数以后,如果BITMAP原来就是DIB那,直接GetBitmapBits就能得到数据,如果是DDB则要创建兼容DC和兼容DIB位图画上然后再在GetBitmapBits。
CreateBitmap 试图创建DDB,失败创建DIB
CreateBitmapIndirect 试图创建DDB,失败创建DIB
CreateCompatibleBitmap 试图创建DDB,失败创建DIB
CreateDIBitmap 试图创建DDB,失败创建DIB
CreateDIBSection 直接创建DIB
具体代码:
DIB获取:
LPVOID bitmapData=NULL; hBitmap =CreateCompatibleBitmap(h,width,height); GetBitmapBits(hBitmap,width*height*4,bitmapData); for(int i=0 ;i<width*height;i++){ unsigned char b = *((char*)bitmapData+i*4) ; unsigned char g = *((char*)bitmapData+i*4+1) ; unsigned char r = *((char*)bitmapData+i*4+2) ; unsigned char a = *((char*)bitmapData+i*4+3) ; }
DDB获取:
LPVOID bitmapData=NULL; int width = 100; int height = 100; HDC hdc = GetDC(hWnd);//hWnd为DDB所在窗口句柄 hCDC = CreateCompatibleDC(hdc); hBitmap =CreateCompatibleBitmap(h,width,height); SelectObject(hCDC,hBitmap); BitBlt(hCDC,0,0,width,height,h,0,0,SRCCOPY); GetBitmapBits(hBitmap,width*height*4,bitmapData); for(int i=0 ;i<width*height;i++){ unsigned char b = *((char*)bitmapData+i*4) ; unsigned char g = *((char*)bitmapData+i*4+1) ; unsigned char r = *((char*)bitmapData+i*4+2) ; unsigned char a = *((char*)bitmapData+i*4+3) ; }