ウィンドウのスクリーンショットを撮る簡単なコンソールC#プログラム
前回の記事は、デスクトップのハンドルを取得してスクリーショットを撮るプログラムでしたが、今度はウィンドウタイトルから検索したウィンドウのハンドルを取得してスクリーショットを撮る簡単なプログラムです。
Nuget情報

Program.c
「Minecraft」を含みただし「server」を含まないウィンドウタイトルを探してスクリーショットを撮ります。これはMinecraft Serverを立ち上げているのでMinecraftのクライアントの方のハンドルを取得するためです。ウィンドウタイトルが変化しない場合サーチせずにstrTitleを固定にしても構いません。
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Spectre.Console;
namespace ScreenTest
{
class Program
{
static void Main(string[] args)
{
string strTitle = "";
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
//タイトルにMinecraftが含まれていてServerが含まれていない
if (0 <= p.MainWindowTitle.IndexOf("Minecraft") && 0 > p.MainWindowTitle.IndexOf("Server"))
{
strTitle = p.MainWindowTitle;
}
}
if (strTitle=="") return;
IntPtr hwd = FindWindow(null, strTitle);
while (true)
{
Image img = CaptureWindow(hwd);
Bitmap bitmap = new Bitmap(img);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Png);
CanvasImage consoleImage = new CanvasImage(ms.ToArray());
consoleImage.MaxWidth = 50;
AnsiConsole.Write(consoleImage);
ms.Dispose();
Console.Write("Input:");
string strInput = Console.ReadLine() ?? "";
if (strInput == "exit") break; // 'exit'と入力したら終わり
}
}
private static Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = GetWindowDC(handle);
RECT windowRect = new RECT();
GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = SelectObject(hdcDest, hBitmap);
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
SelectObject(hdcDest, hOld);
DeleteDC(hdcDest);
ReleaseDC(handle, hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
return img;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
private static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
}
実行結果
コンソール画面に切り替えるときにどうしてもメニュー画面になってしまいます。目的のウィンドウが他のウィンドウに隠れているか最小化されている場合は真っ黒になるかも知れませんがプレイ画面を撮る前提なのでご了承ください。
