Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to use bitblt api

Posted on 1999-07-20
16
Medium Priority
?
461 Views
Last Modified: 2010-11-09
can any expert show me how to use bitblt api.
0
Comment
Question by:tanc02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
16 Comments
 
LVL 6

Expert Comment

by:setiawan
ID: 1526079
0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526080
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.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1526081
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 ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Accepted Solution

by:
stewfidgeon earned 100 total points
ID: 1526082
Thanks Tanc02

Did you understand my code? You'd be the first :)

Lemme know if you need anymore help.


0
 
LVL 1

Author Comment

by:tanc02
ID: 1526083
stewfidgeon ,

plase give me few days to evaluate your code
0
 
LVL 1

Author Comment

by:tanc02
ID: 1526084
when I hit the button, I got this error :
 
   user defined type not defined

then this "Dim rcWinRect As Rectangle" is highlighted. Why ?
0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526085
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

0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526086
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.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1526087
I got this GetDesktopWindow()  function not defined error !
Please check the code again. Thanks !
0
 
LVL 1

Author Comment

by:tanc02
ID: 1526088
I got this GetDesktopWindow()  function not defined error !
Please check the code again. Thanks !
0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526089
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...

0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526090
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.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1526091
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 ?
0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526092
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.
0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526093
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.
0
 
LVL 1

Expert Comment

by:stewfidgeon
ID: 1526094
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question