屏幕截图指的是截取当前系统桌面的展示的所有内容,截取屏幕可以供我们完成冻结桌面的业务。

具体方法如下:


        /// <summary>
        /// 屏幕截图
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static BitmapSource CaptureScreen(int x, int y, int width, int height)
        {
            var bitmap = new Bitmap(width, height);
            using var graphics = Graphics.FromImage(bitmap);
            graphics.CopyFromScreen(x, y, 0, 0, new Size(width, height));
            //BitmapToBitmapSource方法,请参阅:https://huchengv5.gitee.io/post/Bitmap%E4%B8%8EBitmapSource%E7%9A%84%E4%BA%92%E8%BD%AC.html
            return ImageHelper.BitmapToBitmapSource(bitmap);
        }

        /// <summary>
        /// 以当前dpi放大比例截屏
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static BitmapSource CaptureScreenWithCurrentDpi(int x, int y, int width, int height)
        {
            //GetDpiRatio方法,请参阅:https://huchengv5.gitee.io/post/WPF-%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9FDPI.html
            var ratio = DpiHelper.GetDpiRatio();
            return CaptureScreen(x, y, width / (int)ratio, height / (int)ratio);
        }   


本文会经常更新,请阅读原文: https://huchengv5.gitee.io//post/WPF-%E5%AE%9E%E7%8E%B0%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名胡承(包含链接: https://huchengv5.gitee.io/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系