本章介绍如何通过句柄,截取指定窗口内容,以及截取失败的场景
一、根据窗口句柄获取窗口截图
先创建一个测试窗口程序A,显示如下:
同时我们把此窗口的句柄显示到一个文本输入框内。
TestBox.Text = new WindowInteropHelper(this).Handle.ToString();
如上图所示,1774674是此窗口的句柄值。
然后,我们新建一个窗口程序B,对窗口A进行截图并展示
var windowIntPtr = new IntPtr(1774674);
var bitmapImage = GetWindowShotCut(windowIntPtr);
TestImage.Source = bitmapImage;
截图方法及详细操作如下:
private BitmapImage GetWindowShotCut(IntPtr intPtr)
{
var image = WindowCaptureHelper.GetShotCutImage(intPtr);
var bitmapImage = BitmapConveters.ConvertToBitmapImage(image);
return bitmapImage;
}
WindowCaptureHelper:
public class WindowCaptureHelper
{
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("gdi32.dll")]
private static extern int DeleteDC(IntPtr hdc);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, int nFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
public static Bitmap GetShotCutImage(IntPtr hWnd)
{
var hscrdc = GetWindowDC(hWnd);
var windowRect = new Rectangle();
GetWindowRect(hWnd, ref windowRect);
int width = Math.Abs(windowRect.Width- windowRect.X);
int height = Math.Abs(windowRect.Height- windowRect.Y);
var hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
var hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
var bmp = Image.FromHbitmap(hbitmap);
DeleteDC(hscrdc);
DeleteDC(hmemdc);
return bmp;
}
}
BitmapConveters:
public class BitmapConveters
{
[DllImport("gdi32")]
static extern int DeleteObject(IntPtr o);
public static BitmapSource ConvertToBitMapSource(Bitmap bitmap)
{
IntPtr intPtrl = bitmap.GetHbitmap();
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPtrl,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(intPtrl);
return bitmapSource;
}
public static BitmapImage ConvertToBitmapImage(Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}
}
截图后显示如下:
二、窗口截图失败
窗口A在特定场景下,我们截到的窗口内容是黑色的:
截图获取失败了,窗口A做了什么处理?
定位发现是属性AllowsTransparency="True"的锅,搜索了下,网上也有同样的反馈:
- Taking a screenshot of windows with AllowsTransparency=“True” (microsoft.com)
- c# - Capture transparent WPF Window for streaming - Stack Overflow
- .net - Transparent child window renders as black when screen sharing main window on Microsoft Teams - Stack Overflow
官方大佬说,这是他们的一个BUG。在win10 2004更新版本中,已处理。
不过,我现在是win11,依然还有问题。。。我是在win10上直接更新到win11,可能原来那个win10企业LTSC版本有点老,win11更新只更新了UI?或者win11是基于原来旧分支开发的?等回复中…Taking a screenshot of windows with AllowsTransparency=“True” · Issue #358 · microsoft/WindowsCompositionSamples (github.com)