【发布时间】:2017-10-06 00:39:53
【问题描述】:
我想写一个放大屏幕中间的程序,所以我使用了user32.dll中的一些方法。我设法让我的程序捕获屏幕并以 60FPS 的速率刷新图片框上的图像,但是当我想实时显示更大的图像时,程序也会捕获显示更大图像的表单,从而导致无限图像循环内的图像。我想捕获屏幕并忽略显示更大图像的表单。
我想到了一些事情,比如我将要忽略的窗口句柄传递给它,它会捕获除我指定的窗口之外的任何内容。这是我现在的代码:
Bitmap CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Bitmap img = null;
try
{
img = new Bitmap(Image.FromHbitmap(hBitmap), 1920 * 2, 1080 * 2);
using (Graphics g = Graphics.FromImage(img))
{
g.FillRectangle(Brushes.White, new Rectangle(0, 0, 200, 1080));//img.Width - (img.Height/2), img.Height));
// g.FillRectangle(Brushes.White, new Rectangle(img.Width - (img.Height / 2) + img.Height, 0, img.Width - img.Height / 2, img.Height));
}
GDI32.DeleteObject(hBitmap);
}
catch (Exception e)
{ }
return img;
}
我传给它的句柄来自这个方法:
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
我已经尝试过这个解决方案:How can i capture the screen without the Form? 但它效果不佳,因为图片框以 60Hz 的频率刷新,导致它卡顿。
说清楚,我的想法是放大屏幕中间,然后将放大后的图像显示在屏幕中间的一个正方形中(正方形之外的区域将保留其原始大小)。
【问题讨论】:
标签: c# screenshot