首页 > 其他分享 >获取指定的桌面截图

获取指定的桌面截图

时间:2022-08-27 18:00:58浏览次数:86  
标签:截图 桌面 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
From: https://www.cnblogs.com/terryK/p/16631060.html

相关文章