views:

33

answers:

2

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.

A: 

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.

SLaks
A: 

The MSDN documentation states that the value of the resource is returned by ResourceManager.GetObject. Since it sounds like the individual bitmaps don't change at run-time, the only down-side I see to approach #2 is that your memory footprint will be a bit bigger.

Zian Choy