整理的C#屏幕截图,控件截图程序
代码基本从网上搜集而来,整理成以下文件:
包括屏幕截图(和屏幕上看到的一致);
以及控件截图(只要该控件在本窗口内显示完全且不被其他控件遮挡就可正确截图)
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace LC
- {
- class ScreenCapture
- {
- #region 抓取屏幕
- /// <summary>
- /// 抓取屏幕(层叠的窗口)
- /// </summary>
- /// <param name="x">左上角的横坐标</param>
- /// <param name="y">左上角的纵坐标</param>
- /// <param name="width">抓取宽度</param>
- /// <param name="height">抓取高度</param>
- /// <returns></returns>
- public static Bitmap captureScreen(int x, int y, int width, int height)
- {
- Bitmap bmp = new Bitmap(width, height);
- using (Graphics g = Graphics.FromImage(bmp))
- {
- g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
- g.Dispose();
- }
- //bit.Save(@"capture2.png");
- return bmp;
- }
- /// <summary>
- /// 抓取整个屏幕
- /// </summary>
- /// <returns></returns>
- public static Bitmap captureScreen()
- {
- Size screenSize = Screen.PrimaryScreen.Bounds.Size;
- return captureScreen(0,0,screenSize.Width,screenSize.Height);
- }
- #endregion
- #region 使用BitBlt方法抓取控件,无论控件是否被遮挡
- /// <summary>
- /// 控件(窗口)的截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
- /// </summary>
- /// <param name="control">需要被截图的控件</param>
- /// <returns>该控件的截图,控件被遮挡时也可以正确截图</returns>
- public static Bitmap captureControl(Control control)
- {
- //调用API截屏
- IntPtr hSrce = GetWindowDC(control.Handle);
- IntPtr hDest = CreateCompatibleDC(hSrce);
- IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
- IntPtr hOldBmp = SelectObject(hDest, hBmp);
- if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
- {
- Bitmap bmp = Image.FromHbitmap(hBmp);
- SelectObject(hDest, hOldBmp);
- DeleteObject(hBmp);
- DeleteDC(hDest);
- ReleaseDC(control.Handle, hSrce);
- // bmp.Save(@"a.png");
- // bmp.Dispose();
- return bmp;
- }
- return null;
- }
- // /// <summary>
- // /// 有问题!!!!!用户区域坐标不对啊
- // /// 控件(窗口)的用户区域截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
- // /// </summary>
- // /// <param name="control">需要被截图的控件</param>
- // /// <returns>控件(窗口)的用户区域截图</returns>
- // public static Bitmap captureClientArea(Control control)
- // {
- //
- // Size sz = control.Size;
- // Rectangle rect = control.ClientRectangle;
- //
- //
- // //调用API截屏
- // IntPtr hSrce = GetWindowDC(control.Handle);
- // IntPtr hDest = CreateCompatibleDC(hSrce);
- // IntPtr hBmp = CreateCompatibleBitmap(hSrce, rect.Width, rect.Height);
- // IntPtr hOldBmp = SelectObject(hDest, hBmp);
- // if (BitBlt(hDest, 0, 0, rect.Width, rect.Height, hSrce, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
- // {
- // Bitmap bmp = Image.FromHbitmap(hBmp);
- // SelectObject(hDest, hOldBmp);
- // DeleteObject(hBmp);
- // DeleteDC(hDest);
- // ReleaseDC(control.Handle, hSrce);
- // // bmp.Save(@"a.png");
- // // bmp.Dispose();
- // return bmp;
- // }
- // return null;
- //
- // }
- #endregion
- #region 使用PrintWindow方法抓取窗口,无论控件是否被遮挡
- /// <summary>
- /// 窗口的截图,窗口被遮挡时也可以正确截图,使用PrintWindow方法
- /// </summary>
- /// <param name="control">需要被截图的窗口</param>
- /// <returns>窗口的截图,控件被遮挡时也可以正确截图</returns>
- public static Bitmap captureWindowUsingPrintWindow(Form form)
- {
- return GetWindow(form.Handle);
- }
- private static Bitmap GetWindow(IntPtr hWnd)
- {
- IntPtr hscrdc = GetWindowDC(hWnd);
- Control control = Control.FromHandle(hWnd);
- IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
- IntPtr hmemdc = CreateCompatibleDC(hscrdc);
- SelectObject(hmemdc, hbitmap);
- PrintWindow(hWnd, hmemdc, 0);
- Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
- DeleteDC(hscrdc);//删除用过的对象
- DeleteDC(hmemdc);//删除用过的对象
- return bmp;
- }
- #endregion
- #region DLL calls
- [DllImport("gdi32.dll")]
- static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
- wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
- [DllImport("gdi32.dll")]
- static extern IntPtr DeleteDC(IntPtr hDc);
- [DllImport("gdi32.dll")]
- static extern IntPtr DeleteObject(IntPtr hDc);
- [DllImport("gdi32.dll")]
- static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
- [DllImport("gdi32.dll")]
- static extern IntPtr CreateCompatibleDC(IntPtr hdc);
- [DllImport("gdi32.dll")]
- static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
- [DllImport("user32.dll")]
- public static extern IntPtr GetDesktopWindow();
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowDC(IntPtr ptr);
- [DllImport("user32.dll")]
- public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
- [DllImport("user32.dll")]
- static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
- #endregion
- }
- }
整理的C#屏幕截图,控件截图程序的更多相关文章
- DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表
原文:DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助 ...
- ActiveX 控件导入程序
ActiveX 控件导入程序将 ActiveX 控件的 COM 类型库中的类型定义转换为 Windows 窗体控件. http://msdn.microsoft.com/zh-cn/library/8 ...
- 使用SplashScreenManager控件定制程序加载页面
需要devexpress版本在12.0及以上才支持 https://www.cnblogs.com/wuhuacong/p/6112461.html 在DevExpress程序中使用SplashScr ...
- C# WinForm 技巧:控件截图
Point screenPoint = 控件.PointToScreen(new Point()); Rectangle rect = new Rectangle(screenPoint, chart ...
- 【iOS】控件截图、MP4格式视频流和m3u8格式视频流截取某一帧功能的实现
最近开发遇到一个点击按钮实现直播视频流截屏的功能,去网上查了一下资料,总结了一下iOS中截屏相关的知识,然后自己做了个demo. demo主要实现了3种截屏方法,分别对应三种不同的应用场景. 1.im ...
- Winform DevExpress控件库(二) 使用SplashScreenManager控件定制程序加载页面
SplashScreenManager控件:主要作用是显示在进行耗时操作时的等待界面: 位于 工具箱 -> Navigation & Layout(导航栏与布局类控件) 目录下: 在工具 ...
- WPF控件截图
//截图 RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height) { ...
- WPF 控件截图位置不正确的问题
用WPF的RenderTargetBitmap可以截取控件内容到一张图片上,但是实际使用的时候经常出现截取的位置不正确的问题.今天是第二次解决这个问题,所以记录下,免得再忘了. RenderTarge ...
- Delphi实现拍照控件的程序代码
完整的delphi拍照控件代码,实现利用摄像头进行拍照的功能.需要TVideoCap控件支持. procedure Tfrm1.Button2Click(Sender: TObject); Var j ...
随机推荐
- 解决 No module named PyQt5.QtWebKitWidgets
原因:在 PyQt 5.6(+) 版本中, 新增 QtWebEngineWidgets 代替QtWebKitWidgets. 示例代码:#coding: utf-8 import sysfrom Py ...
- unix下网络编程之I/O复用(三)
poll函数 在上文unix下网络编程之I/O复用(二)中已经介绍了select函数的相关使用,本文将介绍另一个常用的I/O复用函数poll.poll提供的功能与select类似,不过在处理流设备时, ...
- Gwt第三方组件、框架介绍
介绍一下我接触过的Gwt第三方组件.框架及项目 1. Mygwt 曾经的大名鼎鼎的gwt第三方框架,在某些gwt框架的排名中排名第一.这个框架完全用gwt的方式实现了ext-js的功能,不依赖于ext ...
- linux串口基本编程
Linux的串口表现为设备文件.Linux的串口设备文件命名一般为/dev/ttySn(n=0.1.2„„),若串口是USB扩展的,则串口设备文件命名多为/dev/ttyUSBn(n=0.1.2„„) ...
- Git学习笔记(三)远程库(GitHub)协同开发,fork和忽略特殊文件
远程库 远程库,通俗的讲就是不再本地的git仓库!他的工作方式和我们本地的一样,但是要使用他就需要先建立连接! 远程库有两种,一个是自己搭建的git服务器:另一种就是使用GitHub,这个网站就是提供 ...
- Celery-4.1 用户指南: Concurrency (并发)
简介 Eventlet 的主页对它进行了描述:它是一个python的并发网络库,可以让你更改如何运行你的代码而不是怎么编写代码. 对高可扩展非阻塞IO操作,它使用 epoll或者libevent. C ...
- [更新中]【South使用总结】django开发中使用South进行数据库迁移
Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...
- 【SymmetricDS】SymmetricDS是如何工作的
2018-04-20 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/8890785.html 本文翻译自SymmetricDS官方文档 ...
- Windows10更新后无限重启
以安全模式进入系统,禁用或卸载显卡驱动. 重启后重新安装驱动.
- oracle DCL-(grant、revoke )
1.授权GRANT <权限列表> to <user_name>; 2.收回权限REVOKE <权限列表> from <user_name>