VisualBrush of an FrameworkElement after expanding/collapsing

UCC 96 Reputation points
2021-03-24T19:40:34.027+00:00

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;
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,205 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

Accepted answer
  1. UCC 96 Reputation points
    2021-03-25T15:41:46.837+00:00

    Kent Boogaart (https://kent-boogaart.com/) provided a workaround.

    Here is the final code with his input.
    Image of a FrameworkElement element can be saved by:

            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);
                brush.ViewboxUnits = BrushMappingMode.Absolute;
                brush.Viewbox = new Rect(element.TranslatePoint(new Point(0, 0), element), new Size(width, height));
                dc.DrawRectangle(brush, null, new Rect(0, 0, width, height));
            }
            bmp.Render(drawingVisual);
    

1 additional answer

Sort by: Most helpful
  1. UCC 96 Reputation points
    2021-03-25T15:53:45.757+00:00

    This code also works:

            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);
                brush.AlignmentX = AlignmentX.Left;
                brush.Stretch = Stretch.None;
                dc.DrawRectangle(brush, null, new Rect(0, 0, width, height));
            }
            bmp.Render(drawingVisual);
    
    0 comments No comments