代码基本从网上搜集而来,整理成以下文件: 
包括屏幕截图(和屏幕上看到的一致); 
以及控件截图(只要该控件在本窗口内显示完全且不被其他控件遮挡就可正确截图)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace LC
  10. {
  11. class ScreenCapture
  12. {
  13. #region 抓取屏幕
  14. /// <summary>
  15. /// 抓取屏幕(层叠的窗口)
  16. /// </summary>
  17. /// <param name="x">左上角的横坐标</param>
  18. /// <param name="y">左上角的纵坐标</param>
  19. /// <param name="width">抓取宽度</param>
  20. /// <param name="height">抓取高度</param>
  21. /// <returns></returns>
  22. public static Bitmap captureScreen(int x, int y, int width, int height)
  23. {
  24. Bitmap bmp = new Bitmap(width, height);
  25. using (Graphics g = Graphics.FromImage(bmp))
  26. {
  27. g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
  28. g.Dispose();
  29. }
  30. //bit.Save(@"capture2.png");
  31. return bmp;
  32. }
  33. /// <summary>
  34. ///  抓取整个屏幕
  35. /// </summary>
  36. /// <returns></returns>
  37. public static Bitmap captureScreen()
  38. {
  39. Size screenSize = Screen.PrimaryScreen.Bounds.Size;
  40. return captureScreen(0,0,screenSize.Width,screenSize.Height);
  41. }
  42. #endregion
  43. #region 使用BitBlt方法抓取控件,无论控件是否被遮挡
  44. /// <summary>
  45. /// 控件(窗口)的截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
  46. /// </summary>
  47. /// <param name="control">需要被截图的控件</param>
  48. /// <returns>该控件的截图,控件被遮挡时也可以正确截图</returns>
  49. public static Bitmap captureControl(Control control)
  50. {
  51. //调用API截屏
  52. IntPtr hSrce = GetWindowDC(control.Handle);
  53. IntPtr hDest = CreateCompatibleDC(hSrce);
  54. IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
  55. IntPtr hOldBmp = SelectObject(hDest, hBmp);
  56. if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
  57. {
  58. Bitmap bmp = Image.FromHbitmap(hBmp);
  59. SelectObject(hDest, hOldBmp);
  60. DeleteObject(hBmp);
  61. DeleteDC(hDest);
  62. ReleaseDC(control.Handle, hSrce);
  63. // bmp.Save(@"a.png");
  64. // bmp.Dispose();
  65. return bmp;
  66. }
  67. return null;
  68. }
  69. //         /// <summary>
  70. //         /// 有问题!!!!!用户区域坐标不对啊
  71. //         /// 控件(窗口)的用户区域截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
  72. //         /// </summary>
  73. //         /// <param name="control">需要被截图的控件</param>
  74. //         /// <returns>控件(窗口)的用户区域截图</returns>
  75. //         public static Bitmap captureClientArea(Control control)
  76. //         {
  77. //
  78. //             Size sz = control.Size;
  79. //             Rectangle rect = control.ClientRectangle;
  80. //
  81. //
  82. //             //调用API截屏
  83. //             IntPtr hSrce = GetWindowDC(control.Handle);
  84. //             IntPtr hDest = CreateCompatibleDC(hSrce);
  85. //             IntPtr hBmp = CreateCompatibleBitmap(hSrce, rect.Width, rect.Height);
  86. //             IntPtr hOldBmp = SelectObject(hDest, hBmp);
  87. //             if (BitBlt(hDest, 0, 0, rect.Width, rect.Height, hSrce, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
  88. //             {
  89. //                 Bitmap bmp = Image.FromHbitmap(hBmp);
  90. //                 SelectObject(hDest, hOldBmp);
  91. //                 DeleteObject(hBmp);
  92. //                 DeleteDC(hDest);
  93. //                 ReleaseDC(control.Handle, hSrce);
  94. //                 // bmp.Save(@"a.png");
  95. //                 // bmp.Dispose();
  96. //                 return bmp;
  97. //             }
  98. //             return null;
  99. //
  100. //         }
  101. #endregion
  102. #region 使用PrintWindow方法抓取窗口,无论控件是否被遮挡
  103. /// <summary>
  104. /// 窗口的截图,窗口被遮挡时也可以正确截图,使用PrintWindow方法
  105. /// </summary>
  106. /// <param name="control">需要被截图的窗口</param>
  107. /// <returns>窗口的截图,控件被遮挡时也可以正确截图</returns>
  108. public static Bitmap captureWindowUsingPrintWindow(Form form)
  109. {
  110. return GetWindow(form.Handle);
  111. }
  112. private static Bitmap GetWindow(IntPtr hWnd)
  113. {
  114. IntPtr hscrdc = GetWindowDC(hWnd);
  115. Control control = Control.FromHandle(hWnd);
  116. IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
  117. IntPtr hmemdc = CreateCompatibleDC(hscrdc);
  118. SelectObject(hmemdc, hbitmap);
  119. PrintWindow(hWnd, hmemdc, 0);
  120. Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
  121. DeleteDC(hscrdc);//删除用过的对象
  122. DeleteDC(hmemdc);//删除用过的对象
  123. return bmp;
  124. }
  125. #endregion
  126. #region  DLL calls
  127. [DllImport("gdi32.dll")]
  128. static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
  129. wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
  130. [DllImport("gdi32.dll")]
  131. static extern IntPtr DeleteDC(IntPtr hDc);
  132. [DllImport("gdi32.dll")]
  133. static extern IntPtr DeleteObject(IntPtr hDc);
  134. [DllImport("gdi32.dll")]
  135. static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
  136. [DllImport("gdi32.dll")]
  137. static extern IntPtr CreateCompatibleDC(IntPtr hdc);
  138. [DllImport("gdi32.dll")]
  139. static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
  140. [DllImport("user32.dll")]
  141. public static extern IntPtr GetDesktopWindow();
  142. [DllImport("user32.dll")]
  143. public static extern IntPtr GetWindowDC(IntPtr ptr);
  144. [DllImport("user32.dll")]
  145. public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
  146. [DllImport("user32.dll")]
  147. static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
  148. #endregion
  149. }
  150. }

