Results 1 to 3 of 3

Thread: how to copy part of screen to an array?

  1. #1

    Thread Starter
    Hyperactive Member temp_12000's Avatar
    Join Date
    Jan 2004
    Location
    LA, USA
    Posts
    411

    Question how to copy part of screen to an array?

    I need copy part (not the entire) of the screen to an array, so that I can do some image processing stuff. What I am doing now is:
    1) simulate key press of CTRL+ALT+SNAPSHOT;
    2) Copy Data from clipboard to an BMP;
    3) Use GetDIBits to get an array (A) of the entire BMP;
    4) Then get the part of array that I want from A;

    It works. However, it has some problems:
    1) I only need a small part of the screen, use the above method, the BMP is the entire screen, it takes more time and memory;
    2) step 3) takes too much time (since it copies all elements in the BMP);

    What I use in step 3) is:

    Call GetDIBits(lDC, lBmp, 0, vBMP.Height, mBits(0, 0), vBMI, 0) ' Get bits

    and I tried to figure out how to copy a part of the BMP, say the area (100, 200)-(150, 250), however, I failed.

    Anyone had the experience? I tried google, however, could not find the solution.

    Thank you very much

  2. #2
    Member
    Join Date
    Oct 2002
    Location
    too expensive land
    Posts
    46
    attached is a sample i found.

    in case you failed to download it - those are the main functions you need :

    Option Explicit
    Option Base 0
    ' CreateBitmapPicture - Creates a picture object from a bitmap and palette.
    ' CaptureWindow - Captures any window given a window handle.
    ' CaptureActiveWindow - Captures the active window on the desktop.
    ' CaptureForm - Captures the entire form.
    ' CaptureClient - Captures the client area of a form.
    ' CaptureScreen - Captures the entire screen.
    ' PrintPictureToFitPage - prints any picture as big as possible on the page.
    Private Type PALETTEENTRY
    peRed As Byte
    peGreen As Byte
    peBlue As Byte
    peFlags As Byte
    End Type
    Private Type LOGPALETTE
    palVersion As Integer
    palNumEntries As Integer
    palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
    End Type
    Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
    End Type
    Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    Private Type PicBmp
    Size As Long
    Type As Long
    hBmp As Long
    hPal As Long
    Reserved As Long
    End Type
    Private Const RASTERCAPS As Long = 38
    Private Const RC_PALETTE As Long = &H100
    Private Const SIZEPALETTE As Long = 104

    Private Declare Function CreateCompatibleBitmap Lib "GDI32" ( _
    ByVal hDC As Long, ByVal nWidth As Long, _
    ByVal nHeight As Long) As Long

    ' Retrieves device-specific information about a specified device.
    Private Declare Function GetDeviceCaps Lib "GDI32" ( _
    ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long

    ' Retrieves a range of palette entries from the system palette
    ' associated with the specified DC.
    Private Declare Function GetSystemPaletteEntries Lib "GDI32" ( _
    ByVal hDC As Long, ByVal wStartIndex As Long, _
    ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) _
    As Long
    ' Creates a memory DC compatible with the specified device.
    Private Declare Function CreateCompatibleDC Lib "GDI32" ( _
    ByVal hDC As Long) As Long
    ' Creates a logical color palette.
    Private Declare Function CreatePalette Lib "GDI32" ( _
    lpLogPalette As LOGPALETTE) As Long
    ' Selects the specified logical palette into a DC.
    Private Declare Function SelectPalette Lib "GDI32" ( _
    ByVal hDC As Long, ByVal hPalette As Long, _
    ByVal bForceBackground As Long) As Long
    ' Maps palette entries from the current logical
    ' palette to the system palette.
    Private Declare Function RealizePalette Lib "GDI32" ( _
    ByVal hDC As Long) As Long
    ' Selects an object into the specified DC. The new
    ' object replaces the previous object of the same type.
    ' Returned is the handle of the replaced object.
    Private Declare Function SelectObject Lib "GDI32" ( _
    ByVal hDC As Long, ByVal hObject As Long) As Long
    ' Performs a bit-block transfer of color data corresponding to
    ' a rectangle of pixels from the source DC into a destination DC.
    Private Declare Function BitBlt Lib "GDI32" ( _
    ByVal hDCDest As Long, ByVal XDest As Long, _
    ByVal YDest As Long, ByVal nWidth As Long, _
    ByVal nHeight As Long, ByVal hDCSrc As Long, _
    ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) _
    As Long
    ' Retrieves the DC for the entire window, including title bar,
    ' menus, and scroll bars. A window DC permits painting anywhere
    ' in a window, because the origin of the DC is the upper-left
    ' corner of the window instead of the client area.
    Private Declare Function GetWindowDC Lib "USER32" ( _
    ByVal hWnd As Long) As Long
    ' Retrieves a handle to a display DC for the Client area of
    ' a specified window or for the entire screen. You can use
    ' the returned handle in subsequent GDI functions to draw in
    ' the DC.
    Private Declare Function GetDC Lib "USER32" ( _
    ByVal hWnd As Long) As Long

    ' Releases a DC, freeing it for use by other applications.
    ' The effect of the ReleaseDC function depends on the type
    ' of DC. It frees only common and window DCs. It has no
    ' effect on class or private DCs.
    Private Declare Function ReleaseDC Lib "USER32" ( _
    ByVal hWnd As Long, ByVal hDC As Long) As Long
    ' Deletes the specified DC.
    Private Declare Function DeleteDC Lib "GDI32" ( _
    ByVal hDC As Long) As Long
    ' Retrieves the dimensions of the bounding rectangle of the
    ' specified window. The dimensions are given in screen
    ' coordinates that are relative to the upper-left corner
    ' of the screen.
    Private Declare Function GetWindowRect Lib "USER32" ( _
    ByVal hWnd As Long, lpRect As RECT) As Long
    ' Returns a handle to the Desktop window. The desktop
    ' window covers the entire screen and is the area on top
    ' of which all icons and other windows are painted.
    Private Declare Function GetDesktopWindow Lib "USER32" () As Long
    ' Returns a handle to the foreground window (the window
    ' the user is currently working). The system assigns a
    ' slightly higher priority to the thread that creates the
    ' foreground window than it does to other threads.
    Private Declare Function GetForegroundWindow Lib "USER32" () As Long
    ' Creates a new picture object initialized according to a PICTDESC
    ' structure, which can be NULL, to create an uninitialized object if
    ' the caller wishes to have the picture initialize itself through
    ' IPersistStream::Load. The fOwn parameter indicates whether the
    ' picture is to own the GDI picture handle for the picture it contains,
    ' so that the picture object will destroy its picture when the object
    ' itself is destroyed. The function returns an interface pointer to the
    ' new picture object specified by the caller in the riid parameter.
    ' A QueryInterface is built into this call. The caller is responsible
    ' for calling Release through the interface pointer returned - phew!
    Private Declare Function OleCreatePictureIndirect _
    Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
    ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    Public Function CreateBitmapPicture(ByVal hBmp As Long, _
    ByVal hPal As Long) As Picture
    Dim r As Long
    Dim Pic As PicBmp
    Dim IPic As IPicture
    Dim IID_IDispatch As GUID
    With IID_IDispatch
    .Data1 = &H20400
    .Data4(0) = &HC0
    .Data4(7) = &H46
    End With
    With Pic
    .Size = Len(Pic) ' Length of structure
    .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
    .hBmp = hBmp ' Handle to bitmap
    .hPal = hPal ' Handle to palette (may be null)
    End With
    r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    Set CreateBitmapPicture = IPic
    End Function
    Public Function CaptureWindow(ByVal hWndSrc As Long, _
    ByVal bClient As Boolean, ByVal LeftSrc As Long, _
    ByVal TopSrc As Long, ByVal WidthSrc As Long, _
    ByVal HeightSrc As Long) As Picture

    Dim hDCMemory As Long
    Dim hBmp As Long
    Dim hBmpPrev As Long
    Dim r As Long
    Dim hDCSrc As Long
    Dim hPal As Long
    Dim hPalPrev As Long
    Dim RasterCapsScrn As Long
    Dim HasPaletteScrn As Long
    Dim PaletteSizeScrn As Long
    Dim LogPal As LOGPALETTE
    If bClient Then
    hDCSrc = GetDC(hWndSrc) 'Get DC for Client area.
    Else
    hDCSrc = GetWindowDC(hWndSrc) 'Get DC for entire window.
    End If
    hDCMemory = CreateCompatibleDC(hDCSrc)
    hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    hBmpPrev = SelectObject(hDCMemory, hBmp)
    RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) 'Raster capabilities
    HasPaletteScrn = RasterCapsScrn And RC_PALETTE 'Palette support
    PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) 'Palette size
    If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    LogPal.palVersion = &H300
    LogPal.palNumEntries = 256
    r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
    hPal = CreatePalette(LogPal)
    hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    r = RealizePalette(hDCMemory)
    End If
    r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
    LeftSrc, TopSrc, vbSrcCopy)
    hBmp = SelectObject(hDCMemory, hBmpPrev)
    If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    End If
    r = DeleteDC(hDCMemory)
    r = ReleaseDC(hWndSrc, hDCSrc)
    Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    End Function
    Public Function CaptureScreen() As Picture
    Dim hWndScreen As Long
    hWndScreen = GetDesktopWindow()
    With Screen
    Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, _
    .Width \ .TwipsPerPixelX, .Height \ .TwipsPerPixelY)
    End With
    End Function

    Public Function CaptureForm(frm As Form) As Picture
    With frm
    Set CaptureForm = CaptureWindow(.hWnd, False, 0, 0, _
    .ScaleX(.Width, vbTwips, vbPixels), _
    .ScaleY(.Height, vbTwips, vbPixels))
    End With
    End Function

    Public Function CaptureClient(frm As Form) As Picture
    '
    ' Capture the client area of the form.
    '
    With frm
    Set CaptureClient = CaptureWindow(.hWnd, True, 0, 0, _
    .ScaleX(.ScaleWidth, .ScaleMode, vbPixels), _
    .ScaleY(.ScaleHeight, .ScaleMode, vbPixels))
    End With
    End Function

    Public Function CaptureActiveWindow() As Picture
    Dim hWndActive As Long
    Dim RectActive As RECT
    '
    ' Get a handle to the active/foreground window.
    ' Get the dimensions of the window.
    '
    hWndActive = GetForegroundWindow()
    Call GetWindowRect(hWndActive, RectActive)
    '
    ' Capture the active window.
    '
    With RectActive
    Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, _
    .Right - .Left, .Bottom - .Top)
    End With
    End Function
    Attached Files Attached Files

  3. #3

    Thread Starter
    Hyperactive Member temp_12000's Avatar
    Join Date
    Jan 2004
    Location
    LA, USA
    Posts
    411
    Originally posted by TOMERP
    [B]attached is a sample i found.

    in case you failed to download it - those are the main functions you need :
    After a little change, now I can capture part of the screen!

    thank you very mcuh and I really appreciate your help.

    Temp

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