当前位置:网站首页>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+截屏就相对容易实现了,当然还有截屏框的实现还是略微不容易的。这样的一套截屏功能对功能进行简单的拓展就可以用于正式项目了。
边栏推荐
- [C question set] of VII
- Interview disassembly: how to check the soaring usage of CPU after the system goes online?
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
- 面试拆解:系统上线后Cpu使用率飙升如何排查?
- 1200. 最小绝对差
- Understanding and difference between viewbinding and databinding
- 【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
- 做事的真正意义和目的,真正想得到什么
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
- Unity Shader学习(三)试着绘制一个圆
猜你喜欢

吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行

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

国内酒店交易DDD应用与实践——代码篇

硬件基础知识-二极管基础

Unity shader learning (3) try to draw a circle

392. 判断子序列

Dgraph: large scale dynamic graph dataset
![[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions](/img/43/1a9786c89a5ab21d1fb8903cb7b77e.png)
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions

自主工业软件的创新与发展

Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
随机推荐
Programmer anxiety
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
1200. 最小绝对差
Install Trinity and solve error reporting
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
如何在 2022 年为 Web 应用程序选择技术堆栈
硬件基础知识-二极管基础
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
Interviewer: what is the internal implementation of hash data type in redis?
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
奇妙秘境 码蹄集
Blob, text geometry or JSON column'xxx'can't have a default value query question
安装Mysql
Read excel table data
golang fmt.printf()(转)
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers