整理的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 ...
随机推荐
- UV有问题?
1.检查读取显示贴图的环境与制作贴图环境UV坐标系是否一致. 如:Directx左上角(0,0),右下角(1,1) unity 左下角(0,0),右上角(1,1) 两者互转需要垂直镜像.
- angular : copy vs extend
While using AngularJS, we come across some situation in which we need to copy one object to another ...
- angularJs 指令的封装
首先 指令的应用场景: 1:使你的html更具语义化,不需要深入研究代码就可以知道页面的大概逻辑. 2:抽象出一个自定义组件,在其他的地方进行重用. 一:directive的定义及其使用方法: 下面是 ...
- 洛谷【P2629】好消息,坏消息
浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://www.luogu.org/problemnew/show/P2629 ...
- 安装部署ELK系统监控Azure China的NSG和WAF Log
ELK是一个开源的产品,其官网是:https://www.elastic.co/ ELK主要保护三个产品: Elasticsearch:是基于 JSON 的分布式搜索和分析引擎,专为实现水平扩展.高可 ...
- 蓝桥杯 历届试题 PREV-32 分糖果
历届试题 分糖果 时间限制:1.0s 内存限制:256.0MB 问题描述 有n个小朋友围坐成一圈.老师给每个小朋友随机发偶数个糖果,然后进行下面的游戏: 每个小朋友都把自己的糖果分一半给左手边 ...
- 组装恢复rbd
标签: ceph,ceph实验,rbd cluster相关环境: # cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) # ce ...
- 修改rbd指定位置的数据
标签(空格分隔): ceph,ceph实验 --- 我们通过查看index为0x01的小4M文件,得知了file2.txt这个文件内容在这个4M内保存的位置为0x9000,因为0x01前面还有一个4M ...
- md5加密(2)
package test1; import java.security.MessageDigest; public class MD5Test { //十六进制下数字到字符的映射数组 private ...
- The lesser known pitfalls of allowing file uploads on your website
These days a lot of websites allow users to upload files, but many don’t know about the unknown pitf ...