Results 1 to 2 of 2

Thread: Capturing image from window

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Location
    Egypt
    Posts
    179
    Hello,

    Is there any way to get an image from a specified area (x,y,w,h) of a picture on a window, and set this image to image control.

    Thanks,
    Belal Marzouk

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    Use a picture control. It is what it was designed for, and more importantly, it has an auto-refresh option, which the image control doesn't.

    The Stretchblt and BitBlt api calls both allow you to choose the starting x and y position andthe x width and the y width of the window you want to copy from. When pasting to a picture box, set auto-redraw to true and pass it the picture1.hdc. Then when the copy is done, do a picture1.refresh. Otherwise it won't draw.
    If you have autoredraw set to false, it will simply draw the image on the window itself and not in the picture box's buffer. This means that it won't refresh the image for you when the window is obscured/unobscured.

    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
    Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetClientRect& Lib "user32" (ByVal hwnd As Long, lpRect As RECT)
    Declare Function GetWindowRect& Lib "user32" (ByVal hwnd As Long, lpRect As RECT)
    Declare Function StretchBlt& Lib "gdi32" (ByVal hdc 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 nSrcWidth As Long, ByVal _
    nSrcHeight As Long, ByVal dwRop As Long)
    Private Const SRCCOPY = &HCC0020
    Type RECT
    left As Long
    top As Long
    right As Long
    bottom As Long
    End Type

    This will copy from one window to another.
    Call BitBlt(form1.picture1.hdc, 0, 0, XEnd - XStart, YEnd - YStart, SrcWindowHDC, XStart, YStart, SRCCOPY)
    form1.picture1.refresh

    This will copy from one window to another and resize to the total client area of the destination window.
    Dim DestRect As RECT
    Call GetClientRect(DestHwnd, DestRect)
    Call StretchBlt(form1.picture1.hdc, 0, 0, DestRect.right - DestRect.left + 1, DestRect.bottom - DestRect.top + 1, SrcHDC, srcXStart, srcYStart, srcXwidth, srcYwidth, SRCCOPY)
    If you don't know the hdc of the window you want to copy from, get it's handle, then
    hdc = getdc(handle)

    Once again, don't use this method to get the picture box's device context. Use it's .hdc property or you will end up drawing directly on the window.
    form1.picture1.refresh
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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