C#/Winform
[C#/Winform] Screen Capture
kjun.kr
2023. 10. 17. 21:57
728x90
728x170
Winform 에서 Screen Capture 하는 방법입니다.
using System.Drawing.Imaging;
namespace ScreenCaptureTest
{
public class ScreenCapture
{
public static void Capture(string outputFilename)
{
// Capture 할 모니터
Rectangle rect = Screen.PrimaryScreen.Bounds;
// 2nd screen = Screen.AllScreens[1]
// 픽셀 포맷 정보
int bitsPerPixel = Screen.PrimaryScreen.BitsPerPixel;
PixelFormat pixelFormat = PixelFormat.Format32bppArgb;
if (bitsPerPixel <= 16)
{
pixelFormat = PixelFormat.Format16bppRgb565;
}
if (bitsPerPixel == 24)
{
pixelFormat = PixelFormat.Format24bppRgb;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, pixelFormat);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
}
// Bitmap 을 이미지 파일로 저장
bmp.Save(outputFilename);
bmp.Dispose();
}
}
}
사용
namespace ScreenCaptureTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void captureButton_Click(object sender, EventArgs e)
{
ScreenCapture.Capture("test.png");
}
}
}
결과
728x90
그리드형