Click here to Skip to main content
Click here to Skip to main content

Screen and Form capture with Managed C++

, 3 Jul 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
This is further demonstration of MC++ IJW capabilities

Introduction

I was continuing to play around with IJW. And when I realized that there was no direct Screen Capture method in .NET, I thought I might as well try to make a simple Screen Capturing program. Of course since I knew as much about GDI programming as some of these Canadians seem to know about Cricket, you might as well have guessed that I found things more than a little difficult. Luckily Tom Archer, James Johnson and Neil Van Note promptly answered my questions.

The sample program allows you to capture the entire screen or only the current form and also allows you to save the image, though here I have hard coded the filename and path, as I was too lazy to show a save dialog. If you repeatedly click on the capture buttons, you get a weird effect of cascading screen captures, one on top of the other as shown in the screenshot of the sample program.

Quick Rundown

Using GetDC or GetWindowDC depending on whether we are capturing the screen or the current window, we get our HDC using which we call CreateCompatibleDC to create our memory DC. Using a call to GetSystemMetrics we get the size of the screen and then call CreateCompatibleBitmap to create a bitmap. Then we select this bitmap into the memory DC using a call to SelectObject and save the old bitmap. Using BitBlt we copy the screen or window contents into this bitmap which is now selected into the memory DC. Then we select the original bitmap back into the DC, delete the memory DC, release the DC, and delete the bitmap. To be frank, there is not much .NET stuff involved in the code, but imagine if you had to write all this using C#. Think of all the P/Invoke stuff you'd have to do and the ugly declarations you'd have had to write for the API calls. With MC++ you are saved all that bother.

Full Source Listing

The source code is self-explanatory. Notice how I have written my WinMain. This is because I am including windows.h where WinMain is already prototyped in that fashion.

#include "stdafx.h"

#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

#include <tchar.h>
#include <windows.h>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class NForm : public Form
{
public:
    Button *btn;
    Button *btn2;
    Button *btn3;
    PictureBox *pbox;
    void btn_Click(Object *sender, System::EventArgs* e); 
    void CaptureScreen(bool FullScreen);

    NForm()
    {
        this->StartPosition = FormStartPosition::CenterScreen;
        this->Text = "Capture screen - Nish for CodeProject -IJW";
        this->Size = Drawing::Size(750,550);
        this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog; 

        btn=new Button();
        btn->Text="Capture Screen";
        btn->Location=Point(5,5);
        btn->Size=Drawing::Size(100,30);
        btn->add_Click(new EventHandler(this,&NForm::btn_Click)); 
        this->Controls->Add(btn);

        btn2=new Button();
        btn2->Text="Capture Form";
        btn2->Location=Point(125,5);
        btn2->Size=Drawing::Size(100,30);
        btn2->add_Click(new EventHandler(this,&NForm::btn_Click)); 
        this->Controls->Add(btn2);

        btn3=new Button();
        btn3->Text="Save C:\\Z.Jpg";
        btn3->Location=Point(250,5);
        btn3->Size=Drawing::Size(100,30);
        btn3->add_Click(new EventHandler(this,&NForm::btn_Click)); 
        this->Controls->Add(btn3);

        pbox = new PictureBox();
        pbox->Location=Point(5,45);
        pbox->Size=Drawing::Size(730,470);
        pbox->BorderStyle = BorderStyle::Fixed3D;
        pbox->SizeMode = PictureBoxSizeMode::StretchImage;


        this->Controls->Add(pbox);
    }
};

int __stdcall WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
    Application::Run(new NForm());
    return 0;
}

void NForm::btn_Click(Object *sender, System::EventArgs *e)
{
    if(sender->Equals(btn))
        CaptureScreen(true);
    else
    {
        if(sender->Equals(btn2))
            CaptureScreen(false);
        else
            pbox->Image->Save("C:\\Z.Jpg",
                Drawing::Imaging::ImageFormat::Jpeg);
    }
}

