Mandarina Portokalli
Mandarina Portokalli

Reputation: 111

Why does setting a BackgroundImage on my form increase load time?

When I set an image as the BackgroundImage on my form, why does the form take a long time to load and why does my application slow down in general? Is there any way I can combat this?

Upvotes: 0

Views: 3233

Answers (3)

Geetika Kapoor
Geetika Kapoor

Reputation: 21

Make sure that the Background Image is set in the option 'Stretch' rather than 'Tile.' In my experience, the 'Tile' layout takes extra time to load.

Upvotes: 0

Clint
Clint

Reputation: 6220

Unfortunately, if you want the form to always have the background image (especially when it's first shown) then if the image is large there's nothing you can do to counteract the load time of the form.

If you're happy with the form being shown and then the image being applied when it's loaded, you could open the form and then load the image in a background thread and apply it when you've got it:

Task<Image>.Factory.StartNew(() =>
            {
                return Bitmap.FromFile("image path here");
            }).ContinueWith(t =>
                {
                    this.BackgroundImage = t.Result;
                }, TaskScheduler.FromCurrentSynchronizationContext());

Let's not also forget the obvious solution of resizing the image in question so that it doesn't take so long to load, or just taking the corner piece and putting it into a picture box without a border.

Upvotes: 1

rqmok
rqmok

Reputation: 864

You are using Visual Studio on Windows.

There is a common problem with normal windows forms in Visual Studio. When a high definition image is being rendered (doesn't have to be too much of a high definition image, or it can be medium too), your application will start to lag.

I also experienced this problem with my applications.

If any controls on your application have the DoubleBuffered property, then you need to set it to true.

Unfortunately, this solution will not completely get rid of your problems.

If you are making an application with a lot of images involved, I recommend you program your application in WPF. Luckily, WPF forms already have this issue solved, so you wouldn't have to do any thing else to tweak the performance of your images in your application.

Good Luck!

Upvotes: 2

Related Questions