CopyFromScreen在屏幕缩放情况下需要做处理

时间:2023-03-09 17:28:48
CopyFromScreen在屏幕缩放情况下需要做处理
using System;
using System.Drawing;
using System.Runtime.InteropServices;
//这段代码转自网上
namespace System.Util
{
public class PrimaryScreen
{
#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
);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
#region DeviceCaps常量
const int HORZRES = ;
const int VERTRES = ;
const int LOGPIXELSX = ;
const int LOGPIXELSY = ;
const int DESKTOPVERTRES = ;
const int DESKTOPHORZRES = ;
#endregion #region 属性
/// <summary>
/// 获取屏幕分辨率当前物理大小
/// </summary>
public static Size WorkingArea
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, HORZRES);
size.Height = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
/// <summary>
/// 当前系统DPI_X 大小 一般为96
/// </summary>
public static int DpiX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
/// <summary>
/// 当前系统DPI_Y 大小 一般为96
/// </summary>
public static int DpiY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
/// <summary>
/// 获取真实设置的桌面分辨率大小
/// </summary>
public static Size DESKTOP
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
} /// <summary>
/// 获取宽度缩放百分比
/// </summary>
public static float ScaleX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
int d = GetDeviceCaps(hdc, HORZRES);
float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
ReleaseDC(IntPtr.Zero, hdc);
return ScaleX;
}
}
/// <summary>
/// 获取高度缩放百分比
/// </summary>
public static float ScaleY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return ScaleY;
}
}
#endregion
}
}
 this.BackColor = Color.Black;
Rectangle rect1 = parentControl.ClientRectangle;
this.SetBounds(rect1.Left, rect1.Top, rect1.Width, rect1.Height);
this.Parent = parentControl;
Double scaleX = PrimaryScreen.ScaleX;
Double scaleY = PrimaryScreen.ScaleY;
if (this.BackgroundImage == null)
{
Rectangle rect = parentControl.ClientRectangle;
Rectangle sRect = parentControl.RectangleToScreen(rect);
Bitmap bit = new Bitmap((int)(sRect.Width* scaleX), (int)(sRect.Height* scaleY));//实例化一个和窗体一样大的bitmap
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
int width = (int)(this.Parent.Parent.Width* scaleX);
int height = (int)(this.Parent.Parent.Width * scaleX);
g.CopyFromScreen((int)(sRect.Left* scaleX), (int)(sRect.Top * scaleY), , , new Size((int)(this.Width * scaleX), (int)(this.Height * scaleY)));
//g.CopyFromScreen(0, 0, 0, 0, new Size((int)(width), (int)(height)));
this.BackgroundImage = bit;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.BringToFront();
}
bShow = true;