Benny
Benny

Reputation: 35

Create snapshot of a non-shown UserControl

I want to take a snapshot of my UserControl, which has not been shown yet. That's my code:

    public Screenshot(MyViewModel viewModel)
    {
        if (viewModel == null)
            return;

        // Create a TabControl, where View is hosted in
        var visualTab = new TabControl();
        visualTab.Width = FrameworkAdjustments.VisualSize.Width;
        visualTab.Height = FrameworkAdjustments.VisualSize.Height;
        visualTab.TabStripPlacement = Dock.Left;

        // Tabs should only be shown, if there are more than one 'SubView'
        Visibility tabVisibility = Visibility.Collapsed;
        if (viewModel.SubViews.Count > 1)
            tabVisibility = Visibility.Visible;
        foreach (var subView in viewModel.SubViews)
        {
            var tab = new TabItem();
            tab.Header = subView.TranslatedId;    // multilanguage header
            tab.Visibility = tabVisibility;
            if (subView.Id == viewModel.ActiveSubView.Id)
            {
                tab.IsSelected = true;
                // Without the following line my UI works, but my TabControl is empty.
                tab.Content = ViewManager.GetViewById(subView.Id);
                // ViewManager.GetViewById(subView.Id); returns a UserControl
            }

            tab.Measure(FrameworkAdjustments.VisualSize);
            tab.Arrange(new Rect(FrameworkAdjustments.VisualSize));
            visualTab.Items.Add(tab);
        }

        _ContentCtrl = new ContentControl() { Width = FrameworkAdjustments.VisualSize.Width, Height = FrameworkAdjustments.VisualSize.Height };
        _ContentCtrl.Content = visualTab;
        _ContentCtrl.Measure(FrameworkAdjustments.VisualSize);
        _ContentCtrl.Arrange(new Rect(FrameworkAdjustments.VisualSize));

        RenderTargetBitmap bmp = new RenderTargetBitmap((int)FrameworkAdjustments.VisualSize.Width, (int)FrameworkAdjustments.VisualSize.Height, 96, 96, PixelFormats.Pbgra32);
        bmp.Render(_ContentCtrl);

        this.ItemBrush = new ImageBrush(bmp);
    }

This code runs for each 'MyViewModel' when I start my App. 'MyViewModel' contains a List of 'SubViews' which are the content of the Tabs and they contain a 'FunctionKeyBar' which's buttons can be activated by using 'F1' to 'F12'. But after creating my screenshot I can't use the F1 to F12 anymore. Also there are other problems, like switch language. Is there an other way to create a snapshot of a control which has not came into view?

Thanks for all replys.

Greetings Benny

Upvotes: 0

Views: 610

Answers (2)

Benny
Benny

Reputation: 35

Now I found a solution, thanks to AnjumSKan. I took his code and changed it a little bit. As I said, I need to create the snapshot on application startup or culture changed and I have more than one view. in my Function AddContentControl, I add the Content to my TabControl which has negative Margin. Add the end I call HiddenTab.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new Action(CreateSnapshotOnRender));

('HiddenTab' is my TabControl which is not shown to user). This calls a function called CreateSnapshotOnRender, where I call AnjumSKhan's CaptureScreen-method from. After that I call again the function AddContentControl with my next content. This looks as following:

private void CreateScreenshotOnRender()
    {
        HiddenTab.Measure(ViewSize);
        HiddenTab.Arrange(new Rect(ViewSize));
        var snapshot = CaptureScreen(HiddenTab, dpiX, dpiY); 
/* Do anything with snapshot */
        _Index++;      // counter to know thich view is next
        CreateAllScreenshots();
    }

Thanks again to AnjumSKan, because you lead me to this. Thats why I marked your Answer as the correct one.

Upvotes: 1

AnjumSKhan
AnjumSKhan

Reputation: 9827

Approach : Set Margin to negative to keep it hidden, Add the control to the Grid / any other container.

enter image description here

Follow these steps :

1) Create a Task to create and add your ContentControl to the Grid.

2) Call user-define CaptureScreen () function.

3) Visibility must not be Hidden/Collapsed. Margin can be negative to hide the control.

In this example, I have done this in a Button.Click.

async private void Button_Click(object sender, RoutedEventArgs e)
{
    Task<ContentControl> t = AddContentControl();
    ContentControl ctrl = await t;

    RenderTargetBitmap bmp = CaptureScreen(ctrl, 5000, 5000);
    Img.Source = bmp;
}
/* Add the ContentControl to the Grid, and keep it hidden using neg. Margin */
private Task<ContentControl> AddContentControl()
{
    Task<ContentControl> task = Task.Factory.StartNew(() =>
    {
        ContentControl ctrl = null;
        Dispatcher.Invoke(() =>
        {

            ctrl = new ContentControl() { Content = "Not shown", Width = 100, Height = 25, Margin = new Thickness(-8888, 53, 0, 0) };
            Grd.Children.Add(ctrl);
        });

        return ctrl;
    });

    return task;
}
/* Important , wont work with Visibility.Collapse or Hidden */
private static RenderTargetBitmap CaptureScreen(Visual target, double dpiX, double dpiY)
{
    if (target == null)
    {
        return null;
    }
    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                    (int)(bounds.Height * dpiY / 96.0),
                                                    dpiX,
                                                    dpiY,
                                                    PixelFormats.Pbgra32);
    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(target);
        ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
    }
    rtb.Render(dv);
    return rtb;
}

Upvotes: 2

Related Questions