How to create a WPF UserControl image at runtime

I created a WPF application that has a Canvas on which I place UserControls that can be moved and resized by the user (just like Windows-Window). Now I have found that this can be very slow on older PCs, which is a problem.

As a solution, I thought about creating a graphic file that displays UserControl, and will show it when changing / dragging a control so that WPF does not recalculate all the elements on an ongoing basis. The only problem is that I have no idea how to generate this image.

Is there something like a function that does this in .Net? Or how can I do it on my own?

+4
source share
2 answers

You can convert a WPF control to a bitmap using RenderTargetBitmap , then this image can be copied to the clipboard saved to a file or used as part of your GUI

Check out Get a bitmap from the management view.

Beware that you may run into problems when parts of the control you are trying to display are not visible (possibly in the scroll viewer)

+5
source

WPF applications do require some pretty serious grunts; especially in the graphics department, and greatly benefit from the presence of a decent video card present in the system. Even then, WPF application performance (if not carefully designed) can leave much to be desired ...

However, you can use the FixedDocument to rasterize the UserControl and then convert it to GIF / JPG / PNG and place it instead of resizing the control ... however, I would expect the process itself to be the same slower or slower than yours current performance issues.

0
source

Source: https://habr.com/ru/post/1344908/


All Articles