Hello All:
I have a user control with a grid. Some of the grid columns might be collapsed as needed.
I'm working on saving an image of the control with the VisualBrush. My code saves the display image fine if all the panels are visible, otherwise, the image is compressed like the code squeezes all the panels into the control panel size when some grids are collapsed. In this case, the collapsed part is shown as a white background.
Thank you in advance.
I found a forum where they discussed a similar problem but the solution blog is not live any longer. Here is the forum page: https://social.msdn.microsoft.com/Forums/vstudio/en-US/997fb616-4cd4-49f0-8b85-f05cde328104/visualbrush-of-an-expander-after-expandingcollapsing?forum=wpf
Here is my code.
public static BitmapSource DrawToBitmap(this FrameworkElement element)
{
double width = element.ActualWidth;
double height = element.ActualHeight;
int pixelWidth = (int)Math.Round(width);
int pixelHeight = (int)Math.Round(height);
var bmp = new RenderTargetBitmap(pixelWidth, pixelHeight, 96, 96, PixelFormats.Default);
var drawingVisual = new DrawingVisual(); using (DrawingContext dc = drawingVisual.RenderOpen())
{
var brush = new VisualBrush(element);
dc.DrawRectangle(brush, null, new Rect(0, 0, width, height));
}
bmp.Render(drawingVisual);
return bmp;
}