46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using Windows.Win32;
|
|
using Windows.Win32.Foundation;
|
|
using Windows.Win32.Graphics.Gdi;
|
|
using Windows.Win32.Storage.Xps;
|
|
|
|
namespace Screenie;
|
|
|
|
internal static class Photographer {
|
|
private static unsafe HWND FindWindowHandle() {
|
|
HWND window = default;
|
|
// last param could be "FINAL FANTASY XIV", but name is changeable
|
|
while (!(window = PInvoke.FindWindowEx(default, window, "FFXIVGAME", null)).IsNull) {
|
|
var windowProc = 0u;
|
|
_ = PInvoke.GetWindowThreadProcessId(window, &windowProc);
|
|
if (windowProc == 0) {
|
|
continue;
|
|
}
|
|
|
|
if (windowProc == Environment.ProcessId && PInvoke.IsWindowVisible(window)) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return window;
|
|
}
|
|
|
|
internal static Bitmap? Capture() {
|
|
var window = FindWindowHandle();
|
|
if (window.IsNull) {
|
|
return null;
|
|
}
|
|
|
|
if (!PInvoke.GetWindowRect(window, out var rect)) {
|
|
return null;
|
|
}
|
|
|
|
var bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
|
|
using var graphics = Graphics.FromImage(bitmap);
|
|
return PInvoke.PrintWindow(window, (HDC) graphics.GetHdc(), PRINT_WINDOW_FLAGS.PW_CLIENTONLY)
|
|
? bitmap
|
|
: null;
|
|
}
|
|
}
|