CopyFromScreenメソッドを利用してスクリーンキャプチャを取得するコードを紹介します。
.NET Framework (C#)ではCopyFromScreenメソッドを用いることでスクリーンキャプチャ画像を取得できます。
WinFormアプリケーションを新規作成し、Buttonを1つ配置します。
ButtonのClickイベントに以下のコードを記述します。
private void button1_Click(object sender, EventArgs e)
{
Rectangle rc = Screen.PrimaryScreen.Bounds;
Bitmap bmp = new Bitmap(rc.Width, rc.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
string file=@"c:\data\screen.bmp";
bmp.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
}
下記のコードでスクリーン全体のRectangleを取得します。
Rectangle rc = Screen.PrimaryScreen.Bounds;
スクリーンキャプチャの画像を格納するビットマップを用意します。
Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
用意したビットマップのGraphicsクラスを取得します。
Graphics g = Graphics.FromImage(bmp);
CopyFromScreenメソッドを呼び出してスクリーンキャプチャを取得します。
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
ビットマップを c:\data\screen.bmpとして保存します。
string file=@"c:\data\screen.bmp";
bmp.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);