Link to home
Start Free TrialLog in
Avatar of MalcolmPike
MalcolmPike

asked on

Painting a forms background

The time that it takes to paint a forms background is far to long.  This is how I am doing it.
First I load the picture into a Picture Box control that is hidden on the form.  Then using the height and width of the Picture Box I tile the forms background using the appropriate x and y coordinates.  This works very well and is extremely fast especially if the picture is small.
My problem begins when I want to apply a picture that is 1024x768 to the form.  Obviously there is no need to tile since the resolution I use is also 1024x768.  But it seems like it takes forever to load the control with the picture and equally as long to paint the form.  I have tried painting the form directly with the picture and not loading the Picture Box but while this takes half the time, it is still far to slow.  By slow I mean 1 second up to as much as 5 seconds.  The lag is barely noticeable on a P-II but since the typical platform this will be running on is a Pentium-200, I need to find a way to speed this up.
Any comments, questions or suggestions will greatly appreciated.
Avatar of Dr. Kamal Mehdi
Dr. Kamal Mehdi
Flag of Greece image

Since you have a fast CPU, make your pictures in .JPG format and load them. They will load and show very fast on the form. Load them directly on the form (not in the picture box).
Hope that helps.
Regards
Avatar of MalcolmPike
MalcolmPike

ASKER

I did forget to inform all that the pictures are all in JPG format.  While all pictures that are 1024x768 in size are painted directly on the form the smaller graphics need to be placed in the picture box so that I will know what the dimensions are and therfore tile the forms background.  
The problem is only with the large 1024x768 JPG pictures.  They are small in size, only 80k to 125k.  But even when I load them directly on the form it still takes anywhere from 2 to 5 seconds before the job is done.
Can you perhaps list the code that you used to do that.  Are you using the 'PaintPicture' method?
When I tile the form I use the 'PaintPicture' method. (This works great).
But when I try to paint the entire forms background with a JPG that is of the same size (1024x768), I simply use
"Me.Picture = LoadPicture(Pic)".
Where 'Pic' = the path and file name of the JPG.
Again I do not load the picture into a PictureBox first if it is 1024x768 so it seems that all the time is used doing the painting.
Is there a faster method?

Any help is greatly appreciated.

Malcolm,

This code disable/enable updating/redrawing of the form.  This could speed it up, maybe.

Declare the following API:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_SETREDRAW = &HB

Before you load the picture to the form, disable updating for the form:

   SendMessage Form1.hWnd, WM_SETREDRAW, False, 0 ' disable redraw

   ' load the picture here and apply to the form's picture property...

   SendMessage Form1.hWnd, WM_SETREDRAW, True, 0 ' enable redraw

It could be neccessary to do a Form1.Refresh afterwards, but first try without it.

Because this is API calls, be carefull with them, make sure you don't have any errors, or if you have, that you always turn on redrawing of the form (error handling).

Hope this can help.
I did everything as explained by wj7ster but unfortunately it did not increase the speed.  FYI: I did have to do the form refresh in order to see the background.
Is there an API that I can call to paint the background?  Or any method that is faster?
It must have to do with the physical size of the graphic because I can tile the background with a jpg that is for example one inch by one inch instantaneously.

Thanks in advance.

Is this for a splash, or somewhere in the middle of your app.?  If in the middle, try creating an instance of the form during the sub main (don't use the main form as a starting point for your app.)  Then you are using the 'splash' time to load the image and all you do is frmMyForm.show !

Jason
The painting of the single forms background can happen many times during the forms life.  
What is going on is this;
I am using one form to display many options or questions to the user.  Once the user has answered the question then the next question is shown.  If the new question has a background different than the previous question then I have to repaint the form.
Using two or more forms is not really an option for me unless I am absolutly forced to do so.

Thanks to Jason.


ASKER CERTIFIED SOLUTION
Avatar of kswinney
kswinney

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another thing to consider, MalcolmPike, is to load all the images necessary for your app at load time into StdPicture (Picture) objects... preferably an array of Picture objects.

Then, use the .Render method to paint them on the form as the user needs to see them.

This way, the load time is all on the front end.  The performance while using the app will be acceptable.

HTH.