获取指定的桌面截图

记录桌面截图(排除不需要的桌面视图:例如本身截图软件的视图),一位组内优秀帅小伙伴(https://www.cnblogs.com/wuty/ )的截图实现。

两种方式:方式一是在截图的时候,将截图软件隐藏,然后获取桌面截图,最后再显示;方式二在截图前将指定窗口的句柄通过User32设置隐藏,然后获取桌面截图,最后再通过User32指定句柄设置显示。

当然直接显示隐藏,可能体验不太好,有点突兀,此时可以使用动画设置透明度进行过度。

方式一:

        /// <summary>
        /// 隐藏窗体,并执行快照
        /// </summary>
        private void HideWindowSnapShot()
        {
            //获取当前的窗体,设置隐藏当前的窗体进行截图     
            _selfWindow.Opacity = 0.3;
            var animationHide = new DoubleAnimation()
            {
                Duration = TimeSpan.FromSeconds(0.2),
                From = 0.3,
                To = 0,
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
            };
            animationHide.Completed += AnimationHide_Completed;
            _selfWindow.BeginAnimation(UIElement.OpacityProperty, animationHide);
        }

        private void AnimationHide_Completed(object sender, EventArgs e)
        {
            GetCurrentScreenSnap();
            var animationShow = new DoubleAnimation()
            {
                Duration = TimeSpan.FromSeconds(0.4),
                From = 0,
                To = 1,
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
            };
            _selfWindow.BeginAnimation(UIElement.OpacityProperty, animationShow);
        }

 /// <summary>
        /// 截取桌面的一张快照
        /// </summary>
        public async void GetCurrentScreenSnap()
        {
            //获取屏幕截图
            var _scanBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            using var graphics = Graphics.FromImage(_scanBitmap);
            graphics.CopyFromScreen(0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));

            var _scanFile = Path.Combine($"C\\TestScreenSnap\\{DateTime.Now.ToString("yyyyMMddHHmmss")}" + ".png");
            await SavaScreenSnapShotAsync(_scanFile, _scanBitmap);
        }
        /// <summary>
        /// 保存屏幕的截图
        /// </summary>
        /// <param name="filePath"></param>
        public Task SavaScreenSnapShotAsync(string filePath, Bitmap bitmap)
        {
            return Task.Run(() =>
            {
//BitmapToBitmapSource using var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.CacheOption = BitmapCacheOption.OnLoad; imageSource.CreateOptions = BitmapCreateOptions.IgnoreColorProfile; imageSource.StreamSource = ms; imageSource.EndInit(); imageSource.Freeze();
//imageSource转成Png保存到本地 BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(imageSource)); using var stream = new FileStream(filePath, FileMode.Create); encoder.Save(stream); }); }

方式二

private Window _selfWindow;
 /// <summary>
 /// 加载时获取窗体
 /// </summary>
 public ICommand LoadedCommand => new ActionCommand<Window>(window=> { _selfWindow = window ; });
//截图实现
User32.ShowWindow((int)intptr, User32.SW_HIDE); GetCurrentScreenSnap();
User32.ShowWindow((int)intptr, User32.SW_SHOW);

public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
[DllImport("user32.dll")]
public static extern int ShowWindow(int hwnd,int nCmdShow);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值