2013-03-10 103 views
1

我正在使用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窗体的捕捉,这将更好地清晰和突出。任何想法。谢谢。

回答

1

缩放图片和任何缩放 - 无论是缩放还是缩放 - 都会导致质量较差的图片。不是缩放图像,而是获取窗口的宽度和高度,创建一个具有该尺寸的新位图,最后绘制尺寸相同的图像。

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); 
    } 
} 

接近上面的东西 - 我还没有真正能够测试它。

+1

它完美的作品 – Thomas 2013-03-10 18:25:04