-
bitmaps in vb6
Hi all,
I have a strange problem. The following code works very well. I copied it off of the net and although I would like to give credit where credit is due, there is no copyright notice or any id. The code returns a picturebox of a portion of the active screen. The problem is: I don't want a picturebox, I want a bitmap array I can process. more exactly, i need a bitmap and a handle to it, as in "c1 = GetPixel(bitmapHdl, x, y)".
Now I am pretty sure that somewhere in this code is the bitmap and the handle to it I need, I just don't know which handle it is.
I would like it very much if someone could tell me which handle it is.
Thanks
droose
code follows:
Public Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Public Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
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 BitBlt Lib "gdi32" ( _
ByVal hDCDest As Long, ByVal XDest As Long, _
ByVal YDest As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal hDCSrc As Long, _
ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) _
As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function SelectObject Lib "gdi32" ( _
ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare Function OleCreatePictureIndirect _
Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Public Function getBitmap(x As Integer, y As Integer, w As Integer, h As Integer) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hWndSrc As Long
Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
hWndSrc = GetForegroundWindow()
hDCSrc = GetWindowDC(hWndSrc) 'Get DC for entire window.
hDCMemory = CreateCompatibleDC(hDCSrc)
hBmp = CreateCompatibleBitmap(hDCSrc, w, h)
hBmpPrev = SelectObject(hDCMemory, hBmp)
r = BitBlt(hDCMemory, 0, 0, w, h, hDCSrc, x, y, vbSrcCopy)
hBmp = SelectObject(hDCMemory, hBmpPrev)
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic) ' Length of structure
.Type = vbPicTypeBitmap ' Type of Picture (bitmap)
.hBmp = hBmp ' Handle to bitmap
.hPal = 0 ' Handle to palette (may be null)
End With
r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
Set getBitmap = IPic
End Function
-
use the Handle property of the StdPicture object to get back the handle of the bitmap, and then use the GetDIBBits API to get the image byte array from the bitmap
You can also get back the bitmap handle directly (is the hbmp variable), but then you have to deal with releasing the memory yourself (something that right now is done automatically when the picture object goes out of scope)
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
bitmaps in vb6
Hi Marco,
Thank you for the tip. I spent several hours trying to get it to work without any luck. Although I did learn a lot. In the process I came across what I think may be a very good soultion for what I want. mainly to get rid of the picture boxes.
Thanks again,
**** roose
Similar Threads
-
Replies: 246
Last Post: 10-26-2002, 12:30 AM
-
By Patrick Troughton in forum .NET
Replies: 187
Last Post: 09-11-2002, 11:22 AM
-
By Richard Curzon in forum .NET
Replies: 3
Last Post: 07-21-2001, 02:32 PM
-
By Mark Burns in forum .NET
Replies: 24
Last Post: 02-09-2001, 01:18 PM
-
By Esmond Hart in forum .NET
Replies: 2
Last Post: 01-29-2001, 07:44 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|