2013-03-10 295 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