【问题标题】:LOWERING Bitmap Quality Produces OutOfMemoryException降低位图质量产生 OutOfMemoryException
【发布时间】:2012-07-06 19:18:27
【问题描述】:

我正在尝试将用户的整个桌面捕获为图像。我通过以下方式做到这一点:

        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,考虑到我降低了质量,我不太明白。

有什么想法吗?或者我怎样才能降低这张图片的质量(因为它会以非常频繁的间隔通过网络发送)

【问题讨论】:

  • 何时会发生 OOM?我想这是在转换过程中,而不是最终结果。
  • 发生在using (Graphics g = Graphics.FromImage(CapturedImage))这一行

标签: c# networking graphics bitmap


【解决方案1】:

来自docs:(也是related

如果图像有这个 [FromImage()] 方法也会抛出异常 以下任何一种像素格式。

  • 未定义
  • 不要关心
  • 格式16bppArgb1555
  • Format16bppGrayScale

在“相关”链接中,msdn 继续说:

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

【讨论】:

  • 嗯,有趣的是只有这四个。我想这可以解决这个问题,我只需要使用不同的格式。谢谢!
  • 我真的很想知道为什么Format16bppArgb1555 会包含在该列表中。它可以完美地转换为正常的每组件 1 字节的颜色格式。
猜你喜欢
  • 2019-08-23
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2016-06-06
  • 2010-11-12
  • 2012-11-07
  • 1970-01-01
相关资源
最近更新 更多