ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

获取指定的桌面截图

2022-08-27 18:00:48  阅读:150  来源: 互联网

标签:截图 桌面 int 获取 var new imageSource public


记录桌面截图(排除不需要的桌面视图:例如本身截图软件的视图),一位组内优秀帅小伙伴(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);

 

 

标签:截图,桌面,int,获取,var,new,imageSource,public
来源: https://www.cnblogs.com/terryK/p/16631060.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有