获取指定的桌面截图

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

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

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

方式一:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <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);
复制代码

 

 

posted on   TanZhiWei  阅读(90)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示