void NForm::CaptureScreen(bool FullScreen)
{
    HDC hDC;
    if(FullScreen)
        hDC = GetDC(NULL); //Get full screen
    else
    {
        HWND hWnd=(HWND)this->Handle.ToInt32(); //Get HWND of form
        hDC = GetWindowDC(hWnd); //Now get it's DC handle 
    }
    HDC hMemDC = CreateCompatibleDC(hDC);
    RECT r;
    GetWindowRect((HWND)this->Handle.ToInt32(),&r); //need this for Form
    SIZE size;
    if(FullScreen)
    { 
        size.cx = GetSystemMetrics(SM_CXSCREEN);
        size.cy = GetSystemMetrics(SM_CYSCREEN);
    } 
    else
    {
        size.cx = r.right-r.left;
        size.cy = r.bottom-r.top;
    }

    //most of the remaining code is normal GDI stuff

    HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
    if (hBitmap)
    {
        HBITMAP hOld = (HBITMAP) SelectObject(hMemDC, hBitmap);
        BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
        SelectObject(hMemDC, hOld);
        DeleteDC(hMemDC);
        ReleaseDC(NULL, hDC);
        pbox->Image = Image::FromHbitmap(hBitmap); 
        DeleteObject(hBitmap);
    }
}

History

  • May 7 02 - Posted the article
  • Jul 4 02 - Added sample VC++ project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAziz Yosofi6-Feb-13 23:25 
Questionexcuse me, i have a problem in capturing when i play the video Pinmemberzhmx_love21-Aug-12 23:47 
GeneralMy vote of 5 PinmemberMember 923867912-Jul-12 23:27 
QuestionQuestions regarding your article PinmemberOlariu Bogdan25-May-10 2:23 
AnswerRe: Questions regarding your article PinmvpNishant Sivakumar25-May-10 2:29 
GeneralRe: Questions regarding your article PinmemberOlariu Bogdan26-May-10 0:39 
Questionhow to get the image containing total content of entire form in .net windows application Pinmemberm.raghu18-Jun-07 20:13 
QuestionHow CreateDIBSection Work??? Pinmemberjavad_200524-Sep-06 10:32 
QuestionC++/CLI Image::FromHBitmap PinmemberCoder2k25-Jun-06 9:35 
QuestionRe: C++/CLI Image::FromHBitmap PinmemberCoder2k25-Jun-06 11:01 
AnswerRe: C++/CLI Image::FromHBitmap PinmemberCoder2k25-Jun-06 11:15 
Generalproblem Pinmembermr_ander5on6-Jun-05 11:58 
Questionhow SetDIBitsToDevice work??? Pinmemberjags_vc17-Mar-05 19:23 
GeneralBlack Screen Pinmembersomeone3214824-Apr-04 15:10 
AnswerRe: Black Screen [modified] Pinmemberdamn_tiby27-Apr-07 7:39 
Generala question Pinsussyediyildiz2-Aug-02 0:02 
GeneralRe: a question PineditorNishant S2-Aug-02 0:11 
GeneralRe: a question Pinsussyediyildiz2-Aug-02 2:24 
Generaldoh Pinmemberem4-Jul-02 16:07 
GeneralGood work but.. PinmemberRama Krishna8-May-02 5:09 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 9:56 
GeneralRe: Good work but.. PinmemberRama Krishna8-May-02 9:57 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 10:08 
GeneralRe: Good work but.. PinsubeditorJames T. Johnson8-May-02 10:38 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 10:42 
GeneralRe: Good work but.. PinsubeditorJames T. Johnson8-May-02 10:47 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 10:49 
GeneralRe: Good work but.. PinsubeditorJames T. Johnson8-May-02 11:05 
GeneralRe: Good work but.. PinmemberRama Krishna8-May-02 10:43 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 10:47 
GeneralRe: Good work but.. PinsubeditorJames T. Johnson8-May-02 10:48 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 10:53 
GeneralRe: Good work but.. PinsubeditorJames T. Johnson8-May-02 11:06 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 11:11 
GeneralRe: Good work but.. PinsubeditorJames T. Johnson8-May-02 11:18 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 11:22 
GeneralRe: Good work but.. PinmemberRama Krishna8-May-02 11:30 
GeneralRe: Good work but.. PinmemberNish [BusterBoy]8-May-02 11:35 
General*poke* PinmemberJames T. Johnson8-May-02 0:26 
GeneralRe: *poke* PinmemberNish [BusterBoy]8-May-02 4:43 
QuestionWhat's wrong with "Print Scrn"? PinmemberGeorge7-May-02 23:14 
AnswerRe: What's wrong with "Print Scrn"? PinmemberJames T. Johnson8-May-02 0:30 
GeneralRe: What's wrong with "Print Scrn"? PinmemberNish [BusterBoy]8-May-02 4:48 
GeneralRe: What's wrong with "Print Scrn"? PinmemberGeorge9-May-02 2:27 
GeneralRe: What's wrong with "Print Scrn"? PinmemberNish [BusterBoy]9-May-02 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 4 Jul 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid