VB Code:
Option Explicit
Private 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
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 SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal HDC As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal HDC As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private 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
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private 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
Private Const SRCCOPY = &HCC0020
Private ImgData() As Byte
Private PicInfo As BITMAP
Private DIBInfo As BITMAPINFO
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
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 RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Sub Command1_Click()
'On Error GoTo Err:
Dim hdcNew As Long
Dim oldhand As Long
Dim ret As Long
Dim Pic As IPictureDisp
Set Pic = LoadPicture("C:\picture.jpg")
Call GetObject(Pic, Len(PicInfo), PicInfo)
hdcNew = CreateCompatibleDC(0&)
oldhand = SelectObject(hdcNew, Pic)
With DIBInfo.bmiHeader
.biSize = 40
.biWidth = PicInfo.bmWidth
.biHeight = PicInfo.bmHeight
.biPlanes = 1
.biBitCount = 32
End With
ReDim ImgData(1 To 4, 1 To PicInfo.bmWidth, 1 To PicInfo.bmHeight) As Byte
ret = GetDIBits(hdcNew, Pic, 0, PicInfo.bmHeight, ImgData(1, 1, 1), DIBInfo, 0)
Dim i As Long, j As Long
Dim r As Integer, g As Integer, b As Integer
For i = 1 To PicInfo.bmWidth 'Step 1
For j = 1 To PicInfo.bmHeight 'Step -1
r = ImgData(1, i, j)
g = ImgData(2, i, j)
b = ImgData(3, i, j)
r = (r + g + b) / 3
ImgData(1, i, j) = r
ImgData(2, i, j) = r
ImgData(3, i, j) = r
Next j
Next i
ret = SetDIBits(hdcNew, Pic, 0, PicInfo.bmHeight, ImgData(1, 1, 1), DIBInfo, 0)
BitBlt Me.HDC, 0, 0, PicInfo.bmWidth, PicInfo.bmHeight, hdcNew, 0, 0, SRCCOPY
Refresh
Set Pic = Nothing
End Sub
That grayscales an image...like you wanted :)