'
'
' GDI Image Tools (v1.0)
' Copyright 2001, Kevin Gadd & Benjamin Marty
' You are free to use this code in your own applications, and reproduce it on web pages,
' Provided you leave this header intact.
'
'
Option Explicit
Public Type GITRect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Type iid
x As Long
s1 As Integer
s2 As Integer
c(0 To 7) As Byte
End Type
Public Type PICTDESC
cbSizeOfStruct As Long
picType As Long
hBitmap As Long
hPal As Long
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 RGBQUAD
RGBBlue As Byte
RGBGreen As Byte
RGBRed As Byte
rgbReserved As Byte
End Type
Public Type RGBTRIPLE
rgbtBlue As Byte
rgbtGreen As Byte
rgbtRed As Byte
End Type
Public Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Public Const DIB_RGB_COLORS = 0
Public Const BI_RGB = 0&
Public Const OBJ_PAL = 5
Public Const BLACK_BRUSH = 4
Public Const S_OK = &H0
Public Enum picType ' IPictureDisp.Type
PICTYPE_UNINITIALIZED = -1
PICTYPE_NONE = 0
PICTYPE_BITMAP = 1
PICTYPE_METAFILE = 2
PICTYPE_ICON = 3
PICTYPE_ENHMETAFILE = 4
End Enum
Public Declare Function GetCurrentObject Lib "gdi32" (ByVal hDC As Long, ByVal uObjectType As Long) As Long
Public Declare Function GetStockObject Lib "gdi32" (ByVal nIndex 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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function FillRect Lib "user32" (ByVal hDC As Long, lpRect As GITRect, ByVal hBrush As Long) 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 SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function OleCreatePictureIndirect Lib "olepro32" (ByRef pPictDesc As PICTDESC, ByRef riid As iid, ByVal fOwn As Boolean, ByRef ppvObj As StdPicture) As Long
Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function StretchDIBits Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal Width As Long, ByVal Height As Long, ByVal x2 As Long, ByVal y2 As Long, ByVal sWidth As Long, ByVal sHeight As Long, ByVal PointerToBits As Long, lpBitsInfo As BITMAPINFO, ByVal wUsage As Long, ByVal dwRop As Long) As Long
Public Declare Function GetDIBits 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 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 DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Public Function CreateMemoryDC() As Long
On Error Resume Next
Dim deskWnd As Long, deskDC As Long
deskWnd = GetDesktopWindow
deskDC = GetDC(deskWnd)
CreateMemoryDC = CreateCompatibleDC(deskDC)
ReleaseDC deskWnd, deskDC
End Function
Public Sub DeleteMemoryDC(hDC As Long)
On Error Resume Next
DeleteDC hDC
End Sub
' Written by Kevin Gadd
Function GetPictureArray(ByRef Pic As IPictureDisp) As Long()
On Error Resume Next
Dim MinX As Long, MinY As Long
Dim MaxX As Long, MaxY As Long
Dim SetX As Long, SetY As Long
Dim DestAddress As Long, YOffset As Long
Dim bmiDest As BITMAPINFO
Dim DIBitsDest() As Long, DC As Long
DC = CreateMemoryDC
With bmiDest.bmiHeader
.biSize = Len(bmiDest.bmiHeader)
.biPlanes = 1
End With
If 0 = GetDIBits(DC, Pic.Handle, 0, 0, ByVal 0&, bmiDest, DIB_RGB_COLORS) Then
DeleteMemoryDC DC
Exit Function
End If
With bmiDest.bmiHeader
.biBitCount = 32
.biCompression = BI_RGB
End With
YOffset = (bmiDest.bmiHeader.biHeight - 1) - MaxY
ReDim DIBitsDest(0 To bmiDest.bmiHeader.biWidth * bmiDest.bmiHeader.biHeight - 1)
bmiDest.bmiHeader.biHeight = -bmiDest.bmiHeader.biHeight
If 0 = GetDIBits(DC, Pic.Handle, 0, Abs(bmiDest.bmiHeader.biHeight), DIBitsDest(0), bmiDest, DIB_RGB_COLORS) Then
DeleteMemoryDC DC
Exit Function
End If
bmiDest.bmiHeader.biHeight = -bmiDest.bmiHeader.biHeight
GetPictureArray = DIBitsDest
DeleteMemoryDC DC
End Function
' Written by Kevin Gadd
Sub DrawArrayToDC(ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByRef Data() As Long)
On Error Resume Next
Dim bitDesc As BITMAPINFO
With bitDesc.bmiHeader
.biSize = Len(bitDesc.bmiHeader)
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
.biWidth = UBound(Data, 1) + 1
.biHeight = -(UBound(Data, 2) + 1)
.biSizeImage = 0
.biXPelsPerMeter = 0
.biYPelsPerMeter = 0
.biClrImportant = vbNull
.biClrUsed = vbNull
End With
StretchDIBits hDC, x, y, UBound(Data, 1) + 1, UBound(Data, 2) + 1, 0, 0, UBound(Data, 1) + 1, UBound(Data, 2) + 1, VarPtr(Data(0, 0)), bitDesc, DIB_RGB_COLORS, vbSrcCopy
End Sub
' Written by Benjamin Marty
Public Function CapturePicture(ByVal hDC As Long, ByVal Left As Long, ByVal Top As Long, ByVal Width As Long, ByVal Height As Long) As IPictureDisp
Dim picGuid As iid
Dim picDesc As PICTDESC
Dim hdcMem As Long
Dim hBmp As Long
Dim hOldBmp As Long
Dim Pic As IPictureDisp
Dim rcBitmap As GITRect
' IID_IPictureDisp
picGuid.x = &H7BF80981
picGuid.s1 = &HBF32
picGuid.s2 = &H101A
picGuid.c(0) = &H8B
picGuid.c(1) = &HBB
picGuid.c(2) = &H0
picGuid.c(3) = &HAA
picGuid.c(4) = &H0
picGuid.c(5) = &H30
picGuid.c(6) = &HC
picGuid.c(7) = &HAB
picDesc.cbSizeOfStruct = Len(picDesc)
hdcMem = CreateCompatibleDC(hDC)
If hdcMem = 0 Then
Exit Function
End If
hBmp = CreateCompatibleBitmap(hDC, Width, Height)
If hBmp = 0 Then
DeleteDC hdcMem
Exit Function
End If
hOldBmp = SelectObject(hdcMem, hBmp)
If Left >= 0 And Top >= 0 Then
If BitBlt(hdcMem, 0, 0, Width, Height, hDC, Left, Top, vbSrcCopy) = 0 Then
SelectObject hdcMem, hOldBmp
DeleteDC hdcMem
DeleteObject hBmp
Exit Function
End If
Else
With rcBitmap
.Left = 0
.Top = 0
.Right = Width
.Bottom = Height
End With
FillRect hdcMem, rcBitmap, GetStockObject(BLACK_BRUSH)
End If
SelectObject hdcMem, hOldBmp
picDesc.hBitmap = hBmp
picDesc.hPal = GetCurrentObject(hDC, OBJ_PAL)
picDesc.picType = PICTYPE_BITMAP
If OleCreatePictureIndirect(picDesc, picGuid, True, Pic) <> S_OK Then
DeleteDC hdcMem
DeleteObject hBmp
Exit Function
End If
Set CapturePicture = Pic
Set Pic = Nothing
DeleteDC hdcMem
End Function