c#屏幕截屏、抓屏

       用c#进行截屏时,由于用户可能在显示设置内调整缩放比例,比如“更改文本、应用等项目的大小”到150%。这时,用传统的代码抓屏时就会出现显示区域不全的问题。
       原因在于当前显示器的“屏幕分辨率当前物理大小”和屏幕显示大小不一致,对应的获取方法分别如下:


        /// <summary>
        /// 获取真实设置的桌面分辨率大小
        /// </summary>
        public static Size DESKTOP
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Height = GetDeviceCaps(hdc, 117);
                size.Width = GetDeviceCaps(hdc, 118);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }
        #region Win32 API
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex // index of capability
        );        
        #endregion

        //获取屏幕显示大小
        Rectangle tScreenRect = Screen.PrimaryScreen.Bounds;


        当两者不相同时,需要先按照物理尺寸截屏,然后进行缩放到显示尺寸,代码如下:


        /// <summary>
        /// 抓屏
        /// </summary>
        /// <returns></returns>
        private Bitmap GetScreenImage()
        {           
            //获取屏幕显示大小
            Rectangle viewRect = Screen.PrimaryScreen.Bounds;
            Size phisicalRect = DESKTOP;
            Bitmap tSrcBmp = new Bitmap(phisicalRect.Width, phisicalRect.Height); // 用于屏幕原始图片保存
            Graphics gp = Graphics.FromImage(tSrcBmp);
            gp.CopyFromScreen(0, 0, 0, 0, phisicalRect);

            if (viewRect.Size != phisicalRect)
            {
                //当两者不相同时进行缩放
                Bitmap tSrcBmp2 = new Bitmap(viewRect.Width, viewRect.Height);
                Graphics gp2 = Graphics.FromImage(tSrcBmp2);
                gp2.DrawImage(tSrcBmp, new Rectangle(0, 0, viewRect.Width, viewRect.Height), 0, 0, phisicalRect.Width, phisicalRect.Height, GraphicsUnit.Pixel);
                return tSrcBmp2 as Bitmap;
            }
            else {
                return tSrcBmp;
            }
    }

要将多个屏幕截屏组合成一张完整的截屏,可以使用 System.Drawing 命名空间中的 Graphics 类来实现。下面是一个示例代码,可以实现将多个屏幕截屏拼接成一张完整的截屏: ```csharp using System; using System.Drawing; using System.Windows.Forms; class MultipleScreenShot { static void Main(string[] args) { // 获取所有屏幕的数量 int screenCount = Screen.AllScreens.Length; // 计算所有屏幕的总宽度和高度 int totalWidth = 0; int totalHeight = 0; for (int i = 0; i < screenCount; i++) { Screen screen = Screen.AllScreens[i]; totalWidth += screen.Bounds.Width; totalHeight = Math.Max(totalHeight, screen.Bounds.Height); } // 创建一个与所有屏幕尺寸相同的图片对象 Bitmap bitmap = new Bitmap(totalWidth, totalHeight); // 创建一个图形对象 Graphics graphics = Graphics.FromImage(bitmap); // 计算当前屏幕的左上角坐标 int x = 0; int y = 0; // 将所有屏幕的内容绘制到图片对象上 for (int i = 0; i < screenCount; i++) { Screen screen = Screen.AllScreens[i]; graphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, x, y, screen.Bounds.Size); x += screen.Bounds.Width; } // 保存图片 bitmap.Save("screenshot.png"); // 释放资源 graphics.Dispose(); bitmap.Dispose(); } } ``` 以上示例代码中,我们首先计算所有屏幕的总宽度和高度,然后创建一个与所有屏幕尺寸相同的图片对象。接着,我们使用一个 for 循环遍历所有的屏幕,逐个截取屏幕内容,并将其绘制到图片对象上。最后,保存图片并释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值