We help IT Professionals succeed at work.

createDC

sidwelle
sidwelle asked
on
I am trying to use the following code snip but it always get a GPF on the CeateDC statement.

Is the command being used wrong ?

thanks

Private Sub ScreenCapture(Left As Long, Top As Long, _
    Right As Long, Bottom As Long)

    ' Captures the specified area of the
    ' screen to the clipboard.
    
    Dim capWidth As Long, capHeight As Long
    Dim SrcDC As Long, DestDC As Long, hBMP As Long
    
    ' Calculate capture area size.
    
    capWidth = Right - Left
    capHeight = Bottom - Top
    
    ' Set up source and destination device contexts.
    
    'SrcDC = CreateDC("DISPLAY", 0, 0, 0)   'Win 95
    SrcDC = CreateDC("DISPLAY", vbNullString, vbNullString, 0) 'NT and later ...
    DestDC = CreateCompatibleDC(SrcDC)
    
    ' Create a bitmap and select it into the destination DC.
    
    hBMP = CreateCompatibleBitmap(SrcDC, capWidth, capHeight)
    SelectObject DestDC, hBMP
    
    ' Transfer the bitmap from the screen DC to the
    ' destination DC.
    
    BitBlt DestDC, 0, 0, capWidth, capHeight, SrcDC, Left, _
        Top, &HCC0020
    
    ' Open the clipboard, empty it, then put
    ' the bitmap on it.
    
    OpenClipboard Screen.ActiveForm.hwnd
    EmptyClipboard
    SetClipboardData 2, hBMP
    CloseClipBoard
    
    ' Clean up.
    
    DeleteObject hBMP
    DeleteDC DestDC
    ReleaseDC 0, SrcDC
    
    ' Enable the Save button.
    
    Command1(1).Enabled = True

End Sub

Open in new window

Comment
Watch Question

Bill NolanOwner, Lead Technology Programmer

Commented:
Haven't done this in awhile, but a couple thoughts:
1) not sure if you are supplying proper parameters to that function: is filename correct? can device name & output be null?
2) you may want to look into GetDC()

Author

Commented:
This is an example that I found on-line and it worked the first couple of times, but then I started to change the params from full screen to just some sections and then it started flaking out.  I put all the code back to the way it was in the original example, but it still GPFs on the CreateDC statement.

it makes me think some resources have been used that have not been released.
I did look up some other examples and even found a example that stated these values were correct.
Except for the output device, but this is how the example used it.


Bill NolanOwner, Lead Technology Programmer

Commented:
Have you tested your suspicion about overloading devices (not releasing them)?
E.g.:
1) does the problem occur on the FIRST attempt at CreateDC()?  If not, when?
2) does the problem disappear when you restart everything - even the OS?

Author

Commented:
yes, but the app usually GPFs then and takes VB out with it.
So last 10 attempts have been from fresh IDE.  Havn't reset the OS. I will try that tomorrow.
Bill NolanOwner, Lead Technology Programmer

Commented:
Again, it's been awhile, but if I remember correctly that is one advantage of GetDC() - you will always get one.  Anyway, check those two things out and good luck.

Author

Commented:
I am trying to read pixels from the screen or another app.

I found another simpler example:

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

lngScreen = GetWindowDC(0)
lngColor = GetPixel(lngScreen, X, Y)

'Could I use this DC with the BitBlt function ??


Owner, Lead Technology Programmer
Commented:
Yes.

Author

Commented:
Works thanks