By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,755 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

GetDIBBits does not fill the array corectly

P: n/a
I have another post asking how to fill in the structure but I now think
GetDIBBits is the way, I just can't get it to work!

I can make the following run but GetDIBBits does not fill the array to
correctly reflect the data from the bitmap.

Any helpful suggestions would be appreciated.

Dim hInBitmap As IntPtr = InBitmap.GetHbitmap()

'Dim hInBitmapDC As IntPtr = GetDC(hInBitmap)

Dim hInBitmapDC As IntPtr = GetDC(Nothing)

Dim dibInfo As New BitmapInfo

Dim dibInfoPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dibInfo))

If GetDIBits(hInBitmapDC, hInBitmap, 0, 0, Nothing, dibInfoPtr,
DIB_RGB_COLORS) = 0 Then

DeleteObject(hInBitmap)

Exit Sub

End If

dibInfo = CType(Marshal.PtrToStructure(dibInfoPtr, dibInfo.GetType),
BitmapInfo)

Marshal.FreeHGlobal(dibInfoPtr)

ReleaseDC(hInBitmap, hInBitmapDC)

Above: Don't know which GetDC is correct?

Public Declare Auto Function GetDIBits Lib "gdi32" (ByVal hDC As IntPtr,
ByVal hBitmap As IntPtr, ByVal nStartScan As Integer, ByVal nNumScans As
Integer, ByRef lpBits As Byte, ByRef lpBI As IntPtr, ByVal wUsage As
Integer) As Integer

Above: Do not know how to use lpBI As BitmapInfo - would that be better?

Below: Don't know how to set the array size at run time when I know how many
colors there are?

Public Structure BitmapInfo

Public bmiHeader As BitmapInfoHeader

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)Public bmiColors() As
RGBQuad

End Structure

Public Structure BitmapInfoHeader

Public biSize As Integer

Public biWidth As Integer

Public biHeight As Integer

Public biPlanes As Short

Public biBitCount As Short

Public biCompression As Integer

Public biSizeImage As Integer

Public biXPelsPerMeter As Integer

Public biYPelsPerMeter As Integer

Public biClrUsed As Integer

Public biClrImportant As Integer

End Structure

Public Structure RGBQuad

Public rgbBlue As Byte

Public rgbGreen As Byte

Public rgbRed As Byte

Public rgbReserved As Byte

End Structure


Aug 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>What is the simplest way to get the BitmapInfo info out of a Bitmap?

Using GetObject with DIBSECTION is the best way to get this information.
Dim hInBitmap As IntPtr = InBitmap.GetHbitmap()

Dim dibInfo As New BitmapInfo

Dim dibInfoPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dibInfo))

If GetObject(hInBitmap, Marshal.SizeOf(dibInfo), dibInfoPtr) <>
Marshal.SizeOf(dibInfo) Then

DeleteObject(hInBitmap)

Exit Sub

End If

I'm back to square one.

That's the first thing I tried.

Can't get past it returning zero for a 256 8bpp bitmap

Public Declare Auto Function GetObject Lib "gdi32.dll" (ByVal hObject As
IntPtr, ByVal nCount As Integer, ByVal nEntries As IntPtr) As Integer
Public Structure BitmapInfo

Public bmiHeader As BitmapInfoHeader

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)Public bmiColors() As
RGBQuad

End Structure

Public Structure BitmapInfoHeader

Public biSize As Integer

Public biWidth As Integer

Public biHeight As Integer

Public biPlanes As Short

Public biBitCount As Short

Public biCompression As Integer

Public biSizeImage As Integer

Public biXPelsPerMeter As Integer

Public biYPelsPerMeter As Integer

Public biClrUsed As Integer

Public biClrImportant As Integer

End Structure
Aug 9 '07 #2

P: n/a
just realize I hadn't changed to
Dim dibInfo As New Wnd.DIBSECTION

I do that now

" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>
>>What is the simplest way to get the BitmapInfo info out of a Bitmap?

Using GetObject with DIBSECTION is the best way to get this information.
Dim hInBitmap As IntPtr = InBitmap.GetHbitmap()

Dim dibInfo As New BitmapInfo

Dim dibInfoPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dibInfo))

If GetObject(hInBitmap, Marshal.SizeOf(dibInfo), dibInfoPtr) <>
Marshal.SizeOf(dibInfo) Then

DeleteObject(hInBitmap)

Exit Sub

End If

I'm back to square one.

That's the first thing I tried.

Can't get past it returning zero for a 256 8bpp bitmap

Public Declare Auto Function GetObject Lib "gdi32.dll" (ByVal hObject As
IntPtr, ByVal nCount As Integer, ByVal nEntries As IntPtr) As Integer
Public Structure BitmapInfo

Public bmiHeader As BitmapInfoHeader

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)Public bmiColors()
As RGBQuad

End Structure

Public Structure BitmapInfoHeader

Public biSize As Integer

Public biWidth As Integer

Public biHeight As Integer

Public biPlanes As Short

Public biBitCount As Short

Public biCompression As Integer

Public biSizeImage As Integer

Public biXPelsPerMeter As Integer

Public biYPelsPerMeter As Integer

Public biClrUsed As Integer

Public biClrImportant As Integer

End Structure


Aug 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.