RenderTargetBitmap does not respect ColumnDefinitions width

156 views Asked by At

I need to create snapshot of Grid with some hidden columns (by setting it's ColumnDefinition.Width = 0).

On screen it looks fine, but the created image has all columns visible (does not respect the ColumnDefinitions). I red somewhere that it is because the RenderTargetBitmap is looking at different layer where these changes aren't present (Visual layer vs. Layout layer). Is there any chance to get realistic snapshot of the grid with correct ColumnDefinitions? I cannot simply use Rectagnel.Fill = VisualBrush, because I need to store these images in cycle (every iteration = new image).

I tried ways like this snippet

2

There are 2 answers

0
karel On BEST ANSWER

It was needed to force UpdateLayout() before each snapshot. I changed the sizes in cycle and layout was updated too late.

0
Pascalsz On

Call this method before you create a snapshot of an UIElement:

public static UIElement GetMeasuredAndArrangedVisual(UIElement visual)
{
    visual.Measure(new Size
    {
        Height = double.PositiveInfinity,
        Width = double.PositiveInfinity
    });

    visual.Arrange(new Rect(0, 0, visual.DesiredSize.Width, visual.DesiredSize.Height));

    visual.UpdateLayout();

    return visual;
}