記憶中獲取dib位圖是有順序一說的,,,今天被坑了一把確實是有一說,是和圖像的格式相關的,MSDN上這樣描述:
“
A pointer to the bitmap buffer. If the bitmap is a bottom-up DIB, the pointer points near the end of the buffer. If the bitmap is a top-down DIB, the pointer points to the first byte of the buffer.
”
摘錄網上查到的筆記:
“
CImage類提供了GetBits()函數來讀取數據區,GetBits()函數返回的是圖片最后一行第一個像素的地址,網上有人說返回指針的起始位置是不同的,有些圖片返回的是左上角像素的地址,有些是左下角像素的地址,跟圖片內部順序有關。這里我們不必關心起始位置,只要很另外兩個函數GetPitch()和GetHeight()一起使用就可以得到圖片數據取得起始位置,定義數據區指針為BYTE* img_Data
img_Data=(BYTE *)m_Image.GetBits()+(m_Image.GetPitch()*(m_Image.GetHeight()-1));
這樣,img_Data就是圖片數據區的起始位置,這個公式是從codeproject里看到的,介紹的很精辟,可以從google里搜索到。其中GetHeight()函數返回圖片的高度(以像素為單位)。GetPitch()返回圖像的斜度,如果圖像的順序是從下到上(也就是GetBits()返回左上角像素的地址),這時GetPitch()返回一個負值,大小為圖像寬所占有的字節數,例如24位800*600的圖片,返回值應該是正或負的800*3。這樣用每一行的字節數乘行數就可以得到起始位置了。
”
以上是使用CImage獲取的時候遇到的坑爹,相信直接使用位圖相關的api也是一樣,msdn描述:
Device-Independent Bitmaps
There are two varieties of DIBs:
- A bottom-up DIB, in which the origin lies at the lower-left corner.
- A top-down DIB, in which the origin lies at the upper-left corner.
使用gdiplus的時候BitmapData也是類似:
Stride
INT
Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
兩個關鍵屬性:
GetPitch( )
Stride
還有泥馬一個被微軟坑了的壞習慣,DWORD用習慣了,調試了一天都沒看出來這兩個值我取到的是負的,,,坑啊,,,