【发布时间】:2019-06-28 21:22:10
【问题描述】:
我编写了一个函数,它将获取一个控件和一个文件目标,并将保存控件所覆盖的表单区域。
我的问题是,当我从外接显示器移动到笔记本电脑的主屏幕时,捕获区域的移动量不一致。我终于发现缩放(DPI)是罪魁祸首。当我将其更改为 100% (96 DPI) 时,它可以在笔记本电脑屏幕上运行。所有其他屏幕都已设置为 100%。回到 125%,这只是笔记本电脑屏幕上的问题。如何允许 125%?
在笔记本电脑屏幕上,表格越靠近屏幕左上角,图像的位置就越准确。生成的图像大小在任何屏幕上都是相同的,只是在笔记本电脑屏幕上位置会发生变化。此外,当我从外接显示器转换到笔记本电脑显示器时,表单会调整大小。正是在调整大小之后,我才得到了这个问题。
private void capture(Control ctrl, string fileName)
{
Rectangle bounds = ctrl.Bounds;
Point pt = ctrl.PointToScreen(bounds.Location);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size);
}
string filetype = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
switch (filetype)
{
case ".png":
bitmap.Save(fileName, ImageFormat.Png);
break;
case ".jpeg":
bitmap.Save(fileName, ImageFormat.Jpeg);
break;
case ".bmp":
bitmap.Save(fileName, ImageFormat.Bmp);
break;
default:
break;
}
}
【问题讨论】: