简体   繁体   中英

RenderTargetBitmap does not respect ColumnDefinitions width

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM