TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Capture Screen using C#.Net
Manoj Bhoir
Apr 18
2015
Code
5.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
using
System.Collections.Generic;
using
System.Drawing;
using
System.Linq;
using
System.Runtime.InteropServices;
using
System.Text;
using
System.Windows.Forms;
namespace
ScreenCapture
{
public
class
ScreenCapture
{
[DllImport(
"user32.dll"
, ExactSpelling =
true
, SetLastError =
true
)]
private
static
extern
IntPtr GetDC(IntPtr hWnd);
[DllImport(
"user32.dll"
, ExactSpelling =
true
)]
private
static
extern
IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport(
"gdi32.dll"
, ExactSpelling =
true
)]
private
static
extern
IntPtr BitBlt(IntPtr hDestDC,
int
x,
int
y,
int
nWidth,
int
nHeight, IntPtr hSrcDC,
int
xSrc,
int
ySrc,
int
dwRop);
[DllImport(
"user32.dll"
, EntryPoint =
"GetDesktopWindow"
)]
private
static
extern
IntPtr GetDesktopWindow();
/// <summary>
/// Capture Screen
/// </summary>
/// <returns></returns>
public
static
Bitmap Capture()
{
int
screenWidth = Screen.PrimaryScreen.Bounds.Width;
int
screenHeight = Screen.PrimaryScreen.Bounds.Height;
Bitmap screenBmp =
new
Bitmap(screenWidth, screenHeight);
Graphics g = Graphics.FromImage(screenBmp);
IntPtr dc1 = GetDC(GetDesktopWindow());
IntPtr dc2 = g.GetHdc();
BitBlt(dc2, 0, 0, screenWidth, screenHeight, dc1, 0, 0, 13369376);
ReleaseDC(GetDesktopWindow(), dc1);
g.ReleaseHdc(dc2);
g.Dispose();
return
screenBmp;
}
}
}
C#.NET
GDI
Graphics
Screen