降低位图质量会产生OutOfMemoryException

时间:2012-07-06 19:18:28

标签: c# networking graphics bitmap

我正在尝试将用户的整个桌面捕获为图像。我通过以下方式执行此操作:

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

但是,如果我尝试将PixelFormatFormat32bppArgb更改为Format16bppArgb1555,则会产生OutOfMemoryException,我不太明白,考虑到我已经降低了质量。

有什么想法吗?或者我如何降低此图像的质量(因为它将以非常频繁的间隔通过网络发送)

1 个答案:

答案 0 :(得分:7)

来自docs :(也related

  

如果图像有,则此[FromImage()]方法也会抛出异常   以下任何像素格式。

     
      
  • 未定义
  •   
  • DontCare
  •   
  • Format16bppArgb1555
  •   
  • Format16bppGrayScale
  •   

在"相关"链接,msdn继续说:

  

这里唯一的问题是OutOfMessageException在这种情况下不是非常准确的异常类型。我们将考虑在VS2010之后的版本中修复它。