Results 1 to 4 of 4

Thread: [resolved] bitblt copy problem..

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19

    [resolved] bitblt copy problem..

    I have a small form. Simply, it has a textbox, a button, and a picturebox.

    On click of the button, I take the window title typed into the text box, find the handle of the window, and do a bitblt of the window's contents into the picture box.

    Everything works except the last part, and I think the sample code I got (the GetShot() func) was written for legacy vb.

    bitblt returns a non-zero return code, which as far as I can find is success, I get no errors, and the picturebox just doesn't contain anything.

    Any help would be appreciated on figuring out where I'm goofing up...

    Thanks.

    (note: this is on xp pro using vs.net 2003)


    VB Code:
    1. Imports System.Drawing.Graphics
    2. Public Class frm_WC
    3.     Inherits System.Windows.Forms.Form
    4.     Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5.     Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long
    6.  
    7.     Private Declare Function GetWindowRect Lib "user32" (ByRef hwnd As Long, ByRef lpRect As S_RECT) As Long
    8.     Private Declare Function GetDesktopWindow Lib "user32" () As Long
    9.  
    10.     Private Declare Function GetActiveWindow Lib "user32" () As Long
    11.     Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
    12.     Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As S_RECT) As Long
    13.     Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    14.     Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As IntPtr, 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
    15.     Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
    16.     Private Const SRCCOPY = &HCC0020
    17.  
    18.     Private Structure S_RECT
    19.         Dim Left As Long
    20.         Dim Top As Long
    21.         Dim Right As Long
    22.         Dim Bottom As Long
    23.     End Structure
    24.     Private Structure S_BITMAP
    25.         Dim bmType As Long
    26.         Dim bmWidth As Long
    27.         Dim bmHeight As Long
    28.         Dim bmWidthBytes As Long
    29.         Dim bmPlanes As Integer
    30.         Dim bmBitsPixel As Integer
    31.         Dim bmBits As Long
    32.     End Structure
    33.  
    34.  
    35. #Region " Windows Form Designer generated code "
    36. '... omitted auto-generated code
    37. #End Region
    38.  
    39.     Private Sub cmd_Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Capture.Click
    40.         Dim lpWindowHandle As Long
    41.         Dim lpWindowHandle2 As Long
    42.         Dim lpWindowHandle3 As Long
    43.         'name of window to capture
    44.         Dim lpWindowName As String = txt_Window.Text.Trim
    45.  
    46.         lpWindowHandle = FindWindow(vbNullString, lpWindowName)
    47.  
    48.         'display window handle
    49.         MsgBox("hwnd returns " & Hex(lpWindowHandle), MsgBoxStyle.OKOnly, "Results")
    50.  
    51.         'bring it to the foreground
    52.         'SetForegroundWindow(lpWindowHandle)
    53.         GetShot(lpWindowHandle, PictureBox1)
    54.  
    55.     End Sub
    56.  
    57.     Private Sub GetShot(ByRef lWindowhWnd As Long, ByRef picScreen As PictureBox)
    58.         Dim nLeft As Long
    59.         Dim nTop As Long
    60.         Dim nWidth As Long
    61.         Dim nHeight As Long
    62.         Dim rRect As New S_RECT
    63.         Dim bm As New S_BITMAP
    64.         Dim lWindowhDC As Long
    65.         Dim vbSrcCopy As Long
    66.         Dim errCode As Integer
    67.  
    68.         'picScreen.Image = Nothing
    69.         GetWindowRect(lWindowhWnd, rRect)
    70.         lWindowhDC = GetWindowDC(lWindowhWnd)
    71.         '// Get coordinates
    72.         nLeft = 0
    73.         nTop = 0
    74.         nWidth = rRect.Right - rRect.Left
    75.         nHeight = rRect.Bottom - rRect.Top
    76.         '// Blt to frm.picScreen
    77.         BitBlt(picScreen.Handle, 0, 0, nWidth, nHeight, lWindowhDC, nLeft, nTop, SRCCOPY)
    78.         '// Del DC
    79.         ReleaseDC(lWindowhWnd, lWindowhDC)
    80.         '// set picture
    81.         picScreen.Refresh()
    82.  
    83.     End Sub
    84. End Class
    Last edited by slanted; Apr 2nd, 2004 at 11:08 AM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you need to use " Integer or Int32 " inplace of your " Long's " when using Apis in .NET
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    I'm not quite sure how that would cause my problem, but I changed everything to integer's, still a no go...

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    After some searching and tweaking I think I got it figured out... atleast, it works.

    VB Code:
    1. Imports System.Drawing.Graphics
    2. Public Class frm_WC
    3.     Inherits System.Windows.Forms.Form
    4.  
    5.     Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    6.     Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Integer) As Integer
    7.     Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _
    8.          (ByVal hDestDC As IntPtr, ByVal x As Integer, _
    9.          ByVal y As Integer, ByVal nWidth As Integer, _
    10.          ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, _
    11.          ByVal xSrc As Integer, ByVal ySrc As Integer, _
    12.          ByVal dwRop As Integer) As Integer
    13.     Private Declare Function GetDC Lib "user32" Alias "GetDC" _
    14.          (ByVal hwnd As IntPtr) As IntPtr
    15.     Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" _
    16.        (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer
    17.     Private Const SRCCOPY As Integer = &HCC0020
    18.  
    19. #Region " Windows Form Designer generated code "
    20. 'removed system generated code
    21. #End Region
    22.  
    23.     Private Sub cmd_Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Capture.Click
    24.         Dim lpWindowHandle As Integer
    25.         'name of window to capture
    26.         Dim lpWindowName As String = txtTitle.Text.Trim
    27.  
    28.         lpWindowHandle = FindWindow(vbNullString, lpWindowName)
    29.  
    30.         'display window handle
    31.         MsgBox("hwnd returns " & Hex(lpWindowHandle), MsgBoxStyle.OKOnly, "Results")
    32.  
    33.         'bring it to the foreground
    34.         SetForegroundWindow(lpWindowHandle)
    35.         Dim bmp As Bitmap = CreateScreenshot(lpWindowHandle)
    36.         PictureBox1.Image = bmp
    37.  
    38.  
    39.     End Sub
    40.  
    41.     Function CreateScreenshot(ByVal winHandle As Integer) As Bitmap
    42.  
    43.         Dim Rect As Rectangle = Screen.PrimaryScreen.Bounds
    44.         Dim gDest As Graphics
    45.         Dim hdcDest, hdcSrc As IntPtr
    46.  
    47.         CreateScreenshot = New Bitmap(Rect.Right, Rect.Bottom)
    48.         gDest = gDest.FromImage(CreateScreenshot)
    49.  
    50.         'hdcSrc = GetDC(IntPtr.Zero)
    51.         hdcSrc = GetDC(IntPtr.op_Explicit(winHandle))
    52.  
    53.         hdcDest = gDest.GetHdc
    54.         BitBlt(hdcDest, 0, 0, _
    55.            Rect.Right, Rect.Bottom, hdcSrc, 0, 0, SRCCOPY)
    56.         gDest.ReleaseHdc(hdcDest)
    57.         ReleaseDC(IntPtr.Zero, hdcSrc)
    58.  
    59.     End Function
    60. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width