Results 1 to 6 of 6

Thread: Taking screenshot of a particular component of a frame

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    2

    Question Taking screenshot of a particular component of a frame

    hi,

    i am making a small application where i need to take a screenshot of a textbox. i am learning vb and i found in one of the books keybd_event to simulate a screenshot..

    keybd_event 44, 1, 0, 0
    Picture1.Image = Clipboard.GetData

    keybd_event takes the shot of the entire frame.. (as expected)

    does anyone know how to take a screenshot of a particular component of a frame?

    another question -- is there any way i can edit an image that is there in the clipboard (or crop an image - for that matter) ??

    thanks!

    edit: this is my first post here, so please bear with me if i have not followed the ethics here! cheers!

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Taking screenshot of a particular component of a frame

    Here's a quick example of taking a screenshot of a TextBox control. Just needs a picturebox on the form, and a command button.

    You can do the same thing without using a temporary picturebox (by using a DC in memory) but I don't have the time right now.

    Code:
    Option Explicit
    
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Private 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
    
    Private Sub Command1_Click()
        
        ScreenshotControl Text1, picTemp, "C:\textbox.bmp"
        
    End Sub
    
    Private Sub Form_Load()
        
        Me.ScaleMode = vbPixels
        
    End Sub
    
    Private Sub ScreenshotControl(ByRef TheControl As Control, _
        ByRef TempPicture As PictureBox, _
        ByRef SavePath As String)
        
        Dim lonDC As Long
        
        lonDC = GetDC(TheControl.hwnd)
        
        With TempPicture
            .Cls
            .AutoRedraw = True
            .ScaleMode = vbPixels
            .Width = TheControl.Width
            .Height = TheControl.Height
            
            BitBlt .hDC, 0, 0, .ScaleWidth, .ScaleHeight, _
                lonDC, 0, 0, vbSrcCopy
            
            SavePicture TempPicture.Image, SavePath
        End With
        
    End Sub

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Taking screenshot of a particular component of a frame

    Regarding the textbox. If the textbox is an actual textbox and you know the hWnd, you can get a "snapshot" but you could also get the text itself. But you will need the hWnd of that control either way.
    Code:
    ' snapshot example
    Private Declare Function PrintWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
    
    Private Sub Command1_Click()
        PrintWindow TexthWnd, Picture1.hDC, 0&
        ' supply the hWnd of the textbox control as TexthWnd
    End Sub
    To get the actual text
    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    Private Const WM_GETTEXTLENGTH As Long = &HE
    Private Const WM_GETTEXT As Long = &HD
    
    Private Sub Command1_Click()
        Dim sText As String, lLen As Long
        lLen = SendMessage(TexthWnd, WM_GETTEXTLENGTH, 0&, ByVal 0&)
        If lLen Then
             sText = String$(lLen, vbNullChar)
             SendMessage TexthWnd, WM_GETTEXT, lLen + 1, ByVal sText
             Picture1.Print sText
        End If
        ' supply the hWnd of the textbox control as TexthWnd
    End Sub
    There are other methods of getting a "window capture", as DigiRev has displayed (beat me to the punch)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    2

    Re: Taking screenshot of a particular component of a frame

    that was fast! thanks! i'll try this! many many thanks!!

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Taking screenshot of a particular component of a frame

    I never knew there was a PrintWindow API function. That way seems a lot easier.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Taking screenshot of a particular component of a frame

    Quote Originally Posted by DigiRev View Post
    I never knew there was a PrintWindow API function. That way seems a lot easier.
    PrintWindow. XP and above per MSDN link. Your way is compatible for all O/S.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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