当前位置:网站首页>C# wpf 实现截屏框实时截屏功能
C# wpf 实现截屏框实时截屏功能
2022-07-04 12:50:00 【CodeOfCC】
前言
在《C# wpf 使用DockPanel实现截屏框》中我们实现了一个截屏框,接下来就要实现相应的截屏功能了。获取截屏区域然后使用GDI+截屏,在这里有些细节需要处理,比如要隐藏截屏框上的控件避免遮挡,处理不同dpi下的坐标位置。
一、实现步骤
1、获取截屏区域
以《C# wpf 使用DockPanel实现截屏框》的界面为例,clipRect是截屏区域控件对象,下面两行代码就可以获取到准确的截屏区域,且不受dpi影响。
var leftTop = clipRect.PointToScreen(new Point(0, 0));
var rightBottom = clipRect.PointToScreen(new Point(clipRect.ActualWidth, clipRect.ActualHeight));
2、隐藏控件
我们需要截屏框内的隐藏控件,比如8个拖动点或工具条。
clipRect.Visibility = Visibility.Hidden;
(1)问题
直接隐藏控件然后截屏是无效的。因为wpf的绘制机制是完全异步的,无法知道确切的屏幕上绘制完成的时间。所以隐藏后立刻截图依然会截取到控件。
(2)解决方法
简单的解决方法是延时截屏,隐藏控件后500毫秒再截屏。但是这种方法不稳定,尤其是在生产环境,用户的机器各种各样,只要有点卡还是会出bug。
完美的解决方案是使用动画,我们利用动画的多帧绘制特性以及完成事件回调来获取控件真正绘制完成后的时机。具体步骤如下:
隐藏控件->创建动画->注册Completed事件->启动动画->Completed事件中截屏
3、截屏
利用上面步骤获取到的截屏区域,结合《C# wpf 使用GDI+实现截屏》里的简单截屏即完成。截屏后还原被隐藏的控件。
二、完整代码
https://download.csdn.net/download/u013113678/85855030
三、效果预览
总结
以上就是今天要讲的内容,本文简单介绍了wpf截屏框截屏的方法。需要注意两个细节,一个是dpi处理,一个是控件隐藏。gdi+截屏就相对容易实现了,当然还有截屏框的实现还是略微不容易的。这样的一套截屏功能对功能进行简单的拓展就可以用于正式项目了。
边栏推荐
- Migration from go vendor project to mod project
- Unity Shader学习(三)试着绘制一个圆
- 德明利深交所上市:市值31亿 为李虎与田华夫妻档
- Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
- 2022危险化学品经营单位主要负责人练习题及模拟考试
- Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
- 英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
- PHP log debugging
- One of the solutions for unity not recognizing riders
猜你喜欢
205. 同构字符串
2022年起重机械指挥考试模拟100题模拟考试平台操作
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
硬件基础知识-二极管基础
小程序直播 + 电商,想做新零售电商就用它吧!
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
392. 判断子序列
近日小结(非技术文)
随机推荐
gorm 之数据插入(转)
奇妙秘境 码蹄集
Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Unittest框架中引入TestFixture
MySQL 5 installation and modification free
Read excel table data
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
Install Trinity and solve error reporting
Learning projects are self-made, and growth opportunities are self created
自主工业软件的创新与发展
Assertion of unittest framework
mac redis安装与使用,连接远程服务器 redis
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
读取 Excel 表数据
Fisher信息量检测对抗样本代码详解
Can mortgage with housing exclude compulsory execution
Gorm read / write separation (rotation)