为什么使用ComponentResourceManager将图像设置为比使用位图慢100ms左右

本文关键字:位图 左右 100ms 设置 ComponentResourceManager 图像 为什么 | 更新日期: 2023-09-27 18:22:44

我正在使用Winforms,需要尽可能快地启动程序。每毫秒都很重要。

我发现了一个有趣的观察结果。当使用资源(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中(我不想单独提供所有图像)。

这个问题可能适用于所有允许添加图像的控件(我用图片框和按钮进行了测试)。

为什么使用ComponentResourceManager将图像设置为比使用位图慢100ms左右

尝试预构造System.ComponentModel.ComponentResourceManager资源。可能额外的100ms用于构建ComponentResourceManager对象