Created
February 17, 2013 10:10
-
-
Save romichi/4970882 to your computer and use it in GitHub Desktop.
特定のアプリケーションのスクリーンショットを取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.Runtime.InteropServices; | |
[StructLayout(LayoutKind.Sequential)] | |
public struct RECT { | |
public int left; | |
public int top; | |
public int right; | |
public int bottom; | |
} | |
public class ScreenShot { | |
/* | |
* 指定アプリのスクリーンショット | |
*/ | |
[DllImport("user32.dll")] | |
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | |
[DllImport("user32.dll")] | |
static extern int GetWindowRect(IntPtr hWnd, out RECT rect); | |
[DllImport("user32.dll")] | |
static extern IntPtr SetForegroundWindow(IntPtr hWnd); | |
private IntPtr hWnd = IntPtr.Zero; // ウィンドウハンドル | |
private Rectangle rect; // 画面のサイズ | |
ScreenShot() { | |
hWnd = FindWindow("notepad", null); // ウィンドウハンドルの取得 | |
if (hWnd != IntPtr.Zero) { | |
// ウィンドウのサイズを取得 | |
RECT winRect; | |
GetWindowRect(hWnd, out winRect); | |
rect = new Rectangle(winRect.left, winRect.top, winRect.right - winRect.left, winRect.bottom - winRect.top); | |
} | |
else Console.WriteLine("アプリが確認できません"); | |
} | |
public Bitmap Capture() { | |
/* | |
* 画像の取得 | |
*/ | |
Bitmap bmp = new Bitmap(rect.Width, rect.Height); | |
Graphics g = Graphics.FromImage(bmp); | |
SetForegroundWindow(hWnd); // 前面に表示 | |
g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy); // 画面のコピー | |
return bmp; | |
} | |
public static void Main(){ | |
ScreenShot ss = new ScreenShot(); | |
Bitmap bmp = ss.Capture(); | |
bmp.Save("test.png"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment