简体   繁体   中英

How to remove image.Source from memory

I have a problem when I add Images in a wrapPanel, it refuses to load if there are too many images in memory.

In fact, when I create an Image control and its source comes from a URL (from which it has to download the image), delete the Image control and recreate one with the same source, the image appears directly but there is no time to download the image like the first creation of the control.

How can I remove the image from memory?

Here is my code:

Image image = new Image();
image.Width = 60;
image.Height = 60;

image.ToolTip = StringExtension.GetTextBetweenTwoWord(Utilities.resource.EmojiesLink[i], "/72/apple/271/", "_").Replace('-', ' '); // name of mood

image.Stretch = Stretch.Fill;

image.Source = new BitmapImage(new Uri(Utilities.resource.EmojiesLink[i]));

image.MouseDown += new MouseButtonEventHandler(ExtraMoodFromWebsiteMouseDown);

wrapPanel_moodFromInternet.Children.Add(image);

PS: it's in WPF

You can release an object from memory by calling the dispose method. https://docs.microsoft.com/en-us/dotnet/api/system.drawing.image.dispose?view=dotnet-plat-ext-5.0

            Image image = new Image();
            image.Width = 60;
            image.Height = 60;

            image.ToolTip = StringExtension.GetTextBetweenTwoWord(Utilities.resource.EmojiesLink[i], "/72/apple/271/", "_").Replace('-', ' '); // name of mood

            image.Stretch = Stretch.Fill;

            image.Source = new BitmapImage(new Uri(Utilities.resource.EmojiesLink[i]));

            image.MouseDown += new MouseButtonEventHandler(ExtraMoodFromWebsiteMouseDown);

            wrapPanel_moodFromInternet.Children.Add(image);

            image.Dispose(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM