经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
.NET 窗口/屏幕截图
来源:cnblogs  作者:唐宋元明清2188  时间:2024/7/31 15:00:02  对本文有异议

图像采集源除了显示控件(上一篇《.NET 控件转图片》有介绍从界面控件转图片),更多的是窗口以及屏幕。

窗口截图最常用的方法是GDI,直接上Demo吧:

  1. 1 private void GdiCaptureButton_OnClick(object sender, RoutedEventArgs e)
  2. 2 {
  3. 3 var bitmap = CaptureScreen();
  4. 4 CaptureImage.Source = ConvertBitmapToBitmapSource(bitmap);
  5. 5 }
  6. 6 /// <summary>
  7. 7 /// 截图屏幕
  8. 8 /// </summary>
  9. 9 /// <returns></returns>
  10. 10 public static Bitmap CaptureScreen()
  11. 11 {
  12. 12 IntPtr desktopWindow = GetDesktopWindow();
  13. 13 //获取窗口位置大小
  14. 14 GetWindowRect(desktopWindow, out var lpRect);
  15. 15 return CaptureByGdi(desktopWindow, 0d, 0d, lpRect.Width, lpRect.Height);
  16. 16 }
  17. 17 private BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
  18. 18 {
  19. 19 using MemoryStream memoryStream = new MemoryStream();
  20. 20 // 将 System.Drawing.Bitmap 保存到内存流中
  21. 21 bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
  22. 22 // 重置内存流的指针到开头
  23. 23 memoryStream.Seek(0, SeekOrigin.Begin);
  24. 24
  25. 25 // 创建 BitmapImage 对象并从内存流中加载图像
  26. 26 BitmapImage bitmapImage = new BitmapImage();
  27. 27 bitmapImage.BeginInit();
  28. 28 bitmapImage.StreamSource = memoryStream;
  29. 29 bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  30. 30 bitmapImage.EndInit();
  31. 31 // 确保内存流不会被回收
  32. 32 bitmapImage.Freeze();
  33. 33 return bitmapImage;
  34. 34 }
  35. 35 /// <summary>
  36. 36 /// 截图窗口/屏幕
  37. 37 /// </summary>
  38. 38 /// <param name="windowIntPtr">窗口句柄(窗口或者桌面)</param>
  39. 39 /// <param name="left">水平坐标</param>
  40. 40 /// <param name="top">竖直坐标</param>
  41. 41 /// <param name="width">宽度</param>
  42. 42 /// <param name="height">高度</param>
  43. 43 /// <returns></returns>
  44. 44 private static Bitmap CaptureByGdi(IntPtr windowIntPtr, double left, double top, double width, double height)
  45. 45 {
  46. 46 IntPtr windowDc = GetWindowDC(windowIntPtr);
  47. 47 IntPtr compatibleDc = CreateCompatibleDC(windowDc);
  48. 48 IntPtr compatibleBitmap = CreateCompatibleBitmap(windowDc, (int)width, (int)height);
  49. 49 IntPtr bitmapObj = SelectObject(compatibleDc, compatibleBitmap);
  50. 50 BitBlt(compatibleDc, 0, 0, (int)width, (int)height, windowDc, (int)left, (int)top, CopyPixelOperation.SourceCopy);
  51. 51 Bitmap bitmap = System.Drawing.Image.FromHbitmap(compatibleBitmap);
  52. 52 //释放
  53. 53 SelectObject(compatibleDc, bitmapObj);
  54. 54 DeleteObject(compatibleBitmap);
  55. 55 DeleteDC(compatibleDc);
  56. 56 ReleaseDC(windowIntPtr, windowDc);
  57. 57 return bitmap;
  58. 58 }

根据user32.dll下拿到的桌面信息-句柄获取桌面窗口的设备上下文,再以设备上下文分别创建内存设备上下文、设备位图句柄

  1. 1 BOOL BitBlt(
  2. 2 HDC hdcDest, // 目标设备上下文
  3. 3 int nXDest, // 目标起始x坐标
  4. 4 int nYDest, // 目标起始y坐标
  5. 5 int nWidth, // 宽度(像素)
  6. 6 int nHeight, // 高度(像素)
  7. 7 HDC hdcSrc, // 源设备上下文
  8. 8 int nXSrc, // 源起始x坐标
  9. 9 int nYSrc, // 源起始y坐标
  10. 10 DWORD dwRop // 操作码(如CopyPixelOperation.SourceCopy)
  11. 11 );

图像位块传输BitBlt是最关键的函数,Gdi提供用于在设备上下文之间进行位图块的传输,从原设备上下文复现位图到创建的设备上下文

另外,与Bitblt差不多的还有StretchBlt,StretchBlt也是复制图像,但可以同时对图像进行拉伸或者缩小,需要缩略图可以用这个方法

然后以设备位图句柄输出一个位图System.Drawing.Bitmap,使用到的User32、Gdi32函数:

  1. 1 /// <summary>
  2. 2 /// 获取桌面窗口
  3. 3 /// </summary>
  4. 4 /// <returns></returns>
  5. 5 [DllImport("user32.dll")]
  6. 6 public static extern IntPtr GetDesktopWindow();
  7. 7 /// <summary>
  8. 8 /// 获取整个窗口的矩形区域
  9. 9 /// </summary>
  10. 10 /// <returns></returns>
  11. 11 [DllImport("user32.dll", SetLastError = true)]
  12. 12 public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  13. 13 /// <summary>
  14. 14 /// 检索整个窗口的设备上下文
  15. 15 /// </summary>
  16. 16 /// <param name="hWnd">具有要检索的设备上下文的窗口的句柄</param>
  17. 17 /// <returns></returns>
  18. 18 [DllImport("user32.dll", SetLastError = true)]
  19. 19 public static extern IntPtr GetWindowDC(IntPtr hWnd);
  20. 20 /// <summary>
  21. 21 /// 创建与指定设备兼容的内存设备上下文
  22. 22 /// </summary>
  23. 23 /// <param name="hdc">现有 DC 的句柄</param>
  24. 24 /// <returns>如果函数成功,则返回值是内存 DC 的句柄,否则返回Null</returns>
  25. 25 [DllImport("gdi32.dll")]
  26. 26 public static extern IntPtr CreateCompatibleDC([In] IntPtr hdc);
  27. 27 /// <summary>
  28. 28 /// 将对象选择到指定的设备上下文中
  29. 29 /// </summary>
  30. 30 /// <param name="hdc">DC 的句柄</param>
  31. 31 /// <param name="gdiObj">要选择的对象句柄</param>
  32. 32 /// <returns>如果函数成功,则返回值是兼容位图 (DDB) 的句柄,否则返回Null</returns>
  33. 33 [DllImport("gdi32.dll")]
  34. 34 public static extern IntPtr SelectObject([In] IntPtr hdc, [In] IntPtr gdiObj);
  35. 35 /// <summary>
  36. 36 /// 创建与与指定设备上下文关联的设备的位图
  37. 37 /// </summary>
  38. 38 /// <param name="hdc">设备上下文的句柄</param>
  39. 39 /// <param name="nWidth">位图宽度(以像素为单位)</param>
  40. 40 /// <param name="nHeight">位图高度(以像素为单位)</param>
  41. 41 /// <returns></returns>
  42. 42 [DllImport("gdi32.dll")]
  43. 43 public static extern IntPtr CreateCompatibleBitmap([In] IntPtr hdc, int nWidth, int nHeight);
  44. 44 /// <summary>
  45. 45 /// 执行与从指定源设备上下文到目标设备上下文中的像素矩形对应的颜色数据的位块传输
  46. 46 /// </summary>
  47. 47 /// <param name="hdcDest">目标设备上下文的句柄</param>
  48. 48 /// <param name="xDest">目标矩形左上角的 x 坐标(逻辑单位)</param>
  49. 49 /// <param name="yDest">目标矩形左上角的 y 坐标(逻辑单位)</param>
  50. 50 /// <param name="wDest">源矩形和目标矩形的宽度(逻辑单位)</param>
  51. 51 /// <param name="hDest">源矩形和目标矩形的高度(逻辑单位)</param>
  52. 52 /// <param name="hdcSource">源设备上下文的句柄</param>
  53. 53 /// <param name="xSrc">源矩形左上角的 x 坐标(逻辑单位)</param>
  54. 54 /// <param name="ySrc">源矩形左上角的 y 坐标(逻辑单位)</param>
  55. 55 /// <param name="rop">定义如何将源矩形的颜色数据与目标矩形的颜色数据相结合</param>
  56. 56 /// <returns></returns>
  57. 57 [DllImport("gdi32.dll")]
  58. 58 public static extern bool BitBlt(IntPtr hdcDest,
  59. 59 int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource,
  60. 60 int xSrc, int ySrc, CopyPixelOperation rop);
  61. 61 /// <summary>
  62. 62 /// 删除逻辑笔、画笔、字体、位图、区域或调色板,释放与对象关联的所有系统资源。
  63. 63 /// 删除对象后,指定的句柄将不再有效。
  64. 64 /// </summary>
  65. 65 /// <param name="hObject"></param>
  66. 66 /// <returns></returns>
  67. 67 [DllImport("gdi32.dll")]
  68. 68 public static extern bool DeleteObject(IntPtr hObject);
  69. 69 /// <summary>
  70. 70 /// 删除指定的设备上下文
  71. 71 /// </summary>
  72. 72 /// <param name="hdc">设备上下文的句设备上下文的句</param>
  73. 73 /// <returns></returns>
  74. 74 [DllImport("gdi32.dll")]
  75. 75 public static extern bool DeleteDC([In] IntPtr hdc);
  76. 76 /// <summary>
  77. 77 /// 释放设备上下文 (DC),释放它以供其他应用程序使用
  78. 78 /// </summary>
  79. 79 /// <param name="hWnd"></param>
  80. 80 /// <param name="hdc"></param>
  81. 81 /// <returns></returns>
  82. 82 [DllImport("user32.dll", SetLastError = true)]
  83. 83 public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hdc);
  84. 84
  85. 85 /// <summary>
  86. 86 /// 定义一个矩形区域。
  87. 87 /// </summary>
  88. 88 [StructLayout(LayoutKind.Sequential)]
  89. 89 public struct RECT
  90. 90 {
  91. 91 /// <summary>
  92. 92 /// 矩形左侧的X坐标。
  93. 93 /// </summary>
  94. 94 public int Left;
  95. 95
  96. 96 /// <summary>
  97. 97 /// 矩形顶部的Y坐标。
  98. 98 /// </summary>
  99. 99 public int Top;
  100. 100
  101. 101 /// <summary>
  102. 102 /// 矩形右侧的X坐标。
  103. 103 /// </summary>
  104. 104 public int Right;
  105. 105
  106. 106 /// <summary>
  107. 107 /// 矩形底部的Y坐标。
  108. 108 /// </summary>
  109. 109 public int Bottom;
  110. 110
  111. 111 /// <summary>
  112. 112 /// 获取矩形的宽度。
  113. 113 /// </summary>
  114. 114 public int Width => Right - Left;
  115. 115
  116. 116 /// <summary>
  117. 117 /// 获取矩形的高度。
  118. 118 /// </summary>
  119. 119 public int Height => Bottom - Top;
  120. 120
  121. 121 /// <summary>
  122. 122 /// 初始化一个新的矩形。
  123. 123 /// </summary>
  124. 124 /// <param name="left">矩形左侧的X坐标。</param>
  125. 125 /// <param name="top">矩形顶部的Y坐标。</param>
  126. 126 /// <param name="right">矩形右侧的X坐标。</param>
  127. 127 /// <param name="bottom">矩形底部的Y坐标。</param>
  128. 128 public RECT(int left, int top, int right, int bottom)
  129. 129 {
  130. 130 Left = left;
  131. 131 Top = top;
  132. 132 Right = right;
  133. 133 Bottom = bottom;
  134. 134 }
  135. 135 }
View Code

还有一种比较简单的方法Graphics.CopyFromScreen,看看调用DEMO:

  1. 1 private void GraphicsCaptureButton_OnClick(object sender, RoutedEventArgs e)
  2. 2 {
  3. 3 var image = CaptureScreen1();
  4. 4 CaptureImage.Source = ConvertBitmapToBitmapSource(image);
  5. 5 }
  6. 6 /// <summary>
  7. 7 /// 截图屏幕
  8. 8 /// </summary>
  9. 9 /// <returns></returns>
  10. 10 public static Bitmap CaptureScreen1()
  11. 11 {
  12. 12 IntPtr desktopWindow = GetDesktopWindow();
  13. 13 //获取窗口位置大小
  14. 14 GetWindowRect(desktopWindow, out var lpRect);
  15. 15 return CaptureScreenByGraphics(0, 0, lpRect.Width, lpRect.Height);
  16. 16 }
  17. 17 /// <summary>
  18. 18 /// 截图屏幕
  19. 19 /// </summary>
  20. 20 /// <param name="x">x坐标</param>
  21. 21 /// <param name="y">y坐标</param>
  22. 22 /// <param name="width">截取的宽度</param>
  23. 23 /// <param name="height">截取的高度</param>
  24. 24 /// <returns></returns>
  25. 25 public static Bitmap CaptureScreenByGraphics(int x, int y, int width, int height)
  26. 26 {
  27. 27 var bitmap = new Bitmap(width, height);
  28. 28 using var graphics = Graphics.FromImage(bitmap);
  29. 29 graphics.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy);
  30. 30 return bitmap;
  31. 31 }

Graphics.CopyFromScreen调用简单了很多,与GDI有什么区别?

Graphics.CopyFromScreen内部也是通过GDI.BitBlt来完成屏幕捕获的,封装了下提供更高级别、易胜的API。

测试了下,第一种方法Gdi32性能比Graphics.CopyFromScreen性能略微好一点,冷启动时更明显点,试了2次耗时大概少个10多ms。

所以对于一般应用场景,使用 Graphics.CopyFromScreen 就足够了,但如果你需要更高的控制权和性能优化,建议使用 Gdi32.BitBlt

 kybs00/CaptureImageDemo (github.com)

原文链接:https://www.cnblogs.com/kybs0/p/18330803

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号