Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Screen Capturing via GDI+ and GDI
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Screen Capturing via GDI+ and GDI
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-08-06

    Table of Contents:
  • Screen Capturing via GDI+ and GDI
  • GDI’s Role in Screen Capturing
  • Developing a Screen Capture Application
  • Persisting the Captured Images via GDI
  • Adding a Reference to GDI and Visualizing the Result

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Screen Capturing via GDI+ and GDI - Developing a Screen Capture Application


    (Page 3 of 5 )

    A screen capture application contains at least three steps:

    (1) Capture mouse cursor

    (2) Allow the user to draw the specified target

    (3) Save the target window as a custom bitmap and terminate the operation that captures the mouse movement.

    Now, let’s compose the sample application:

    (1) Launch Visual Studio 2005, select Visual C++ to create a MFC styled dialog based project, and name it ScreenCapture. Then, to gain a better visual effect, download or manually compose a camera cursor file (*.cur), import it into the project, and name it IDC_CAMERA. After that, declare two public variables within the CscreenCaptureDlg class as follows:

    public:

    HWND hwndCapture;

    CRect rectCapture;

    hwndCapture is used to hold the handler of the target window that is to be captured and rectCapture corresponds to the rectangle of the capturing window.

    (2) Define the two (WM_MOUSEMOVE and WM_LBUTTONUP) event handlers via the wizard. The complete source code of the two event handlers is listed below:

    void CScreenCaptureDlg::OnMouseMove(UINT nFlags, CPoint point)

    {

    //if the user press the left mouse button and does not release it (i.e. drag), then start to capture

    if(nFlags==MK_LBUTTON){

    //hide the demo application window in order not to affect the vision of the mouse capturing

    ShowWindow(SW_HIDE);

    //load the camera shaped mouse cursor, and start to track the movement of the mouse cursor

    HCURSOR cur=LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CAMERA));

    SetCursor(cur);

    SetCapture();

    //obtain the handler of the window that corresponds to the current mouse cursor

    this->ClientToScreen(&point);

    hwndCapture=(HWND)::WindowFromPoint(point);

    //obtain the handler of the device context of the screen so as to draw at any positon on the screen

    HDC hDC=::GetDC(NULL);

    //create a new red paint brush

    HPEN hPen=CreatePen(PS_INSIDEFRAME,6,RGB(255,0,0));

    //set the draw mode to R2_NOTXORPEN so as not to destroy the original background color

    int nMode=SetROP2(hDC,R2_NOTXORPEN);

    HPEN hpenOld=(HPEN)SelectObject(hDC,hPen);

    //get the rectangle of the window relating to the current mouse cursor

    ::GetWindowRect(hwndCapture,&rectCapture);

    //draw a red rectangle around the window relating to the current mouse cursor to friendly prompt the user

    POINT pt[5];

    pt[0]=CPoint(rectCapture.left,rectCapture.top);

    pt[1]=CPoint(rectCapture.right,rectCapture.top);

    pt[2]=CPoint(rectCapture.right,rectCapture.bottom);

    pt[3]=CPoint(rectCapture.left,rectCapture.bottom);

    pt[4]=CPoint(rectCapture.left,rectCapture.top);

    ::Polyline(hDC,pt,5);

    //first sleep 100 milliseconds and then redraw the red rectangle so as not to destroy the original content

    Sleep(100);

    ::Polyline(hDC,pt,5);

    //seemly verbose while effective

    ::SelectObject(hDC,hpenOld);

    ::ReleaseDC(NULL,hDC);

    }

    CDialog::OnMouseMove(nFlags, point);

    }

    void CScreenCaptureDlg::OnLButtonUp(UINT nFlags, CPoint point)

    {

    // obtain the width and height of the window the mouse cursor points to

    int nWidth=rectCapture.Width();

    int nHeight=rectCapture.Height();

    HDC hdcScreen,hMemDC;

    HBITMAP hBitmap,hOldBitmap;

    //set up a handler of the device context of the screen

    hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);

    hMemDC=CreateCompatibleDC(hdcScreen);

    // set up a bitmap that is compatible with the above handler of the screen device context and has the same size as the window the current mouse cursor points to

    hBitmap=CreateCompatibleBitmap(hdcScreen,nWidth,nHeight);

    //select the newly-built bitmap into the memory device context

    hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);

    //copy the screen device context into the memory device context

    BitBlt(hMemDC,0,0,nWidth,nHeight,hdcScreen,rectCapture.left,rectCapture.top,
    SRCCOPY);

    DeleteDC(hdcScreen);

    DeleteDC(hMemDC);

    //return the handler of the bitmap

    // Open the clipboard and copy the bitmap to it

    OpenClipboard();

    EmptyClipboard();

    SetClipboardData(CF_BITMAP,hBitmap);

    // Close the clipboard

    CloseClipboard();

    MessageBox("The specified content has been copied to the clipboard!");

    ReleaseCapture();

    //restore the original state of the demo applicaion’s window

    ShowWindow(SW_NORMAL);

    CDialog::OnLButtonUp(nFlags, point);

    }

    Since there are already detailed explanations in the above source code, we don't need to give uncecessary details. The core module of a professional screen capturing application has been accomplished.

    More Windows Scripting Articles
    More By Xianzhong Zhu


     

    WINDOWS SCRIPTING ARTICLES

    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...
    - Modifying XML Files in WSH
    - Reading XML Files in WSH
    - Visual Basic 2005 XML Programming Using XML ...
    - Creating an XML Document in WSH
    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT