C# - Form Capture

2015. 2. 20. 20:53IT/C#

C#에서 폼화면을 캡쳐할려면, Graphics라는 클래스의 CopyFromScreen 매소드를 이용해야합니다.

먼저 폼 소스에서 아래의 매소드를 추가해주세요.

public void FormCapture(Size uFormSize, String uFileName)
{
            Bitmap bitmap = new Bitmap(uFormSize.Width, uFormSize.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.CopyFromScreen(new Point(this.Bounds.X, this.Bounds.Y), new Point(0, 0), uFormSize);
            bitmap.Save(uFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}


임의의 버튼을 생성하여, 버튼을 눌렀을 때 위에서 만든 FormCapture 메소드를 호출하면 폼 캡쳐가 됩니다.
private void button2_Click_1(object sender, EventArgs e)
{
            Size sz = new Size(this.Bounds.Width, this.Bounds.Height);
            FormCapture(sz, fileName);
}


전체 화면을 캡쳐할려면 위의 소스에서 조금만 수정하면 됩니다.
포인트를 0,0을 기준잡아줍니다. 참고로, 0,0은 왼쪽 상단입니다.
public void FormCapture(Size uFormSize, String uFileName)
{
            Bitmap bitmap = new Bitmap(uFormSize.Width, uFormSize.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), uFormSize);
            bitmap.Save(uFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}

그리고 아래와 같이 주 디스플레이의 가로, 세로의 길이를 넣어주면 됩니다.

private void button2_Click_1(object sender, EventArgs e)
{
 
            Size sz = new Size(Screen.PrimaryScreen.Bounds.Width,
                               Screen.PrimaryScreen.Bounds.Height);
            FormCapture(sz, fileName);
}