【发布时间】:2016-07-25 17:08:56
【问题描述】:
我目前正在使用 PrintWindow 截取外部应用程序的屏幕截图,我想将其保存到我的桌面。
这是我的代码:
// Get proc
Process proc = Process.GetProcessesByName("procName").Single();
IntPtr hWnd = proc.MainWindowHandle;
// Restore proc if minimised
int style = GetWindowLong(hWnd, GWL_STYLE);
if ((style & WS_MINIMIZE) == WS_MINIMIZE)
ShowWindow(hWnd, WindowShowStyle.Restore);
// Get RECT
RECT rect;
GetWindowRect(new HandleRef(this, hWnd), out rect);
// Get screenshot
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
IntPtr dc = g.GetHdc();
if (!PrintWindow(hWnd, dc, 0))
{
int error = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(error);
Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
return;
}
//Thread.Sleep(200);
bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.jpeg", ImageFormat.Jpeg);
panel1.BackgroundImage = bmp;
g.ReleaseHdc(dc);
}
panel1 向我展示了良好的图像,即应用程序的实际屏幕截图。当我转到我的桌面时,我找到了一个 test.jpeg,但它全是黑色的。为什么?
谢谢!
【问题讨论】:
-
对他来说,只有背景是黑色的。对我来说,整个图像都是黑色的
-
".. JPEG 图像默认为黑色背景,因此如果您的文本颜色也是黑色,您将获得黑色图像。如果您的图像没有背景颜色,您必须将其保存为 PNG .." From Saved Bitmap is black
-
我的图片是应用程序的屏幕截图,它确实包含黑色文本,但不仅仅是黑色文本。
-
我们无法看到
PrintWindow()方法中发生了什么。 -
它是一个user32 API方法,基本上它只是筛选应用程序并将其保存在图形对象中,就像我说的那样,图像在panel1中显示良好,但保存后全黑跨度>