2012-02-04 69 views
0

我正在使用Winforms,並且需要我的程序的啓動儘可能快。每毫秒都很重要。爲什麼使用ComponentResourceManager設置比使用位圖慢100ms左右的圖像?

我發現了一個有趣的觀察。當使用資源添加圖像(Winform向控件添加圖像的默認方式)來說,按鈕時,與添加直位圖相比,表單加載需要大約100毫秒的時間。比較:

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); 
button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image"))); 

下面的代碼是100毫秒左右更快(速度增益並不適用於其他圖像雖然):

button1.Image = new Bitmap("myimage.png"); 

我想知道,爲什麼前者是如此緩慢,如果我能以某種方式加速它?我寧願使用前者,因爲它將圖片嵌入到exe文件中(並且我不想單獨提供所有圖片)。

該問題適用於所有允許添加圖像的控件(我使用圖片框和按鈕進行測試)。

+0

您是如何測量時差的? – BrokenGlass 2012-02-04 01:58:07

+0

多次運行(體面平均),並使用我以前使用過的可信定時器。 – 2012-02-04 17:57:24

回答

2

嘗試preConstruct System.ComponentModel.ComponentResourceManager資源。 可能額外100ms用於構建ComponentResourceManager對象

+0

你如何去預構造一些東西? – 2014-12-11 16:49:09

相關問題