Link to home
Start Free TrialLog in
Avatar of tanc02
tanc02

asked on

how to use bitblt api

can any expert show me how to use bitblt api.
Avatar of setiawan
setiawan

Ok, to start of you need to declare the API call, so I put the following line into a module:

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

Ok, thats the API declared, now you just need to use the function.

result = BitBlt(lngHDestDC, lngX, lngY, lngWidth, lngHeight, lngHSrcDC, lngXSrc, lngYSrc, OpConst)

The above line specifies the parameters in the following order
lngHDestDC = A device context to use as the destination
lngX = The leftmost point of the destination area
lngY = the uppermost point of the destination area
lngWidth= the width of both the source image and the destination image
lngHeight = the height of both the source image and the destination image
lngHSrcDC = Device context of the source image
lngSrcX= Leftmost point of the source image
lngSrcY= Uppermost point of the source image
OpConst = a constant that defines the operation to be performed on the image IE SRCCOPY would tell bitblt to copy the image from the source to the destination.

To better illustrate this I will give you a function below which will copy the whole screen or a specific window to a picturebox called Picture1 on a form called Grabber



Function ScreenThief(strCommand As String, strFileName As String) As String

Dim lngResult As Long
Dim rcWinRect As Rectangle
Dim lngHwndSrc As Long
Dim lngHSrcDC As Long
Dim lngXSrc As Long
Dim lngYSrc As Long
Dim lngX As Long
Dim lngY As Long
Dim lngWidth As Long
Dim lngHeight As Long
Dim lngHDestDC As Long
Dim lngSuc As Long
Const SRCOPYY = &HCC0020


Load Grabber

' Decide which window to grab, a user choice, or
' the desktop window.
If strCommand <> vbNullString Then

    ' Check that the window requested actually exists.
    lngHwndSrc = FindWindowEx(vbNullString, vbNullString, 0&, strCommand)
    If lngHwndSrc <= 0 Then
        lngHwndSrc = GetDesktopWindow()
        lngHSrcDC = GetDC(lngHwndSrc)
    End If

    ' Activate the required window.
    Call BringWindowToTop(lngHwndSrc)
    lngHSrcDC = GetWindowDC(lngHwndSrc)
Else
    ' Use the contents of the desktop window.
    lngHwndSrc = GetDesktopWindow()
    lngHSrcDC = GetDC(lngHwndSrc)
End If

' Grab the window.
lngXSrc = 0
lngYSrc = 0
Call GetWindowRect(lngHwndSrc, rcWinRect)
lngWidth = rcWinRect.intRight - rcWinRect.intLeft
lngHeight = rcWinRect.intBottom - rcWinRect.intTop
lngX = 0
lngY = 0
Grabber.Picture1.ScaleMode = 3
Grabber.Picture1.Top = 0
Grabber.Picture1.Left = 0
Grabber.Picture1.Width = (lngWidth + 1)
Grabber.Picture1.Height = (lngHeight + 1)
lngHDestDC = Grabber.Picture1.hdc
lngSuc = BitBlt(lngHDestDC, lngX, lngY, lngWidth, lngHeight, lngHSrcDC, lngXSrc, lngYSrc, SRCOPYY)
lngResult = ReleaseDC(lngHwndSrc, lngHSrcDC)

' Save the image.
SavePicture Grabber.Picture1.Image, strFileName

End Function


Just paste that function into a forms declaration area, create a form called Grabber and on it create a picture box called Picture1
Then just call the function from a command button like this:
Result = ScreenThief(WindowTitle,FileName)

Where windowtitle is the title text of the window, and strFileName is the filename to save the image as.


Hope this helps, if not lemme know and I'll give you more advice.


oops, you're Q's been answered while I was typing this. No matter, Just consider this free advice.
Avatar of tanc02

ASKER

Sorry ! I think it will be fair to give  stewfidgeon the point. And I thnak you very much for trying.

to  stewfidgeon : can you post you idea as answer ?
ASKER CERTIFIED SOLUTION
Avatar of stewfidgeon
stewfidgeon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tanc02

ASKER

stewfidgeon ,

plase give me few days to evaluate your code
Avatar of tanc02

ASKER

when I hit the button, I got this error :
 
   user defined type not defined

then this "Dim rcWinRect As Rectangle" is highlighted. Why ?
Ooops, this bit of code needs to go into the module or declaration section of the form where you put the API calls.
Sorry about that.


Type Rectangle
    intLeft As Long
    intTop As Long
    intRight As Long
    intBottom As Long
End Type

You will also need to put the declaration for FindWindowEx in at the same point as the bitblt declaration.

Ok, so revised version of the code would look like this:


