Getting Image by ResourceManager GetObject — Call it everytime or store the result?

Each call to GetObject will read the image from the assembly and load it into a Bitmap object.

Calling it many times will create significant overhead; you should store the images.


Just one other thing to point out about calling "ResourceManager.GetObject" each time you need to use a image from Resources is it seems to create a new Windows Handle each time. In your case probably not a big deal but if you were to hold on to them for a while like we did it might cause a issue.

We had a DataGridView that we were pushing images from Resources into different fields of the grid and when that grid got up over 3000 rows we were actually exceeding the maximum allowed Windows handles for a 32bit program.

The error appeared a random Argument Exceptions with message "Parameter is not valid". It took a few hours thinking we had a memory leak but finally found what we loaded this GUI with that grid the applications handles went from 700-1000 to over 10K before it even finished loading and would crash the whole program and could not recover. So I do recommend option 2 here.


I've also implemented the "read once then store in variable" concept in my classes.

To give an example, here is an excerpt from my code:

internal static class MyResourcesHolder
{
    private static Image _i1;
    private static Image _i2;
    private static Image _i3;
    private static Image _i4;
    private static Image _i5;

    public static Image MyImage01 => _i1 ?? (_i1 = Resources.MyImage01);
    public static Image MyImage02 => _i2 ?? (_i2 = Resources.MyImage02);
    public static Image MyImage03 => _i3 ?? (_i3 = Resources.MyImage03);
    public static Image MyImage04 => _i4 ?? (_i4 = Resources.MyImage04);
    public static Image MyImage05 => _i5 ?? (_i5 = Resources.MyImage05);
}

Maybe this helps someone someday.