我正在尝试捕获C#中的表单屏幕截图,我正在使用以下代码。代码正常工作,但仅当表单在每个其他应用程序的顶部时。如果表格低于其他形式,那么它不是被捕获的形式。
有人知道要执行的任何代码吗?
由于
Public Function GetScreenShot(F As Form) As Bitmap
Dim Bounds As Rectangle = F.Bounds
Dim Bitmap As Bitmap = New Bitmap(Bounds.Width, Bounds.Height)
Dim G As Graphics = Graphics.FromImage(Bitmap)
G.CopyFromScreen(New Point(Bounds.Left, Bounds.Top), Point.Empty, Bounds.Size)
Return Bitmap
End Function
答案 0 :(得分:2)
这是一个工作副本,我创建了一个Windows窗体并从另一个应用程序中截取屏幕截图并将其保存在磁盘上。
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ScreenshotApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void TakeScreenShot()
{
Process p = Process.GetProcessById(16416); //modify to use PID or use GetProcessByName
IntPtr handle = p.MainWindowHandle;
SomeImageThingy.CaptureWindow(handle);
}
private void button1_Click(object sender, System.EventArgs e)
{
TakeScreenShot();
}
}
public class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}
public class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
public static class SomeImageThingy
{
public static Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
img.Save("capture.png", ImageFormat.Png);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
}
}
答案 1 :(得分:1)
您可以使用内置的方式将整个表单绘制到位图:
F.DrawToBitmap(Bitmap, F.ClientRectangle);
其中F
是表单的实例
答案 2 :(得分:-2)
这是因为您的表单位于z顺序的窗口下方,这使得它们覆盖表单区域并使其不被捕获。您可以做的是使用Form.TopMost
将表单置于最前面,然后重置它以快速修复问题:
Public Function GetScreenShot(F As Form) As Bitmap
'Bring the form to top and reset afterwards
F.TopMost = True
F.TopMost = False
/.../
End Function