当前位置:网站首页>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+截屏就相对容易实现了,当然还有截屏框的实现还是略微不容易的。这样的一套截屏功能对功能进行简单的拓展就可以用于正式项目了。
边栏推荐
- 2022危险化学品经营单位主要负责人练习题及模拟考试
- 好博医疗冲刺科创板:年营收2.6亿 万永钢和沈智群为实控人
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
- 吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
- 392. Judgement subsequence
- Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
- Learning projects are self-made, and growth opportunities are self created
- Worried about "cutting off gas", Germany is revising the energy security law
- go vendor 项目迁移到 mod 项目
- Variable promotion and function promotion in JS
猜你喜欢

Mask wearing detection based on yolov1

205. 同构字符串

【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法

吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器

Dgraph: large scale dynamic graph dataset

Openharmony application development how to create dayu200 previewer
![[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp](/img/eb/11e5da1c5e897c5f6a18d49125925f.png)
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
![[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect](/img/96/379d1692f9d3c05a7af2e938cbc5d7.png)
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect

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

Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
随机推荐
常见 content-type对应表
markdown 语法之字体标红
Source code compilation and installation of MySQL
TestSuite and testrunner in unittest
Install Trinity and solve error reporting
Use the default route as the route to the Internet
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
go语言中的文件创建,写入,读取,删除(转)
MySQL8版本免安装步骤教程
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
Understanding and difference between viewbinding and databinding
PHP log debugging
测试流程整理(3)
2022年山东省安全员C证考试题库及在线模拟考试
392. 判断子序列
华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
Common content type correspondence table
[FAQ] summary of common causes and solutions of Huawei account service error 907135701