I have a complex user control that displays an image and overlays certain tags with shadows, etc. I want to display all these things in memory, then make an image of this card, and then use this image in a real user interface. This is the thing at the end.

I do this because the interface has started to move slowly with all of these elements, and I'm trying to simplify it. Am I going the right way?
The problem here is that I create a brainmap, feed it with data, and then try to create imagen and BAM! this cannot be done, because the entire component is not displayed, ActualWith is zero.
This is how I extract the image from the control (the method works fine when the control is displayed on the screen)
/// <summary> /// The controls need actual size, If they are not render an "UpdateLayout()" might be needed. /// </summary> /// <param name="control"></param> /// <param name="Container"></param> public static System.Windows.Controls.Image fromControlToImage(System.Windows.FrameworkElement control) { if (control.ActualWidth == 0) throw new Exception("The control has no size, UpdateLayout is needed"); // Here is where I get fired if the control was not actually rendered in the screen RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32); rtb.Render(control); var bitmapImage = new BitmapImage(); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(rtb)); using (var stream = new System.IO.MemoryStream()) { bitmapEncoder.Save(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } System.Windows.Controls.Image testImage = new System.Windows.Controls.Image(); testImage.Source = bitmapImage; return testImage; }
If the control was part of the layout at the beginning, adding UpdateLayout really solves the problem (why I added an exception for myself there), but when the control is created from the code and never reached the UpdateLayout page there will be no help at all.
What can I do to ensure that all the elements in memory are rendered and then the image is rendered without page input? (fixed size perceived)
source share