Dan*_*iel 3 c# screenshot winforms
我正在尝试捕获屏幕的一部分。问题是,即使我使用 png 保存图像,质量仍然比使用普通打印屏幕差。
这是我使用的代码:
//display a save file dialog for the user to set the file name
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "PNG (*.png)|*.png";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
//if the user proceed saving the picture
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
//simplify code with constant numbers for demo
//get the width of the panel we need the screenshoot off
int x = 10;
//get the height of the panel we need the screenshoot off
int y = 10;
//create the ractangle of the screenshoot panel
Rectangle rect = new Rectangle(x, y, 5, 5);
//create new bitmap
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
//get the screenshoot of the panel
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
string fileName = saveFileDialog.FileName;
if (!fileName.Contains(".png"))
fileName += ".png";
bmp.Save(fileName, ImageFormat.Png);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我用代码拍摄的示例图像:
正常截图:
这里看起来并没有什么不同,但这是最糟糕的。
您问题中的顶部图像已重新缩放,比原始图像小。这在包含精细细节的图像中很明显,例如用于使文本更具可读性的 ClearType 抗锯齿像素。当它们重新缩放时,视觉效果会被破坏,文本看起来会更糟。
完全不清楚为什么图像被重新缩放,您的代码中没有任何内容可能导致这种情况。使用调试器检查 bmp.HorizontalResolution 属性进行仔细检查,它应该与视频适配器的 DPI 匹配。最简单的解释是,它是由您使用的任何图像查看程序完成的,也许是为了使图像适合窗口。尝试缩小。