整理的C#屏幕截图,控件截图程序的更多相关文章

  1. DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表

    原文:DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助 ...

  2. ActiveX 控件导入程序

    ActiveX 控件导入程序将 ActiveX 控件的 COM 类型库中的类型定义转换为 Windows 窗体控件. http://msdn.microsoft.com/zh-cn/library/8 ...

  3. 使用SplashScreenManager控件定制程序加载页面

    需要devexpress版本在12.0及以上才支持 https://www.cnblogs.com/wuhuacong/p/6112461.html 在DevExpress程序中使用SplashScr ...

  4. C# WinForm 技巧:控件截图

    Point screenPoint = 控件.PointToScreen(new Point()); Rectangle rect = new Rectangle(screenPoint, chart ...

  5. 【iOS】控件截图、MP4格式视频流和m3u8格式视频流截取某一帧功能的实现

    最近开发遇到一个点击按钮实现直播视频流截屏的功能,去网上查了一下资料,总结了一下iOS中截屏相关的知识,然后自己做了个demo. demo主要实现了3种截屏方法,分别对应三种不同的应用场景. 1.im ...

  6. Winform DevExpress控件库(二) 使用SplashScreenManager控件定制程序加载页面

    SplashScreenManager控件:主要作用是显示在进行耗时操作时的等待界面: 位于 工具箱 -> Navigation & Layout(导航栏与布局类控件) 目录下: 在工具 ...

  7. WPF控件截图

    //截图         RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height)         { ...

  8. WPF 控件截图位置不正确的问题

    用WPF的RenderTargetBitmap可以截取控件内容到一张图片上,但是实际使用的时候经常出现截取的位置不正确的问题.今天是第二次解决这个问题,所以记录下,免得再忘了. RenderTarge ...

  9. Delphi实现拍照控件的程序代码

    完整的delphi拍照控件代码,实现利用摄像头进行拍照的功能.需要TVideoCap控件支持. procedure Tfrm1.Button2Click(Sender: TObject); Var j ...

随机推荐

  1. spark gateway引发:跟踪Cloudera安装服务异常日志跟踪

    spark gateway是用于接收cloudera管理的应用:可以上报数据,不影响正常使用.启动gateway失败,我觉得可能是因为配置问题? 这个问题可能比较深,因为我通过查看日志(clouder ...

  2. DIY ESXI虚拟化服务器再度升级ESXI6.0 (U盘安装Esxi)

    前期我写了一个篇关于<IT屌丝DIY ESXI虚拟化服务器记实 >链接地址:http://lidongni.blog.51cto.com/2554605/1643996,这次主要是在原有的 ...

  3. live555源码分析----RSTPServer创建过程分析

    最近五一回家,终于有机会能安静的看一下流媒体这方面相关的知识,准备分析live555的源码,接下来会把我读源码的过程记录成博客,以供其他的同路人参考. 因为再读源码的过程中,并不是一路顺着读下来,往往 ...

  4. Spring 学习一 @Autowired

    @Autowired Spring 2.5 引入了 @Autowired ,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方 ...

  5. Spring MVC配置详解(1)

    web.xml的配置 <!-- 配置前端控制器 前端控制器(DispatcherServlet)--> <servlet> <servlet-name>spring ...

  6. 2015.3.4 VS2005调用MFC dll时报错及解决

    今天在用VS2005调用MFCdll时报错: 正试图在 os 加载程序锁内执行托管代码.不要尝试在 DllMain 或映像初始化函数内运行托管代码... 原因是我在dll的CSpaceApp::CSp ...

  7. Repmat:Replicate and tile an array

    Repmat:Replicate and tile an array Syntax B = repmat(A,m,n) B = repmat(A,[m n]) B = repmat(A,[m n p. ...

  8. xcode中用pods管理第三方库<转>

    安装pods :http://www.cnblogs.com/wangluochong/p/5567082.html 史上最详细的CocoaPods安装教程 --------------------- ...

  9. 【276】◀▶ Python 字符串函数说明

    参考:Python 字符串函数 01   capitalize 把字符串的第一个字符大写,其他字母变小写. 02   center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...

  10. Windows系统上release版本程序bug跟踪解决方案-.dmp文件。

    使用场景: Win32程序在release模式下编译完成,发送给最终用户使用时,我们的程序有时候也会出现崩溃的情况,这个时候如果能快速定位崩溃原因或提供一些程序崩溃时的状态信息,对我们解决问题将会带来 ...