如何捕获屏幕的一部分
我正在使用 win32 PrintWindow 函数将屏幕捕获到 BitMap 对象。
如果我只想捕获窗口的一个区域,如何裁剪内存中的图像?
这是我用来捕获整个窗口的代码:
[System.Runtime.InteropServices.DllImport(strUSER32DLL, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int PrintWindow(IntPtr hWnd, IntPtr hBltDC, uint iFlags);
public enum enPrintWindowFlags : uint
{
/// <summary>
///
/// </summary>
PW_ALL = 0x00000000,
/// <summary>
/// Only the client area of the window is copied. By default, the entire window is copied.
/// </summary>
PW_CLIENTONLY = 0x00000001
}
public System.Drawing.Bitmap CaptureWindow(IntPtr hWnd, enPrintWindowFlags eFlags)
{
System.Drawing.Rectangle rctForm = System.Drawing.Rectangle.Empty;
using(System.Drawing.Graphics grfx = System.Drawing.Graphics.FromHdc(GetWindowDC(hWnd)))
{
rctForm = System.Drawing.Rectangle.Round(grfx.VisibleClipBounds);
}
System.Drawing.Bitmap pImage = new System.Drawing.Bitmap(rctForm.Width, rctForm.Height);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pImage);
IntPtr hDC = graphics.GetHdc();
//paint control onto graphics using provided options
try
{
PrintWindow(hWnd, hDC, (uint)eFlags);
}
finally
{
graphics.ReleaseHdc(hDC);
}
return pImage;
}
I am using the win32 PrintWindow function to capture a screen to a BitMap object.
If I only want to capture a region of the window, how can I crop the image in memory?
Here is the code I'm using to capture the entire window:
[System.Runtime.InteropServices.DllImport(strUSER32DLL, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int PrintWindow(IntPtr hWnd, IntPtr hBltDC, uint iFlags);
public enum enPrintWindowFlags : uint
{
/// <summary>
///
/// </summary>
PW_ALL = 0x00000000,
/// <summary>
/// Only the client area of the window is copied. By default, the entire window is copied.
/// </summary>
PW_CLIENTONLY = 0x00000001
}
public System.Drawing.Bitmap CaptureWindow(IntPtr hWnd, enPrintWindowFlags eFlags)
{
System.Drawing.Rectangle rctForm = System.Drawing.Rectangle.Empty;
using(System.Drawing.Graphics grfx = System.Drawing.Graphics.FromHdc(GetWindowDC(hWnd)))
{
rctForm = System.Drawing.Rectangle.Round(grfx.VisibleClipBounds);
}
System.Drawing.Bitmap pImage = new System.Drawing.Bitmap(rctForm.Width, rctForm.Height);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pImage);
IntPtr hDC = graphics.GetHdc();
//paint control onto graphics using provided options
try
{
PrintWindow(hWnd, hDC, (uint)eFlags);
}
finally
{
graphics.ReleaseHdc(hDC);
}
return pImage;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地抓取整个屏幕,然后将图像传递到裁剪函数中,该函数选择整个图像的一个区域。看一下 Bitmap.Clone() 方法。例如
,请注意,我从 此博客中提取了此内容
You could simply grab the whole screen and then pass the image into a cropping function that selects a region of the total image. Take a look at the Bitmap.Clone() method. e.g.
Note, I pulled this down from this blog
以下是捕获屏幕并创建 100 像素正方形的裁剪图像的完整代码。该代码取自按钮 Click 事件。使用你需要的东西。
Here's the complete code to capture the screen and create a cropped image of 100 pixels square. The code is taken from a button Click event. Use what you need.
为自己省去一些麻烦,请获取 Cropper 的源代码。
Save yourself some trouble and pick up the source to Cropper.