BackgroundImage: Slow Redraw for runtime loaded bitmaps

J

jgraham

I have an application which configures the user interface (UI) at
runtime, meaning, the images that make up the UI are loaded based on
the current configuration when the application starts. The UI is
populated with normal controls (buttons, lists, etc.) but some controls
can load a bitmap to represent them. For instance, each button in the
window loads a bitmap as it's BackgroundImage. Also, the
BackgroundImage of each dialog is also specified so as to embed an
image as the background for the dialog. I am loading the bitmaps at
startup as follows:

this.BackgroundImage = new Bitmap(imagefile); // a
System.Windows.Forms.Form
or
this.mybutton.BackgroundImage = new Bitmap(imagefile); // a button

My problem comes when the screen is refreshed. For instance, when I
open a new dialog and then close it and return to the main window, the
main window refreshes very slowly. I am not overriding the Paint
method in the main window. The machine is also not underpowered as it
is a 3.2Ghz with 2GB of memory. The images themselves are either BMP
or GIF files and they are not large (e.g. my 800x600 window background
image is 1,407KB in BMP format and 21KB in GIF). Each window only
contains a few (~10) controls and the typical button file size is
around 3KB for the GIF files. The images are also not reloaded each
time a new dialog is opened; they are loaded only at startup.

Can someone help me improve the refresh performance under these
circumstances?

Thanks!
 
R

raj

The problem may be that you are reloading the bitmap each time
i.e. if you have a constructor that draws the bitmap on a control
and the constructor loads them image
then everytime there is a refresh to window the control will be redrawn thus
it will call the constuctor and the constructor will reload the bitmap.

i would suggest you load the bitmaps to an array at the very first run and
save
them.

i have done this in the past

//private variable
ArrayList myList = new ArrayList();

string[] myBitmaps = new string[ {" path1.bmp", "path2.bmp"}

somefunction()
{
Bitmap b = null;
foreach(string s in myBitmaps)
{

b = new Bitmap.FromFile(s);
myList.Add(b);
}

to assign a bitmap

mypicbox.Bitmap = (Bitmap)myList[0];


hth
 
J

jgraham

raj said:
The problem may be that you are reloading the bitmap each time
i.e. if you have a constructor that draws the bitmap on a control
and the constructor loads them image
then everytime there is a refresh to window the control will be redrawn thus
it will call the constuctor and the constructor will reload the bitmap.

i would suggest you load the bitmaps to an array at the very first run and
save
them.

thanks for the response. however, i'm a bit confused (and i'm also new
to c# so maybe that helps explain why). i load the bitmaps in the
InitializeComponent() method which i assume is only run once, at
startup or when the object is instanciated. i understand the example
you provided, i just do not see how it differs from performing the
bitmap load when each control is created (which i assume is a one time
deal). Or, am i missing something?

jamey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top