C#
C#에서 스크린 캡쳐 (윈폼)

C#에서 스크린을 카피하기 위해서는 먼저 스크린의 사이즈를 알아내야 하는데, 이는 System.Windows.Forms.Screen 클래스의 PrimaryScreen.Bounds 속성을 읽어 오면 쉽게 해결할 수 있다. 다음으로 한 픽셀이 몇개의 비트로 구성되어 있는지 알아야 하는데, 이것도 역시 Screen.PrimaryScreen.BitsPerPixel 속성을 참조하면 된다. 그리고 이어 픽셀당 비트수에 따라 PixelFormat 값을 설정할 수 있는데, 기본적으로 32비트 포맷을 사용하고 비트수가 적은 경우에는 더 낮은 픽셀 포맷을 사용한다. (요즘 대부분의 스크린이 32비트 포맷을 사용하므로 이를 생략할 수도 있다)
이렇게 구한 스크린 사이즈와 픽셀포맷을 사용하여 화면크기 만큼의 Bitmap 이미지 객체를 생성하고, Bitmap 이미지를 변경하기 위해 Graphics 객체를 얻어 온다. 이어 Graphics의 CopyFromScreen() 메서드를 사용하여 스크린 이미지를 Bitmap 객체로 복사하면 되고, 복사된 이미지는 Bitmap의 Save() 메서드를 써서 파일로 복사할 수 있다.


예제

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ScreenCopy
{
    public class ScreenCopy
    {
        // 사용예: ScreenCopy.Copy("test.png");
        // 
        public static void Copy(string outputFilename)
        {            
            // 주화면의 크기 정보 읽기
            Rectangle rect = Screen.PrimaryScreen.Bounds;            
            // 2nd screen = Screen.AllScreens[1]

            // 픽셀 포맷 정보 얻기 (Optional)
            int bitsPerPixel = Screen.PrimaryScreen.BitsPerPixel;
            PixelFormat pixelFormat = PixelFormat.Format32bppArgb;
            if (bitsPerPixel <= 16)
            {
                pixelFormat = PixelFormat.Format16bppRgb565;
            }
            if (bitsPerPixel == 24)
            {
                pixelFormat = PixelFormat.Format24bppRgb;
            }

            // 화면 크기만큼의 Bitmap 생성
            Bitmap bmp = new Bitmap(rect.Width, rect.Height, pixelFormat);

            // Bitmap 이미지 변경을 위해 Graphics 객체 생성
            using (Graphics gr = Graphics.FromImage(bmp))
            {
                // 화면을 그대로 카피해서 Bitmap 메모리에 저장
                gr.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
            }

            // Bitmap 데이타를 파일로 저장
            bmp.Save(outputFilename);
            bmp.Dispose();
        }
    }
}


  • 만약 모니터가 복수개인 경우 위의 예제는 첫번째 모니터 스크린만 복사한다.
  • 복수개의 모니터는 Screen.AllScreens 컬렉션 속성을 참조하여 엑세스할 수 있다.
  • 즉, 첫번째 모니터는 Screen.AllScreens[0], 두번째 모니터는 Screen.AllScreens[1] 등과 같이 엑세스한다.



WPF에서 스크린 캡쳐

WPF에서 스크린 캡쳐는 기본적으로 위의 윈폼에서의 스크린 캡쳐와 같다. 즉, 화면크기 만큼의 Bitmap 객체를 할당하고, 여기에 Graphics의 CopyFromScreen() 메서드를 사용하여 화면을 복사해서 넣으면 된다. 단 화면 크기를 구하는 System.Windows.Forms.Screen 을 WPF에서 사용하지 않고 대신 System.Windows.SystemParameters.PrimaryScreenWidth와 SystemParameters.PrimaryScreenHeight 를 사용하게 된다.

예제

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;

namespace ScreenCopyWpf
{
    class ScreenClass
    {
        public static void ScreenCopy()
        {
            // 주화면의 크기 정보 읽기
            int width = (int)SystemParameters.PrimaryScreenWidth;
            int height = (int)SystemParameters.PrimaryScreenHeight;

            // 화면 크기만큼의 Bitmap 생성
            using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
            {
                // Bitmap 이미지 변경을 위해 Graphics 객체 생성
                using (Graphics gr = Graphics.FromImage(bmp))
                {
                    // 화면을 그대로 카피해서 Bitmap 메모리에 저장
                    gr.CopyFromScreen(0, 0, 0, 0, bmp.Size);
                }
                // Bitmap 데이타를 파일로 저장
                bmp.Save("test.png", ImageFormat.Png);
            }
        }
    }
}




본 웹사이트는 광고를 포함하고 있습니다. 광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.




Comment bongbbil@*** 9/11/2016 2:25:10 PM
Alex Lee가 좋아합니다
Comment ekthf98@*** 6/10/2018 4:13:56 PM
Comment admin@*** 6/11/2018 6:08:50 PM
Comment 8506xx@*** 10/9/2018 5:25:56 PM
Comment admin@*** 10/9/2018 11:39:59 PM