How Bitmapiamge's Cache Works

HoWe Yu 41 Reputation points
2022-06-07T05:51:42.137+00:00

I need to add thousands of PNG icons (32x32) to the Canvas, the current method is to add thousands of DrawingVisuals to the Canvas, then iterate through all the DrawingVisuals and call DrawImage on it, each call will create a BitmapImage object.
Then when panning the Canvas with the middle mouse drag, I refresh the positions of all icons.
Originally, this operation was very smooth, until I added a line of code to load the same image resource in the construction method of Canvas, so it would be very stuck when dragging.
As far as I know, BitmapImage will be cached by default, but I don't know the specific caching mechanism. Is the cache checked every time a New BitmapImage is created? If so, how to explain the above stuck phenomenon?

This is normal
208848-01.gif
This is after adding that line of code
208934-02.gif

2048 icons on Canvas

Main code:
208868-maincode.png
Constructor:
208889-constructor.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.