2013-11-27 112 views
1

我正在嘗試在定時器的滴答功能中將位圖圖像添加到List<Image>。計時器有100ms的刻度,並使用下面的代碼:將屏幕上的區域添加到圖像列表中

private void GifTimer_Tick(object sender, EventArgs e) 
{ 
    using (var bmp = new Bitmap(selection.Width, selection.Height, PixelFormat.Format32bppPArgb)) 
    { 
     using (var graphics = Graphics.FromImage(bmp)) graphics.CopyFromScreen(selection.Left, selection.Top, 0, 0, bmp.Size); 
     images.Add(bmp); //Adds null values apparently. 
    } 
} 

我跑了一些斷點調試,發現bmp不爲空,且具有正確的寬度和高度,根據該選擇。我在其他地方使用相同的代碼來實現其他目的,並且按預期工作。但是,當這個位圖添加到我的列表中時,它將返回null。

我在這裏錯過了什麼嗎?該列表在我的構造函數中初始化爲new List<Image>();

回答

0

不要使用using自動處理位圖:

var bmp = new Bitmap(selection.Width, selection.Height, PixelFormat.Format32bppPArgb); 
using (var graphics = Graphics.FromImage(bmp)) 
    graphics.CopyFromScreen(selection.Left, selection.Top, 0, 0, bmp.Size); 
images.Add(bmp); 
+1

優秀,非常感謝你! – Dragonphase

+0

@Dragonphase不客氣:) –

相關問題