想要捕获最小化窗口的屏幕截图
本文关键字:窗口 屏幕截图 最小化 | 更新日期: 2023-09-27 18:03:08
我正在使用MDI应用程序。在最小化任何sdi表单之前,我想捕获它的屏幕截图没有标题栏。我的代码是工作的,但我捕捉图像的方式是不清楚,而有点模糊。我是这样做的。这是我的代码。
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COMMAND && m.WParam.ToInt32() == SC_MINIMIZE)
{
OnMinimize(EventArgs.Empty);
}
base.WndProc(ref m);
}
protected virtual void OnMinimize(EventArgs e)
{
if (_lastSnapshot == null)
{
_lastSnapshot = new Bitmap(100, 100);
}
using (Image windowImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))
using (Graphics windowGraphics = Graphics.FromImage(windowImage))
using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
{
Rectangle r = this.RectangleToScreen(ClientRectangle);
windowGraphics.CopyFromScreen(new Point(r.Left, r.Top), Point.Empty, new Size(r.Width, r.Height));
windowGraphics.Flush();
float scaleX = 1;
float scaleY = 1;
if (ClientRectangle.Width > ClientRectangle.Height)
{
scaleY = (float)ClientRectangle.Height / ClientRectangle.Width;
}
else if (ClientRectangle.Height > ClientRectangle.Width)
{
scaleX = (float)ClientRectangle.Width / ClientRectangle.Height;
}
tipGraphics.DrawImage(windowImage, 0, 0, 100 * scaleX, 100 * scaleY);
}
}
所以请指导我如何获得sdi表单的快照,这将更好地清晰和突出。任何想法。谢谢。
您缩放图片,任何缩放-无论是向上还是向下缩放-都会导致图像质量降低。而不是缩放图像,我将得到窗口的宽度和高度,创建一个新的位图的大小,最后绘制的图像与相同的大小。
protected virtual void OnMinimize(EventArgs e)
{
Rectangle r = this.RectangleToScreen(ClientRectangle);
if (_lastSnapshot == null)
{
_lastSnapshot = new Bitmap(r.Width, r.Height);
}
using (Image windowImage = new Bitmap(r.Width, r.Height))
using (Graphics windowGraphics = Graphics.FromImage(windowImage))
using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
{
windowGraphics.CopyFromScreen(new Point(r.Left, r.Top), new Point(0, 0), new Size(r.Width, r.Height));
windowGraphics.Flush();
tipGraphics.DrawImage(windowImage, 0, 0, r.Width, r.Height);
}
}
或类似于上面的东西-我实际上还没有能够测试它