我遇到了一个小问题,我似乎无法找到答案。我有一个应用程序可以获取某些进程并获取它的窗口大小。唯一的问题是它占用了实际屏幕尺寸(用户看到的)的百分比。
我想制作应用程序的屏幕截图,但如果我使用窗口的 Rect,我会得到比实际更小的屏幕,因为分辨率为 125%。这意味着我输出的原始分辨率 ( 1280 * 800
) 小于我的屏幕分辨率 ( 1600 * 1000
) 你可以理解这个小问题使我的程序不可靠。我的问题是如何解决这个问题?
我创建了一个清单,并将其设置DPIAware
为 true。我还在调试中禁用了 Visual Studio 托管。但这无济于事。我仍然得到相同的值和相同的问题。这是我实际截屏的代码片段:
RECT Rect = new RECT();
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("Process");
ShowWindow(p[0].MainWindowHandle, 9);
SetForegroundWindow(p[0].MainWindowHandle);
if (GetWindowRect(p[0].MainWindowHandle, ref Rect))
{
var bmp = new Bitmap(Rect.Width, Rect.Height);
var graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(Rect.Left, Rect.Top, 0, 0, new Size(Rect.Width, Rect.Height), CopyPixelOperation.SourceCopy);
bmp.Save(@"C:\Screenshots\temp1.png");
}
这给了我一个截图1280 * 800
,不足以涵盖整个过程,即1600 * 1000
. 一切都关闭了,因为屏幕坐标不正确。如果我将所有内容乘以 1,25 就可以了,但这不是解决方案,因为我不知道其他 PC 上的 DPI 设置是什么。
编辑 3:
我将发布带有 RECT 的完整代码。
RECT Rect = new RECT();
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("LoLPatcherUx");
ShowWindow(p[0].MainWindowHandle, 9);
SetForegroundWindow(p[0].MainWindowHandle);
if (GetWindowRect(p[0].MainWindowHandle, ref Rect))
{
int processH = Rect.Bottom - Rect.Top;
int processW = Rect.Right - Rect.Left;
float processWidth;
float processHeight;
SizeF dpi = GetCurrentDpi();
// Calc the scale.
SizeF scale = new SizeF()
{
Width = dpi.Width / 96f,
Height = dpi.Height / 96f
};
// Scale the rectangle.
processWidth = Rect.Width * scale.Width;
processHeight = Rect.Height * scale.Height;
var bmp = new Bitmap(lolPatcherBreedte, lolPatcherHoogte);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(Rect.Left, Rect.Top, 0, 0, new Size(processW, processH));
bmp.Save(@"C:\Screenshots\temp1.png");
}
public struct RECT
{
private int _Left;
private int _Top;
private int _Right;
private int _Bottom;
public RECT(RECT Rectangle)
: this(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom) { }
public RECT(int Left, int Top, int Right, int Bottom)
{
_Left = Left;
_Top = Top;
_Right = Right;
_Bottom = Bottom;
}
public int X
{
get { return _Left; }
set { _Left = value; }
}
public int Y
{
get { return _Top; }
set { _Top = value; }
}
public int Left
{
get { return _Left; }
set { _Left = value; }
}
public int Top
{
get { return _Top; }
set { _Top = value; }
}
public int Right
{
get { return _Right; }
set { _Right = value; }
}
public int Bottom
{
get { return _Bottom; }
set { _Bottom = value; }
}
public int Height
{
get { return _Bottom - _Top; }
set { _Bottom = value + _Top; }
}
public int Width
{
get { return _Right - _Left; }
set { _Right = value + _Left; }
}
public Point Location
{
get { return new Point(Left, Top); }
set
{
_Left = value.X;
_Top = value.Y;
}
}
public Size Size
{
get { return new Size(Width, Height); }
set
{
_Right = value.Width + _Left;
_Bottom = value.Height + _Top;
}
}
public static implicit operator Rectangle(RECT Rectangle)
{
return new Rectangle(Rectangle.Left, Rectangle.Top, Rectangle.Width, Rectangle.Height);
}
public static implicit operator RECT(Rectangle Rectangle)
{
return new RECT(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom);
}
public static bool operator ==(RECT Rectangle1, RECT Rectangle2)
{
return Rectangle1.Equals(Rectangle2);
}
public static bool operator !=(RECT Rectangle1, RECT Rectangle2)
{
return !Rectangle1.Equals(Rectangle2);
}
public override string ToString()
{
return "{Left: " + _Left + "; " + "Top: " + _Top + "; Right: " + _Right + "; Bottom: " + _Bottom + "}";
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public bool Equals(RECT Rectangle)
{
return Rectangle.Left == _Left && Rectangle.Top == _Top && Rectangle.Right == _Right && Rectangle.Bottom == _Bottom;
}
public override bool Equals(object Object)
{
if (Object is RECT)
{
return Equals((RECT)Object);
}
else if (Object is Rectangle)
{
return Equals(new RECT((Rectangle)Object));
}
return false;
}
}