2026-01-26 18:56:53 +09:00

75 lines
2.9 KiB
C#

/*
* Copyright (c) 2025 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using Aurora.Api;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace VisionCommon {
public class ScreenshotHelper {
private static Logger _Logger = new Logger("ScreenshotHelper");
public const int SHOWNORMAL = 1;
public const int SHOWMINIMIZED = 2;
public const int SHOWMAXIMIZED = 3;
public static Bitmap ScreenShotWindow(string windowTitle) {
try {
IntPtr whandle = IntPtr.Zero;
Rect window = new Rect();
whandle = User32.FindWindow(null, windowTitle);
if (whandle == IntPtr.Zero) {
return null;
}
//User32.ShowWindowAsync(whandle, SHOWNORMAL); // 윈도우가 최소화 되어 있다면 활성화 시킨다.
User32.ShowWindowAsync(whandle, SHOWMAXIMIZED); // 윈도우가 최대화 시킨다.
User32.SetForegroundWindow(whandle); // 윈도우에 포커스를 줘서 최상위로 만든다.
User32.GetWindowRect(whandle, ref window);
int width = window.Right - window.Left;
int height = window.Bottom - window.Top;
System.Drawing.Size size = new System.Drawing.Size();
size.Width = width;
size.Height = height;
var bmpScreenshot = new Bitmap(width,
height,
PixelFormat.Format32bppArgb);
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(window.Left, window.Top,
0, 0, size,
CopyPixelOperation.SourceCopy);
return bmpScreenshot;
}
catch (Exception e) {
_Logger.FuncE(e.Message);
}
return null;
}
private class User32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
}
public struct Rect {
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
}
}