Dot-Net

.net2.0 與 .net 4.0 gdi+ 的區別?

  • January 22, 2013

我有一個非常奇怪的問題,我有一個應用程序對圖片進行大量涉及 GDI+ 操作。例如裁剪縮放等。我的應用程序在 .net 2.0 中執行良好,但在 .net 4.0 中,我收到使用者報告說它因 gdi+“記憶體不足”錯誤而崩潰。現在我知道“記憶體不足”gdi+ 錯誤是很多錯誤的統稱,但為什麼它會在 .net 2.0 中起作用,而不是在 .net 4 中起作用。

具體來說,我有一個控制項,可以在彼此之上繪製“圖層”以創建組合點陣圖。這個控制項在 .net 2.0 中工作得很好,在 .net 4 中卻不行。

當我從文件系統載入一個 10 兆像素的 jpeg 並且我正在對圖像應用縮放和變換時,就會發生這種情況。

提供更多細節。矩陣比例為 4 的 g.draw 表示任何旋轉都大 400% 將返回此“記憶體不足錯誤”。

它只發生在 xp 盒子上,而不是 Windows 7 盒子上。這裡有什麼區別?

任何接受者…

這是從擷取的異常中記錄的堆棧跟踪的範圍。

 <Event>
   <TimeStamp>11/30/10 11:02:43.706</TimeStamp>
   <Source>APPro2</Source>
   <EventType>Error</EventType>
   <Message><![CDATA[##: OutOfMemoryException
Message:
Out of memory.

Stack Trace:
  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)
  at Colorvision.Graphics.Layers.Picture.DrawBig(Graphics g) in D:\Colorvision_Workspaces\Colorvision\Graphics\Layers\Picture.cs:line 321
  at Colorvision.Graphics.LayerCollection.DrawBig(Graphics e) in D:\Colorvision_Workspaces\Colorvision\Graphics\LayerCollection.cs:line 690]]></Message>
   <EventID>0</EventID>
 </Event>

感謝您的時間。要溫柔,因為這是我在這裡的第一個問題。

0xa3 我目前沒有堆棧跟踪,但確切的 g.draw 呼叫如下:

g.DrawImage(
   bmpBigPicture,
   new Rectangle(
       destBigX,
       destBigY,
       (int)(destBigWidth*Scale),
       (int)(destBigHeight*Scale)),
   0,
   0,
   bmpBigPicture.Width,
   bmpBigPicture.Height,
   GraphicsUnit.Pixel,
   imgAttribs
); 

其中 1s 4 為 400%

我遇到了類似的問題。就我而言,問題是 LOH 碎片。也許這會有所幫助:Large Object Heap Fragmentation

基本上你新人肯定知道記憶體是如何分配的。有時你會處理大量數據而僥倖,有時你的應用程序會失敗。如果您的程序執行很長時間並處理大量數據,則更有可能出現此問題。您提到了 10 兆像素的圖像 - 如果您對此類文件進行大量處理,則很容易遇到 LOH 問題。

引用自:https://stackoverflow.com/questions/4318955