Есть следующий код:
- Код: Выделить всё
...
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
...
Private Enum BitmapColorUsage
DIB_RGB_COLORS = 0
DIB_PAL_COLORS = 1
End Enum
Private Type RGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BitmapInfoHeader
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type BitmapInfo
bmiHeader As BitmapInfoHeader
bmiColors As RGBQuad
End Type
Private Declare Function GetDIBits Lib "gdi32" (ByVal hDC As Long, ByVal hBitmap As Long, ByVal StartScan As Long, ByVal NumScans As Long, pBits As Any, pBitmapInfo As BitmapInfo, ByVal ColorUsage As BitmapColorUsage) As Long
...
Private Function PictureToString(pImage As StdPicture) As String
If pImage Is Nothing Then Exit Function
Dim A() As Byte, BI As BitmapInfo, I As Long, hDC As Long
hDC = CreateCompatibleDC(0&)
SelectObject hDC, pImage.Handle
BI.bmiHeader.biSize = Len(BI.bmiHeader)
I = GetDIBits(hDC, pImage.Handle, 0&, 0&, ByVal 0&, BI, DIB_RGB_COLORS)
If BI.bmiHeader.biSizeImage = 0 Then
I = BI.bmiHeader.biHeight * BI.bmiHeader.biWidth * BI.bmiHeader.biBitCount / 8
Else
I = BI.bmiHeader.biSizeImage
End If
I = (CLng(I + 3) \ 4) * 4
ReDim A(0 To I - 1)
I = GetDIBits(hDC, pImage.Handle, 0&, BI.bmiHeader.biHeight, A(0), BI, DIB_RGB_COLORS)
DeleteDC hDC
...
End Function
И эта сволочь падает при выборке битов в массив (вторая I = GetDIBits...). Пример из API-Guide работает на ура. Вся разница в том, что в API-Guide hBitmap создается API, а у меня используется StdPicture.Handle. Но в MSDN сказано, что .Handle является hBitmap. Это я гоню или MSDN обманывает?
Причем, в первый раз GetDIBits отрабатывает (когда вместо буфера передается NULL, то функция заполняет BITMAPINFO, причем заполняет правильно). Но при втором вызове вылетает.