Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Capture Screen using C#.Net
WhatsApp
Manoj Bhoir
Apr 18
2015
6.2
k
0
0
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