当前位置:网站首页>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+截屏就相对容易实现了,当然还有截屏框的实现还是略微不容易的。这样的一套截屏功能对功能进行简单的拓展就可以用于正式项目了。
边栏推荐
- 常见 content-type对应表
- Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
- 吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
- Worried about "cutting off gas", Germany is revising the energy security law
- 392. Judgement subsequence
- 奇妙秘境 码蹄集
- Source code compilation and installation of MySQL
- C language programming topic reference
- Whether the loyalty agreement has legal effect
- Interview disassembly: how to check the soaring usage of CPU after the system goes online?
猜你喜欢

【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法

【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对

華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東

sharding key type not supported

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

锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东

Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc

2022年山东省安全员C证考试题库及在线模拟考试

CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...

392. Judgement subsequence
随机推荐
30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)
德明利深交所上市:市值31亿 为李虎与田华夫妻档
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
gorm 之数据插入(转)
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
【R语言数据科学】:交叉验证再回首
基于PaddleX的智能零售柜商品识别
golang fmt.printf()(转)
以房抵债能否排除强制执行
Read excel table data
Blob, text geometry or JSON column'xxx'can't have a default value query question
Idea shortcut keys
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
392. Judgement subsequence
Basic mode of service mesh
1200. 最小绝对差
2022g3 boiler water treatment examination question simulation examination question bank and simulation examination
MongoDB常用28条查询语句(转)
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