Magdalena Lauberger
Magdalena Lauberger

Reputation: 11

How to show an array of images, without freezing the whole programm for seconds

I have the following problem:

I want to make a little 2d-game in c#. To display a background that consists of several images. To make animated gifs possible, i used MediaElement objects. I added these MediaElements to my Canvas. But when i run my programm, the performance is a disaster. It takes almost half a minute to display the images.

I obviously need a better idea to display an array of images, without slowing down everything.

This is my Starting-Point:

class BackgroundMediaElement : MediaElement
{

    public BackgroundMediaElement(string imageSourcePath, int rowInCanvas, int columnInCanvas, int lengthOfSquare)
    {
        this.UnloadedBehavior = MediaState.Manual;
        this.Source = new Uri(imageSourcePath, UriKind.Relative);

        Canvas.SetTop(this, rowInCanvas*lengthOfSquare);
        Canvas.SetLeft(this, columnInCanvas*lengthOfSquare);

        this.Play();

        this.MediaEnded += method_MediaEnded;
    }

    private void method_MediaEnded(object sender, RoutedEventArgs e)
    {
        //MediaElement m = (MediaElement)sender;
        this.Position = TimeSpan.FromMilliseconds(1);
    }
}

I created more than 200 elements and added them to my Canvas. But it even slows down with 40 elements. One image does not even have one kByte.

Upvotes: 0

Views: 91

Answers (0)

Related Questions