C# 解决';内存不足';调用DrawImage时发生异常

C# 解决';内存不足';调用DrawImage时发生异常,c#,.net,memory-management,system.drawing,C#,.net,Memory Management,System.drawing,大约有1%的用户在使用我们的应用程序时遇到突然崩溃。下面的日志显示了一个例外,到目前为止,我看到的唯一共同点是,它们都有xpsp3 提前谢谢 Out of memory. at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32

大约有1%的用户在使用我们的应用程序时遇到突然崩溃。下面的日志显示了一个例外,到目前为止,我看到的唯一共同点是,它们都有xpsp3

提前谢谢

Out of memory. at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr) at System.Windows.Forms.ControlPaint.DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle) at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent) at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle) at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent) at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle) at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) Operation System Information ---------------------------- Name = Windows XP Edition = Home Service Pack = Service Pack 3 Version = 5.1.2600.196608 Bits = 32 内存不足。 at系统.绘图.图形.检查错误状态(Int32状态) 在System.Drawing.Graphics.DrawImage(图像图像、矩形析取、Int32 srcX、Int32 srcY、Int32 srcWidth、Int32 srcHeight、GraphicsUnit、ImageAttributes imageAttrs、DrawImageAbort回调、IntPtr回调数据) 在System.Drawing.Graphics.DrawImage(图像图像、矩形析取、Int32 srcX、Int32 srcY、Int32 srcWidth、Int32 srcHeight、GraphicsUnit、ImageAttributes imageAttr、DrawImageAbort回调) 在System.Drawing.Graphics.DrawImage(图像图像、矩形析取、Int32 srcX、Int32 srcY、Int32 srcWidth、Int32 srcHeight、GraphicsUnit、ImageAttributes imageAttr) 在System.Windows.Forms.ControlPaint.DruckGroundImage(图形g、图像背景图像、颜色背景色、图像布局背景图像布局、矩形边界、矩形clipRect、点滚动偏移、右至左至右至左) 在System.Windows.Forms.Control.PaintBackground(PaintEventArgs e、矩形、彩色背景、点滚动偏移) 位于System.Windows.Forms.Control.PaintBackground(PaintEventArgs e,矩形) 位于System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs-pevent) 位于System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) 位于System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e、矩形、区域透明区域) 在System.Windows.Forms.Control.PaintBackground(PaintEventArgs e、矩形、彩色背景、点滚动偏移) 位于System.Windows.Forms.Control.PaintBackground(PaintEventArgs e,矩形) 位于System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs-pevent) 位于System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) 位于System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e、矩形、区域透明区域) 在System.Windows.Forms.Control.PaintBackground(PaintEventArgs e、矩形、彩色背景、点滚动偏移) 位于System.Windows.Forms.Control.PaintBackground(PaintEventArgs e,矩形) 位于System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs-pevent) 在System.Windows.Forms.Control.PaintEventArgs处理中(PaintEventArgs e、Int16层、Boolean disposeEventArgs) 在System.Windows.Forms.Control.WmPaint(Message&m)中 位于System.Windows.Forms.Control.WndProc(Message&m) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中 在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)中 操作系统信息 ---------------------------- 名称=Windows XP 版本=主页 服务包=服务包3 版本=5.1.2600.196608 位=32
我相信你现在已经弄明白了,但万一你还没弄明白,我们会遇到同样的问题。这是GDI+中的一个bug,可以作为一种解决方法。不幸的是,我不知道为什么,但对于这类问题,我更关心的是它是固定的,而不是它为什么是固定的

显然,该调用会调整PixelFormat并将图像置于imagelist使用DrawImage可以处理的格式

发件人:

是不是正在绘制的一张非常大的图像会合法地导致内存不足异常?@Jacob-hmm,我们有一个地方可以拍摄用户桌面的屏幕截图并显示给他。现在让我们假设他有1920x1080x32位的分辨率,这意味着需要8mb的内存,对吗?8MB是否超出预期?这取决于在此8MB之前已经占用了多少内存。我敢打赌“内存不足”错误是一种误导。它可能只是得到一些一般性错误,并假设这是由于内存不足。无论你遇到什么问题,都不是因为你的内存不足。不幸的是,我相信问题在于GDI+没有详细的错误代码,所以.Net只是将错误转换为“内存不足”。实际上我无法解决它,因为它每月发生一次。。但我肯定会尝试一下,如果不会再发生这种情况,我会将其标记为解决方案。感谢您,GetThumbNailImage使用嵌入的exif数据(或者如果缺少双线性缩放),图像质量非常低。我们需要一个更好的解决方案。不幸的是,这在我的例子中不起作用-GetThumbnailImage()现在抛出OutOfMemoryException。我将DrawImage()与ImageAttributes类变量一起使用。
image = image.GetThumbnailImage(image.Width, image.Height, null, IntPtr.Zero);