C# Screen Capture - 스크린 캡쳐
C# 2021. 11. 22. 23:02 |반응형
C#으로 스크린을 캡쳐 해 보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bm = ScreenCapture.Capture();
Graphics g = CreateGraphics();
g.DrawImage(bm, 10, 40, new Rectangle(100, 100, 450, 250), GraphicsUnit.Pixel);
g.Dispose();
}
}
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();
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 desktopDC = GetDC(GetDesktopWindow());
IntPtr hDC = g.GetHdc();
BitBlt(hDC, 0, 0, screenWidth, screenHeight, desktopDC, 0, 0, 0x00CC0020); //SRCCOPY (DWORD)0x00CC0020
ReleaseDC(GetDesktopWindow(), desktopDC);
g.ReleaseHdc(hDC);
g.Dispose();
return screenBmp;
}
}
}
|
소스를 입력하고 빌드한다.
반응형
'C#' 카테고리의 다른 글
C# Image, Bitmap Resolution - 이미지, 비트맵 파일 해상도 (0) | 2021.11.25 |
---|---|
C# Desktop Moving Image Display - 바탕화면에 이미지 출력하고 움직이기 (0) | 2021.11.23 |
C# Form Designer Layout Mode - 폼 디자이너 레이아웃 모드 (0) | 2021.11.22 |
C# Windows Media Player Audio/Video Play #2 (0) | 2021.11.21 |
C# Windows Media Player Audio/Video Play #1 (0) | 2021.11.21 |