当前位置:网站首页>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+截屏就相对容易实现了,当然还有截屏框的实现还是略微不容易的。这样的一套截屏功能对功能进行简单的拓展就可以用于正式项目了。
边栏推荐
- 锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
- Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
- Detailed explanation of Fisher information quantity detection countermeasure sample code
- BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
- Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
- Lick the dog until the last one has nothing (state machine)
- 硬件基础知识-二极管基础
- Summary of recent days (non-technical article)
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- Understanding and difference between viewbinding and databinding
猜你喜欢

DGraph: 大规模动态图数据集

Interview disassembly: how to check the soaring usage of CPU after the system goes online?
![[R language data science]: cross validation and looking back](/img/a8/84a5685ebcb12d3cf8e32e1fbac053.png)
[R language data science]: cross validation and looking back

2022g3 boiler water treatment examination question simulation examination question bank and simulation examination

小程序直播 + 电商,想做新零售电商就用它吧!

基于PaddleX的智能零售柜商品识别

Byte interview algorithm question

Understanding and difference between viewbinding and databinding

30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)

Test evaluation of software testing
随机推荐
MongoDB常用28条查询语句(转)
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
Mongodb commonly used 28 query statements (forward)
392. 判断子序列
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
MySQL version 8 installation Free Tutorial
常见 content-type对应表
【C 题集】of Ⅶ
安装Mysql
Read excel table data
做事的真正意义和目的,真正想得到什么
Introducing testfixture into unittest framework
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
qt 怎么检测鼠标在不在某个控件上
2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
Unittest框架之断言