티스토리 뷰
Visual Studio 2012사용
특정 프로세스의 윈도우 화면을 캡쳐해보자.
윈폼 프로그램에서 버튼 클릭으로 메모장의 화면을 캡쳐하는 것이 목표
1. 우선 Windows Form 응용프로그램으로 프로젝트 생성하자.
- 버튼 컨트롤 하나 생성
2. using 문 추가
using System.Runtime.InteropServices; //DllImport를 사용하기 위해
using System.Drawing.Imaging; //Bitmap 자원 활용
3. 소스는 아래와 같이 작성
namespace Capture
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
private const int SW_RESTORE = 9;
[DllImport("user32.dll")]
private static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
public Bitmap CaptureApplication(string procName)
{
Process proc;
// Cater for cases when the process can't be located.
try
{
proc = Process.GetProcessesByName(procName)[0];
}
catch (IndexOutOfRangeException e)
{
return null;
}
// You need to focus on the application
SetForegroundWindow(proc.MainWindowHandle);
ShowWindow(proc.MainWindowHandle, SW_RESTORE);
// You need some amount of delay, but 1 second may be overkill
Thread.Sleep(1000);
Rect rect = new Rect();
IntPtr error = GetWindowRect(proc.MainWindowHandle, ref rect);
// sometimes it gives error.
while (error == (IntPtr)0)
{
error = GetWindowRect(proc.MainWindowHandle, ref rect);
}
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics.FromImage(bmp).CopyFromScreen(rect.left,
rect.top,
0,
0,
new Size(width, height),
CopyPixelOperation.SourceCopy);
return bmp;
}
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
Bitmap bmpTmp;
bmpTmp = CaptureApplication("notepad");
bmpTmp.Save("d:\\test.png", ImageFormat.Png);
}
}
}
- btn1 버튼 클릭시 메모장 프로세스 이름을 읽어서 화면을 캡쳐한다.
- 캡쳐한 내용을 D:\test.png로 저장한다.
- 끝
'Programming > C#' 카테고리의 다른 글
| [C#] 자료 CSV 저장/불러오기(CsvSerializer) (0) | 2020.02.12 |
|---|---|
| [C#] WinForm에 안드로이드 폼, 폰트 적용하기(MaterialSkin) (1) | 2020.02.10 |
| [C#] 텍스트를 파일로 저장해보자! (0) | 2019.01.17 |
| [C#] 백그라운드에서 키보드 입력 읽기 (2) | 2018.12.04 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 소켓
- 부가가치세
- MFC ADO
- Sticky Notes Loading
- MSSQL
- 전자신고변환
- MXCOMPONENT
- ADODB
- 법정동코드
- 모달리스
- 전자신고파일설명서
- 스쿠트항공 환불받기
- 청년내일채움공제 만기신청
- 세금계산서합계표양식
- 부가가치세전산매체
- ADO
- MFC
- 해외송금확인
- sqlite3
- 모달리스 다이얼로그
- 항공알파벳
- 청년내일채움공제
- CMFCBUTTON
- 프로세스이름변경
- c++
- Modeless
- SendMessage
- #자동업데이트
- 스티커메모로드중
- MFC Modeless
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함