使用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大小也不是完整的
首先我们需要得的屏幕的原始分辨率和缩放比率
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 成功的单个屏幕截图
当使用上文所给出的代码时,如果拥有多个屏幕,则仅仅会截图第一个屏幕,第二个屏幕内容被忽略
使用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);//得到原始分辨率
将上述分辨率带入,可以得到一张全屏幕的截图
但是会发现上半部分会缺失一部分
得到的图片是这样的,缺失了第二个屏幕上半部分
问题就出在屏幕相对位置上,Windows将主屏幕左上角定义为(0,0)
而g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(X1, Y1));的第一个Point会让截图从(0,0)开始,所以需要修正这个初始位置。
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几乎一致。