Click here to Skip to main content
15,843,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,
Can you please guide me the concepts/articles which would help me to write a program to take a screen shot/ Video of the desktop while a user is working or idle and save it to a folder. Can this happen. if so please help me.

Regards,
Kiran
Posted

1 solution

Hi,

It's fairly easy to capture screenshots using the gdi32 api.
Look at this article by James Crowley

http://www.developerfusion.com/code/4630/capture-a-screen-shot/[^]

public Image CaptureWindow(IntPtr handle)
{
    // get te hDC of the target window
    IntPtr hdcSrc = User32.GetWindowDC(handle);
    // get the size
    User32.RECT windowRect = new User32.RECT();
    User32.GetWindowRect(handle,ref windowRect);
    int width = windowRect.right - windowRect.left;
    int height = windowRect.bottom - windowRect.top;
    // create a device context we can copy to
    IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
    // create a bitmap we can copy it to,
    // using GetDeviceCaps to get the width/height
    IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
    // select the bitmap object
    IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
    // bitblt over
    GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
    // restore selection
    GDI32.SelectObject(hdcDest,hOld);
    // clean up
    GDI32.DeleteDC(hdcDest);
    User32.ReleaseDC(handle,hdcSrc);
    // get a .NET image object for it
    Image img = Image.FromHbitmap(hBitmap);
    // free up the Bitmap object
    GDI32.DeleteObject(hBitmap);
    return img;
}


If you want to capture video then you could have a look Microsoft Expression encoder.

here is a code sample showing video capture:

_screenCaptureJob = new ScreenCaptureJob();
int width = Convert.ToInt32(area.Width / 4) * 4;
int height = Convert.ToInt32(area.Height / 4) * 4;
Rectangle rect = new Rectangle(area.Location, new Size(width, height));
_screenCaptureJob.CaptureRectangle = rect;
_screenCaptureJob.ShowFlashingBoundary = true;
_screenCaptureJob.ScreenCaptureVideoProfile.FrameRate = 8;
_screenCaptureJob.ScreenCaptureVideoProfile.Quality = 75;
_screenCaptureJob.CaptureMouseCursor = true;
_screenCaptureJob.OutputScreenCaptureFileName = Path.ChangeExtension(Path.Combine(this.textBox1.Text, Path.GetRandomFileName()), ".wmv");
_screenCaptureJob.Duration = new TimeSpan(0, 0, Convert.ToInt32(textBoxVideoLength.Text));
_screenCaptureJob.Start();


These two code sample are in c#.

Valery.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-11 13:27pm    
Very nice, a 5.
--SA
Valery Possoz 28-May-11 16:11pm    
Thanks :) Valery.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900