Search code examples
c#resourcemanager

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


Let's say that I have to show some graphics on some control. But there will be three images switched based on some condition. Three bitmap is added in the resource file.

So, I retrieve them by calling ResourceManager.GetObject.

The question is that, should it be:

  1. Everytime I have to switch image, I call GetObject to get it and assign to the control or
  2. hold the result of GetObject for each image at the start, so that there will only ever be 3 calls to the GetObject. Assign image from my variables instead.

Doing 1) seems to produce a lot of GC Handle when viewed with CLR Profiler. Hoping to know any bad side effect of 2).

Thanks a lot.


Solution

  • 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.