【发布时间】:2014-12-17 09:21:28
【问题描述】:
我最近发现了一个代码来制作活动窗口的屏幕截图。它实际上可以工作,但是图像有点太大了,它有点超出当前窗口的边界。
这是我的课:
public static class Screenshotter
{
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
public static void MakeScreenshot()
{
var foregroundWindowsHandle = GetForegroundWindow();
var rect = new RECT();
GetWindowRect(foregroundWindowsHandle, out rect);
Rectangle bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
Bitmap bmp = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
bmp.Save("test.png", ImageFormat.Png);
}
}
我只希望它截取活动窗口而不是窗口外的一点点。我希望有人可以帮助我:)
【问题讨论】:
-
我很好奇,
CursorPosition的目的是什么? -
哦哇哦,其实没什么意义,我只是看到了哈哈。
-
考虑窗口的边框尺寸:msdn.microsoft.com/en-us/library/…
-
@Aybe 这并没有给我我的活动窗口的大小,而且我怀疑这个类可以给我我的表单之外的活动窗口的任何属性。我用 IntPtr 找不到任何东西,我认为代码需要窗口的 IntPtr 才能从中获取详细信息。
-
只是一个疯狂的建议,尝试关闭 Aero 主题并选择一个不涉及任何透明度或任何花哨混合的基本主题。
标签: c# screenshot