To start off you need to declare the API call, so I put the following line into a module:

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 FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Type Rectangle
    intLeft As Long
    intTop As Long
    intRight As Long
    intBottom As Long
End Type


Ok, thats the API's declared, now you just need to use the function.



Function ScreenThief(strCommand As String, strFileName As String) As String

Dim lngResult As Long
Dim rcWinRect As Rectangle
Dim lngHwndSrc As Long
Dim lngHSrcDC As Long
Dim lngXSrc As Long
Dim lngYSrc As Long
Dim lngX As Long
Dim lngY As Long
Dim lngWidth As Long
Dim lngHeight As Long
Dim lngHDestDC As Long
Dim lngSuc As Long
Const SRCOPYY = &HCC0020


Load Grabber

' Decide which window to grab, a user choice, or
' the desktop window.
If strCommand <> vbNullString Then

    ' Check that the window requested actually exists.
    lngHwndSrc = FindWindowEx(vbNullString, vbNullString, 0&, strCommand)
    If lngHwndSrc <= 0 Then
        lngHwndSrc = GetDesktopWindow()
        lngHSrcDC = GetDC(lngHwndSrc)
    End If

    ' Activate the required window.
    Call BringWindowToTop(lngHwndSrc)
    lngHSrcDC = GetWindowDC(lngHwndSrc)
Else
    ' Use the contents of the desktop window.
    lngHwndSrc = GetDesktopWindow()
    lngHSrcDC = GetDC(lngHwndSrc)
End If

' Grab the window.
lngXSrc = 0
lngYSrc = 0
Call GetWindowRect(lngHwndSrc, rcWinRect)
lngWidth = rcWinRect.intRight - rcWinRect.intLeft
lngHeight = rcWinRect.intBottom - rcWinRect.intTop
lngX = 0
lngY = 0
Grabber.Picture1.ScaleMode = 3
Grabber.Picture1.Top = 0
Grabber.Picture1.Left = 0
Grabber.Picture1.Width = (lngWidth + 1)
Grabber.Picture1.Height = (lngHeight + 1)
lngHDestDC = Grabber.Picture1.hdc
lngSuc = BitBlt(lngHDestDC, lngX, lngY, lngWidth, lngHeight, lngHSrcDC, lngXSrc, lngYSrc, SRCOPYY)
lngResult = ReleaseDC(lngHwndSrc, lngHSrcDC)

' Save the image.
SavePicture Grabber.Picture1.Image, strFileName

End Function



The function is still called the same way, all I have done is declare a few more api functions and provide a TYPE for a rectangle.
Avatar of tanc02

ASKER

I got this GetDesktopWindow()  function not defined error !
Please check the code again. Thanks !
Avatar of tanc02

ASKER

I got this GetDesktopWindow()  function not defined error !
Please check the code again. Thanks !
Doh! I must look so stupid.

You also need the following declarations:

Public Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long

Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long

Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Long) As Long

Public Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long

Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, ByVal hdc As Long) As Long


I'm sorry about this messing about. The function I gave you is a small part of a larger project I'm working on, and I just missed a few of the API Declarations out because they were at the top of my list, not the bottom where the rest of the declare statements were.

If you like, I can email you a working VB6 version of the code, that way I've included everything...

Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As Rectangle) As Long

Is also missing.


Ooops, ok I've made way too many mistakes here, so I'm gonna put this into a zip file on the web, so you can get a working copy.

http://www.geocities.com/TimesSquare/Realm/1273/download.html

There is a file on there called bitblt.zip which contains a working version. You may need to create a new project file and just add the files biblt.frm bitblt.bas and grabber.frm

Let me know if you have any problems.

Oh, and excuse the crappy website, it was just for an assignment many moons ago when I was at Uni.
Avatar of tanc02

ASKER

I have ran your vb program which I got it from your web-site. I can see the
test.bmp in c dir, but there is no picture. And In the Grabber form, I only can
see 1.5cm by 1.5cm picture box with nothing on it. Why ? Any eorror ?
The file should be called testing.bmp, and it should have a picture of the desktop window in it.

Did you use my grabber.frm or did you create your own?

If you made your own grabber form, try it with mine, or set the autoredraw property of picture1 picture box to true and the auto resize to false.

If you cant get my version to work, email me what you have at sfidgeon@geocities.com and I'll try and fix it for you.
Tanc02:

Are you still having problems with my code?

Mail me what you have at the above address and I'll fix it for you.

If you already have it working, please grade my answer, as I have spent quite some time writing it.

Thanks,
Stew.
Tanc02:

Are you still having problems with my code?

Mail me what you have at the above address and I'll fix it for you.

If you already have it working, please grade my answer, as I have spent quite some time writing it.

Thanks,
Stew.