C# 屏幕截图非活动外部应用程序
我需要拍摄一个非活动外部应用程序的截图,例如TeamSpeak或Skype 我已经搜索过了,但没有找到太多,我知道不可能截屏一个最小化的应用程序,但我认为应该可以截屏一个非活动的应用程序 PS:我只想截屏应用程序,所以如果另一个应用程序在我想要的应用程序之上,这会是一个问题吗 我现在没有代码,我找到了一个User32API,可以做我想做的事情,但是我忘记了它的名字 感谢您的帮助。实现此功能所需的一切都是使用from user32 API。PrintWindow将正确捕获特定应用程序的内容,即使它被其顶部的另一个窗口遮挡 值得注意的是,这可能不适用于捕获DirectX windows的内容。使用与from user32 API结合使用应该是实现该功能所需的全部。PrintWindow将正确捕获特定应用程序的内容,即使它被其顶部的另一个窗口遮挡C# 屏幕截图非活动外部应用程序,c#,screenshot,external,C#,Screenshot,External,我需要拍摄一个非活动外部应用程序的截图,例如TeamSpeak或Skype 我已经搜索过了,但没有找到太多,我知道不可能截屏一个最小化的应用程序,但我认为应该可以截屏一个非活动的应用程序 PS:我只想截屏应用程序,所以如果另一个应用程序在我想要的应用程序之上,这会是一个问题吗 我现在没有代码,我找到了一个User32API,可以做我想做的事情,但是我忘记了它的名字 感谢您的帮助。实现此功能所需的一切都是使用from user32 API。PrintWindow将正确捕获特定应用程序的内容,即使它
值得注意的是,这可能不适用于捕获DirectX窗口的内容。您所使用的API是
PrintWindow
:
void Example()
{
IntPtr hwnd = FindWindow(null, "Example.txt - Notepad2");
CaptureWindow(hwnd);
}
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public void CaptureWindow(IntPtr handle)
{
// Get the size of the window to capture
Rectangle rect = new Rectangle();
GetWindowRect(handle, ref rect);
// GetWindowRect returns Top/Left and Bottom/Right, so fix it
rect.Width = rect.Width - rect.X;
rect.Height = rect.Height - rect.Y;
// Create a bitmap to draw the capture into
using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
{
// Use PrintWindow to draw the window into our bitmap
using (Graphics g = Graphics.FromImage(bitmap))
{
IntPtr hdc = g.GetHdc();
if (!PrintWindow(handle, hdc, 0))
{
int error = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(error);
Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
// TODO: Throw the exception?
}
g.ReleaseHdc(hdc);
}
// Save it as a .png just to demo this
bitmap.Save("Example.png");
}
}
您需要的API是
PrintWindow
:
void Example()
{
IntPtr hwnd = FindWindow(null, "Example.txt - Notepad2");
CaptureWindow(hwnd);
}
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public void CaptureWindow(IntPtr handle)
{
// Get the size of the window to capture
Rectangle rect = new Rectangle();
GetWindowRect(handle, ref rect);
// GetWindowRect returns Top/Left and Bottom/Right, so fix it
rect.Width = rect.Width - rect.X;
rect.Height = rect.Height - rect.Y;
// Create a bitmap to draw the capture into
using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
{
// Use PrintWindow to draw the window into our bitmap
using (Graphics g = Graphics.FromImage(bitmap))
{
IntPtr hdc = g.GetHdc();
if (!PrintWindow(handle, hdc, 0))
{
int error = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(error);
Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
// TODO: Throw the exception?
}
g.ReleaseHdc(hdc);
}
// Save it as a .png just to demo this
bitmap.Save("Example.png");
}
}
PrintWindow返回false,那么这是否意味着它不工作?是的,要么您输入的HWND无效,要么目标窗口已禁用对PrintWindow的支持<代码>封送。GetLastWin32Error可能包含有关失败的更多信息。我相信我得到的HWND是有效的,因为GetWindowRect工作正常。Marshal.GetLastWin32Error是如何工作的?访问被拒绝,我必须以管理员身份启动应用程序,然后PrintWindow返回false,这是否意味着它不工作?是的,您提供的HWND无效,或者目标窗口已禁用对PrintWindow的支持<代码>封送。GetLastWin32Error可能包含有关失败的更多信息。我相信我得到的HWND是有效的,因为GetWindowRect工作正常。Marshal.GetLastWin32Error如何工作?访问被拒绝,我必须以管理员身份启动我的应用程序