A
active
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
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