C#截图操作屏幕不完整/多屏幕共同截图解决

1截图不完整问题

1.1问题分析

使用C#如下代码进行截图

Bitmap cBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(cBmp);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));

会发现截取出的图片左上角(Top、Left)是正确的,但是右下角(Size)却比屏幕小

图1为截图结果(1707*960),图2为完整屏幕(2560*1440)

对比不难发现,图1的长和高只有图2的1/1.5,也就是截图出来的大小,是经过window缩放的。

即代码:Screen.AllScreens[0].Bounds.Width,Screen.AllScreens[0].Bounds.Height得到的值是缩放后的,但是g.CopyFromScreen得到的图片是未经过缩放。导致最终Bitmap不是完整的,

g.CopyFromScreen()中Size大小也不是完整的

1.2解决方案

首先我们需要得的屏幕的原始分辨率和缩放比率

        public const int HORZRES = 8;
        public const int VERTRES = 10;
        public const int LOGPIXELSX = 88;
        public const int LOGPIXELSY = 90;
        public const int DESKTOPVERTRES = 117;
        public const int DESKTOPHORZRES = 118;

        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(IntPtr hdc, int nIndex);// index of capability

        public void GetScreen()
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            int PhyW = GetDeviceCaps(hdc, DESKTOPHORZRES); //得到物理分辨率
            int PhyH = GetDeviceCaps(hdc, DESKTOPVERTRES);


            int LogicW = GetDeviceCaps(hdc, HORZRES); //得到逻辑分辨率(经过缩放后)
            int LogicH = GetDeviceCaps(hdc, VERTRES);

            Bitmap cBmp = new Bitmap(PhyW, PhyH);
            Graphics g = Graphics.FromImage(cBmp);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(PhyW, PhyH));
            cBmp.Save("D:/1111.bmp");
        }

使用如上代码,将逻辑分辨率全部替换为物理分辨率即可解决

图3 成功的单个屏幕截图

2多个屏幕截图问题

2.1问题分析

2.1.1多屏幕

当使用上文所给出的代码时,如果拥有多个屏幕,则仅仅会截图第一个屏幕,第二个屏幕内容被忽略

使用PrintScreen按键让windows截图,可以得到一张两个屏幕都在的截图

这个截图,长和高分别为

SystemInformation.VirtualScreen.Width

SystemInformation.VirtualScreen.Height

当然,这两个取值也是逻辑分辨率,实际截图仍然需要得到物理分辨率

可以通过

public int OverNum(float Num)//用于将小数转化为较大相邻的整数
        {
            if (Num % 1 == 0)
            {
                return (int)Num;
            }
            else
            {
                return (int)Num + 1;
            }
        }


F(X):
 float ScrScale = (float)GetDeviceCaps(hdc, 118) / (float)GetDeviceCaps(hdc, 8);//得到缩放比率
 return OverNum(SystemInformation.VirtualScreen.Height * ScrScale);//得到原始分辨率

将上述分辨率带入,可以得到一张全屏幕的截图

但是会发现上半部分会缺失一部分

2.1.2屏幕相对位置

得到的图片是这样的,缺失了第二个屏幕上半部分

问题就出在屏幕相对位置上,Windows将主屏幕左上角定义为(0,0)

而g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(X1, Y1));的第一个Point会让截图从(0,0)开始,所以需要修正这个初始位置。

2.2解决方案

 public int OverNum(float Num)    //获取小数相邻较大整数(修正Scale乘以分辨率的误差)
        {
            if (Num % 1 == 0)
            {
                return (int)Num;
            }
            else
            {
                return (int)Num + 1;
            }
        }

        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(IntPtr hdc,int nIndex);// index of capability
        public float GetDisplay(int Type)           //得到显示器的各种参数
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            if (Type == -1)
            {
                return Screen.AllScreens.Length;    //有几个显示器
            }
            else if(Type == 0)
            {
                return (float)GetDeviceCaps(hdc, 118) / (float)GetDeviceCaps(hdc, 8); //缩放
            }
            else if(Type == 1)
            {
                return SystemInformation.VirtualScreen.Width;     //虚拟屏幕大小(缩放)
            }
            else if(Type == 2)
            {
                return SystemInformation.VirtualScreen.Height;    //虚拟屏幕大小(缩放)
            }
            else if(Type == 3)
            {
                float ScrScale = (float)GetDeviceCaps(hdc, 118) / (float)GetDeviceCaps(hdc, 8);
                return OverNum(SystemInformation.VirtualScreen.Width * ScrScale);//物理大小
            }
            else if(Type == 4)
            {
                float ScrScale = (float)GetDeviceCaps(hdc, 118) / (float)GetDeviceCaps(hdc, 8);
                return OverNum(SystemInformation.VirtualScreen.Height * ScrScale);
            }
            return 0;
        }

        public Bitmap ScreenShot(int X0 = 0, int Y0 = 0, int X1 = -1, int Y1 =-1)
        {
            int MostTop = 0, MostLeft = 0;
            for(int i = 0; i < GetDisplay(-1);i++)
            {
                MostTop = (MostTop < Screen.AllScreens[i].Bounds.Top ? MostTop : Screen.AllScreens[i].Bounds.Top);
                MostLeft = (MostLeft < Screen.AllScreens[i].Bounds.Left ? MostLeft : Screen.AllScreens[i].Bounds.Left);
            }
            MostTop = OverNum(MostTop * GetDisplay(0));    //得到所有屏幕的最Top、Left,设置为起始点
            MostLeft = OverNum(MostLeft * GetDisplay(0));

           if(X1 == -1)
            {
                X1 = (int)GetDisplay(3);
            }
            if (Y1 == -1)
            {
                Y1 = (int)GetDisplay(4);
            }

            Bitmap catchBmp = new Bitmap(X1-X0, Y1-Y0);
            Graphics g = Graphics.FromImage(catchBmp);
            g.CopyFromScreen(new Point(MostLeft + X0, MostTop + Y0), new Point(0, 0), new Size(X1-X0, Y1-Y0));
            return catchBmp;
            
        }

通过以上代码,可以实现多个屏幕的完整截图,截图效果跟PrintScreen几乎一致。

TODO:目前这个截图需要多个屏幕的缩放比率都是一样的,如果不一样还是会出Bug!

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将多个屏幕的截屏组合成一张完整的截屏,可以使用 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、付费专栏及课程。

余额充值