C# 根据窗口句柄获取窗口截图
2024-09-03 17:30:05
本章介绍如何通过句柄,截取指定窗口内容,以及截取失败的场景
一、根据窗口句柄获取窗口截图
先创建一个测试窗口程序A,显示如下:
同时我们把此窗口的句柄显示到一个文本输入框内。
1 TestBox.Text = new WindowInteropHelper(this).Handle.ToString();
如上图所示,1774674是此窗口的句柄值。
然后,我们新建一个窗口程序B,对窗口A进行截图并展示
1 var windowIntPtr = new IntPtr(1774674);
2 var bitmapImage = GetWindowShotCut(windowIntPtr);
3 TestImage.Source = bitmapImage;
截图方法及详细操作如下:
1 private BitmapImage GetWindowShotCut(IntPtr intPtr)
2 {
3 var image = WindowCaptureHelper.GetShotCutImage(intPtr);
4 var bitmapImage = BitmapConveters.ConvertToBitmapImage(image);
5 return bitmapImage;
6 }
WindowCaptureHelper:
1 public class WindowCaptureHelper
2 {
3 [DllImport("user32.dll")]
4 private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
5 [DllImport("gdi32.dll")]
6 private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
7 [DllImport("gdi32.dll")]
8 private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
9 [DllImport("gdi32.dll")]
10 private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
11 [DllImport("gdi32.dll")]
12 private static extern int DeleteDC(IntPtr hdc);
13 [DllImport("user32.dll")]
14 private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, int nFlags);
15 [DllImport("user32.dll")]
16 private static extern IntPtr GetWindowDC(IntPtr hwnd);
17
18 public static Bitmap GetShotCutImage(IntPtr hWnd)
19 {
20 var hscrdc = GetWindowDC(hWnd);
21 var windowRect = new Rectangle();
22 GetWindowRect(hWnd, ref windowRect);
23 int width = Math.Abs(windowRect.Width- windowRect.X);
24 int height = Math.Abs(windowRect.Height- windowRect.Y);
25 var hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
26 var hmemdc = CreateCompatibleDC(hscrdc);
27 SelectObject(hmemdc, hbitmap);
28 PrintWindow(hWnd, hmemdc, 0);
29 var bmp = Image.FromHbitmap(hbitmap);
30 DeleteDC(hscrdc);
31 DeleteDC(hmemdc);
32 return bmp;
33 }
34 }
BitmapConveters:
1 public class BitmapConveters
2 {
3 [DllImport("gdi32")]
4 static extern int DeleteObject(IntPtr o);
5 public static BitmapSource ConvertToBitMapSource(Bitmap bitmap)
6 {
7 IntPtr intPtrl = bitmap.GetHbitmap();
8 BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPtrl,
9 IntPtr.Zero,
10 Int32Rect.Empty,
11 BitmapSizeOptions.FromEmptyOptions());
12 DeleteObject(intPtrl);
13 return bitmapSource;
14 }
15 public static BitmapImage ConvertToBitmapImage(Bitmap bitmap)
16 {
17 using (MemoryStream stream = new MemoryStream())
18 {
19 bitmap.Save(stream, ImageFormat.Bmp);
20 stream.Position = 0;
21 BitmapImage result = new BitmapImage();
22 result.BeginInit();
23 result.CacheOption = BitmapCacheOption.OnLoad;
24 result.StreamSource = stream;
25 result.EndInit();
26 result.Freeze();
27 return result;
28 }
29 }
30 }
截图后显示如下:
二、窗口截图失败
窗口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)
最新文章
- CentOS一键ftp
- CloudStack系统部署系列教程-KVM
- JS_Ajax基础
- 通知---iOS
- MySQL key/value存储方案(转)
- 电脑U盘启动总结
- 开源自己的一个小android项目(美女撕衣服游戏)
- AForm
- .NET生成PDF文件
- 05).30分钟学会Servlet+过滤器+监听器+实际案例
- jQuery动画详解
- Nagios详解(基础、安装、配置文件解析及监控实例)
- 编程心法 之什么是MVP What is MVP development?
- macos 下usb键盘问题.
- Linux-echo、cat命令详解(14)
- webVR全景图多种方案实现(pannellum,aframe,Krpano,three,jquery-vrview)
- 用python做数字油画或者从一幅画学习风格,去画另一幅画
- UVAlive6807 Túnel de Rata (最小生成树)
- SQL Server 备份数据库到指定路径,任务实现
- 启动vsftpd失败