2012-07-06 95 views
2

我試圖捕獲用戶的整個桌面作爲圖像。我這樣做以下列方式:降低位圖質量產生OutOfMemoryException

 public Bitmap CaptureScreen() 
    { 
     // Set up a bitmap of the correct size 
     Bitmap CapturedImage = new Bitmap((int)SystemInformation.VirtualScreen.Width, 
      (int)SystemInformation.VirtualScreen.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

     // Create a graphics object from it 
     System.Drawing.Size size = new System.Drawing.Size((int)SystemInformation.VirtualScreen.Width, (int)SystemInformation.VirtualScreen.Height); 

     using (Graphics g = Graphics.FromImage(CapturedImage)) 
     { 
      // copy the entire screen to the bitmap 
      g.CopyFromScreen(0, 0, 0, 0, 
       size, CopyPixelOperation.SourceCopy); 
     } 
     return CapturedImage; 
    } 

但是,如果我嘗試從Format32bppArgbPixelFormat更改爲Format16bppArgb1555,它產生的OutOfMemoryException,我真的不明白,考慮到我已經降低了質量。

任何想法?或者我如何能降低這種圖像的質量(如將處於相當頻繁地通過網絡發送)

+0

* *是否發生OOM?我會想象它是在轉換過程中,而不是最終結果。 – 2012-07-06 19:21:02

+0

它發生在線 '使用(圖形g = Graphics.FromImage(CapturedImage))' – 2012-07-06 19:26:35

回答

7

docs:(也related

這[FromImage()]方法也拋出如果圖像有 任何以下像素格式,則爲例外。

  • 未定義
  • dontCare項
  • Format16bppArgb1555
  • Format16bppGrayScale

在 「相關」 鏈接,MSDN接着說:

唯一的問題這是OutOfMessag在這種情況下eException並不是非常準確的異常類型。我們將考慮在VS2010之後的版本中修復它。

+0

嗯,有趣的是,它只有那四個。我想那就解決了,我只需要使用不同的格式。感謝你! – 2012-07-06 19:28:19