2015-06-28 63 views
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace Player_Manager 
{ 
    public partial class ScreenShotsPlayer : Form 
    { 
     FileInfo[] images; 
     DirectoryInfo di1; 
     int current = 0; 

     public ScreenShotsPlayer() 
     { 
      InitializeComponent(); 

      di1 = new DirectoryInfo(@"e:\screenshots"); 
      images = di1.GetFiles("*.bmp"); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      timer1.Start(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      current = (current >= images.Length - 1) ? 0 : ++current; 
      pictureBox1.Image = new Bitmap(images[current].FullName); 
      pictureBox1.Refresh(); 
     } 
    } 
} 

硬盤上有1200張圖像,定時器設置爲100ms。 經過約258-270播放的圖像它拋出內存不足異常就行了:當試圖在pictureBox中播放圖像時爲什麼會出現內存不足異常?

pictureBox1.Refresh(); 

System.OutOfMemoryException was unhandled 
    HResult=-2147024882 
    Message=Out of memory. 
    Source=System.Drawing 
    StackTrace: 
     at System.Drawing.Graphics.CheckErrorStatus(Int32 status) 
     at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) 
     at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect) 
     at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) 
     at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) 
     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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    InnerException: 

如果我將刪除線pictureBox1.Refresh(); 然後將拋出異常:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Player_Manager 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 

上線Application.Run(新Form1的());

同樣的例外。

+1

這不是「C」問題。這是一個'C#'問題。 – cubrr

+0

不要對明顯非C語言使用C標記。提示:「Python」和「Pascal」都以「T」開頭,但不會用Pascal標籤標記Python問題。 – Olaf

+0

是的我的錯誤對不起,我的意思是把它標記爲c#不是c –

回答

1

那麼與GDI +的事情是,它使用的內存不是管理 - GC不知道你是多少浪費來觸發集合,所以它只是堆積如山,直到GDI +用完可分配記憶。

該修復非常簡單。您知道您正在分配的位圖,只需在不再需要時處理它們即可:

private void timer1_Tick(object sender, EventArgs e) 
    { 
     if(pictureBox1.Image!=null) 
     { 
      var img = pictureBox1.Image; // cache it to dispose 
      pictureBox1.Image = null;  // don't dispose an used object 
      img.Dispose();    // and dispose of it 
     } 

     current = (current >= images.Length - 1) ? 0 : ++current; 
     pictureBox1.Image = new Bitmap(images[current].FullName); 
     pictureBox1.Refresh(); 
    } 
+0

爲什麼你「緩存它來處理」,但你從來沒有對* var img *做任何事情? –

相關問題