Option Explicit
Public Const BI_RGB = 0&
Public Const BI_RLE4 = 2&
Public Const BI_RLE8 = 1&
Public Const DIB_RGB_COLORS = 0 ' color table in RGBs
Public Const DIB_PAL_COLORS = 1 ' color table in palette indices
Public Const DIB_PAL_INDICES = 2 ' No color table indices into surf palette
Public Const DIB_PAL_PHYSINDICES = 2 ' No color table indices into surf palette
Public Const DIB_PAL_LOGINDICES = 4 ' No color table indices into DC palette
Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Public Type SAFEARRAYBOUND
cElements As Long
lLbound As Long
End Type
Public Type SAFEARRAY2D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
Bounds(0 To 1) As SAFEARRAYBOUND
End Type
Public Type BITMAPINFOHEADER '40 bytes
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
Public Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As Long ' RGBQUAD
End Type
Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long
Public Declare Function GetObjectType Lib "gdi32" (ByVal hgdiobj As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Public Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Public Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpbi As BITMAPINFO, ByVal wUsage As Long) As Long
Public Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpbi As BITMAPINFO, ByVal wUsage As Long) As Long
Public Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As Long
Public Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Public Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function GetBitmapData(hdc As Long, Width As Long, Height As Long, value() As Byte, Optional ByVal ReSize As Double = 1) As Boolean
Dim bi As BITMAPINFO, mhDC As Long, bitsPtr As Long, hDIB As Long
Dim bDibFrom() As Byte, Size As Long, old_bmp As Long, Ret As Long
Dim RWidth As Integer, RHeight As Integer
Dim tSAFrom As SAFEARRAY2D
mhDC = CreateCompatibleDC(0)
If mhDC <> 0 Then
With bi.bmiHeader
.biSize = Len(bi.bmiHeader)
If ReSize <> 1 Then
RWidth = Width * ReSize
.biWidth = RWidth
RHeight = Height * ReSize
.biHeight = RHeight
Else
.biWidth = Width
.biHeight = Height
End If
.biPlanes = 1
.biBitCount = 24
.biCompression = BI_RGB
.biSizeImage = BytesPerScanLine(.biWidth, .biBitCount) * .biHeight
End With
hDIB = CreateDIBSection(mhDC, bi, DIB_RGB_COLORS, bitsPtr, 0, 0)
If hDIB <> 0 Then
old_bmp = SelectObject(mhDC, hDIB)
If ReSize <> 1 Then
Ret = StretchBlt(mhDC, 0, 0, Width * ReSize, Height * ReSize, hdc, 0, 0, Width, Height, SRCCOPY)
Else
Ret = BitBlt(mhDC, 0, 0, Width, Height, hdc, 0, 0, SRCCOPY)
End If
Else
DeleteDC mhDC
Exit Function
End If
End If
Size = bi.bmiHeader.biSizeImage
If (Size > 0) Then
ReDim value(Size - 1)
With tSAFrom
.cbElements = 1
.cDims = 2
.Bounds(0).lLbound = 0
.Bounds(0).cElements = bi.bmiHeader.biHeight
.Bounds(1).lLbound = 0
.Bounds(1).cElements = BytesPerScanLine(bi.bmiHeader.biWidth, bi.bmiHeader.biBitCount)
.pvData = bitsPtr
End With
CopyMemory ByVal VarPtrArray(bDibFrom()), VarPtr(tSAFrom), 4
CopyMemory value(0), bDibFrom(0, 0), Size
'Clear the temporary array descriptor, This is necessary under NT4.
CopyMemory ByVal VarPtrArray(bDibFrom), 0&, 4
End If
DeleteObject hDIB
SelectObject mhDC, old_bmp
DeleteDC mhDC
GetBitmapData = True
End Function
Public Function SetBitmapData(ByVal hdc As Long, ByVal Width As Long, ByVal Height As Long, ByVal value As Long, Optional ByVal ReSize As Double = 1) As Boolean
Dim bi As BITMAPINFO, mhDC As Long, bitsPtr As Long, hDIB As Long
Dim bDibFrom() As Byte, old_bmp As Long, Ret As Long
mhDC = CreateCompatibleDC(0)
If mhDC <> 0 Then
With bi.bmiHeader
.biSize = Len(bi.bmiHeader)
.biWidth = Width
.biHeight = Height
.biPlanes = 1
.biBitCount = 24
.biCompression = BI_RGB
.biSizeImage = BytesPerScanLine(.biWidth, .biBitCount) * .biHeight
End With
hDIB = CreateDIBSection(mhDC, bi, DIB_RGB_COLORS, bitsPtr, 0, 0)
If hDIB <> 0 Then
old_bmp = SelectObject(mhDC, hDIB)
Ret = SetDIBits(mhDC, hDIB, 0, bi.bmiHeader.biHeight, ByVal value, bi, DIB_RGB_COLORS)
If ReSize <> 1 Then
Ret = StretchBlt(hdc, 0, 0, Width * ReSize, Height * ReSize, mhDC, 0, 0, Width, Height, SRCCOPY)
Else
Ret = BitBlt(hdc, 0, 0, Width, Height, mhDC, 0, 0, SRCCOPY)
End If
Else
DeleteDC mhDC
Exit Function
End If
End If
DeleteObject hDIB
SelectObject mhDC, old_bmp
DeleteDC mhDC
SetBitmapData = Ret > 0
End Function
Public Function BytesPerScanLine(Width As Long, BitCount As Integer) As Long
BytesPerScanLine = (Width * BitCount)
If (BytesPerScanLine Mod 32 > 0) Then BytesPerScanLine = BytesPerScanLine + 32 - (BytesPerScanLine Mod 32)
BytesPerScanLine = BytesPerScanLine \ 8
End Function