win81截图工具-(win8系统截图工具)

来源:191路由网 2023-01-17 22:58:39

win81截图工具 (win8系统截图工具) 前言

Windows 上面,屏幕截图通常被调用 win32 api 完成的,如果 C# 要实现截图功能,需要封装相关功能 api。在 Windows 上,主图形接口有 GDI 和 DirectX。GDI 界面更灵活,可以截取指定的窗口,即使窗口被屏蔽或位于显示区外,但兼容性较低,无法截取 DX 接口输出的图片。DirectX 它是一个高性能的图形接口(当然,还有其他功能,与本文无关,忽略),主要用作游戏图形接口,灵活性低,不能指定特定的窗口(或只是我不能),但兼容性高,可以根据情况截取任何输出到屏幕的内容。

正文

使用了以下代码 C# 8.0 只能使用新功能 VS 2019 如果需要在旧版本中编译, VS 使用时,需要自行改造。

GDI

简单地用静态类包装 GDI 并调用接口截图。

1 public static class CaptureWindow 2{ 3 #region 类 4 /// <summary> 5 /// Helper class containing User32 API functions 6 /// </summary> 7 private class User32 8{ 9[StructLayout(LayoutKind.Sequential)]10 public struct RECT 11{ 12 public int left; 13 public int top; 14 public int right; 15 public int bottom; 16 }17[DllImport("user32.dll")]18 public static extern IntPtr GetDesktopWindow(); 19[DllImport("user32.dll")]20 public static extern IntPtr GetWindowDC(IntPtr hWnd); 21[DllImport("user32.dll")]22 public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); 23[DllImport("user32.dll")]24 public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); 25 26[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Unicode)]27 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 28 }29 30 private class Gdi32 31{ 32 33 public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter 34[DllImport("gdi32.dll")]35 public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, 36 int nWidth, int nHeight, IntPtr hObjectSource, 37 int nXSrc, int nYSrc, int dwRop); 38[DllImport("gdi32.dll")]39 public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, 40 int nHeight); 41[DllImport("gdi32.dll")]42 public static extern IntPtr CreateCompatibleDC(IntPtr hDC); 43[DllImport("gdi32.dll")]44 public static extern bool DeleteDC(IntPtr hDC); 45[DllImport("gdi32.dll")]46 public static extern bool DeleteObject(IntPtr hObject); 47[DllImport("gdi32.dll")]48 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); 49 }50 #endregion 51 52 /// <summary> 53 /// 根据句柄截图 54 /// </summary> 55 /// <param name="hWnd">句柄</param> 56 /// <returns></returns> 57 public static Image ByHwnd(IntPtr hWnd) 58{ 59 // get te hDC of the target window 60 IntPtr hdcSrc = User32.GetWindowDC(hWnd); 61 // get the size 62 User32.RECT windowRect = new User32.RECT(); 63 User32.GetWindowRect(hWnd, ref windowRect); 64 int width = windowRect.right - windowRect.left; 65 int height = windowRect.bottom - windowRect.top; 66 // create a device context we can copy to 67 IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); 68 // create a bitmap we can copy it to, 69 // using GetDeviceCaps to get the width/height 70 IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); 71 // select the bitmap object 72 IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap); 73 // bitblt over 74 Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY); 75 // restore selection 76 Gdi32.SelectObject(hdcDest, hOld); 77 // clean up 78 Gdi32.DeleteDC(hdcDest); 79 User32.ReleaseDC(hWnd, hdcSrc); 80 // get a .NET image object for it 81 Image img = Image.FromHbitmap(hBitmap); 82 // free up the Bitmap object 83 Gdi32.DeleteObject(hBitmap); 84 return img; 85 }86 87 /// <summary> 88 /// 根据窗口名称截图 89 /// </summary> 90 /// <param name="windowName">窗口名称</param> 91 /// <returns></returns> 92 public static Image ByName(string windowName) 93{ 94 IntPtr handle = User32.FindWindow(null, windowName); 95 IntPtr hdcSrc = User32.GetWindowDC(handle); 96 User32.RECT windowRect = new User32.RECT(); 97 User32.GetWindowRect(handle, ref windowRect); 98 int width = windowRect.right - windowRect.left; 99 int height = windowRect.bottom - windowRect.

点击展开全文

相关阅读

u盘装win10拔掉u盘时间-(u盘安装win10什么时候拔掉)

u盘装win10拔掉u盘时间(u盘安装win10何时拔掉)u盘重装系统win10.第一步是将系统u盘插入主机后面。记住,蓝色的嘴必须插入蓝色的嘴现在给电脑开机,开始充电系统,开机后快速按下f这是华硕的引导键,如果是微星的主板,按下f11,技嘉的是f12,像我一样一直点我们可以看到屏幕上有这样一个选择界面。下面,我们可以记住...

2023-01-17

u盘制作启动盘萝卜-(白菜u盘启动盘怎么制作)

u盘制开始盘萝卜(如何制作白菜u盘启动盘)昨天写了如何制作u盘启动盘,今天写了一个如何安装系统的教程,专门为小白准备,新手上路,希望关注。1、系统下载原版下载:可以去msdn下载,但是比较慢,也可以去下面的网站下载也可以到下载,都是原版。GHOST版本下载(重新封包的版本...

2023-01-17

lenovog50u盘安装系统-(lenovog50u盘启动)

lenovog50u盘安装系统(lenovog50u盘启动)u磁盘重新安装系统,品牌联想电脑BIOS设置U磁盘启动教程,联想电脑不同型号打开BIOS设置界面可能有一些差异,但差异不大,可以参考操作。不同品牌的电脑有不同的方式进入bio...

2023-01-17

win10xitongbeifen-()

win10xitongbeifen()无论是win7还是win在日常使用中,要注意系统的备份工作,防止系统崩溃时的有效恢复。虽然会丢失一些实时数据,但也可以将风险降到最低水平,提高效率。当然,备份系统的方法有很多。以win以10系统备份为例,与您分享如何使用它win10系统备份功能完成系统备份和恢复操作。一、备份系统步骤1,开始设置更新和安全步骤2:选择备份并单击右侧的“转移到备份和恢复”步骤3...

2023-01-17

华硕u启动识别不u盘-(华硕u启动识别不了u盘)

华硕u启动识别不u盘(华硕u启动识别不了u盘)电脑卡住了,还是出了故障?我们最常用的方法是重新安装系统。说到重新安装系统,首先要做的就是制作U盘启动盘。不久,U当你高兴地将启动盘插入电脑时,你突然发现...

2023-01-17

191路由网 - 192.168.1.1,192.168.0.1无线路由器设置教程

版权声明:本站的文章和图片来自互联网收集,仅做分享之用如有侵权请联系站长,我们将在24小时内删除