WPF 截取包含隐藏内容的应用程序截图 [英] WPF take screenshot of application including hidden content

查看:55
本文介绍了WPF 截取包含隐藏内容的应用程序截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在技术限制"类别下:

我收到了在我的应用程序中有一个屏幕截图按钮的要求,该按钮将截取屏幕截图并启动一个打印机对话框.很公平.我的代码实现了这一点.我只是拿我的窗口,并使用 RenderTargetBitmap 来渲染窗口.

I have received the requirement to have a screenshot button in my application that will take a screenshot and launch a printer dialog. Fair enough. My code achieves that. I simply take my window, and use a RenderTargetBitmap to render the window.

但是,要求现在规定它应该包括隐藏在滚动条后面的所有内容.这意味着,在屏幕截图中,应用程序应该看起来拉伸"以消除滚动条并显示所有数据.例如,如果有一个很大的列表或数据网格,所有数据都应该是可见的.

However, the requirement now states that it should include all content that is hidden behind scrollbars. Meaning, that in the screenshot the application should look "stretched" in order to eliminate scrollbars, and show all data. For instance in case there is a large list or datagrid, all the data should be visible.

请记住,WPF 可能正在虚拟化而不是渲染不在视图中的东西,有什么方法可以实现此要求?是否有可能将可视化树渲染到一个单独的无限空间并在那里截取屏幕截图?还有什么?

Keeping in mind that WPF might be virtualizing and not rendering things that are not in view, is there any way I can achieve this requirement? Is there a possibility of rendering the visual tree to a seperate infinite space and taking a screenshot there? Something else?

回复评论:

屏幕截图按钮位于仅包含菜单的外壳上.在这个 shell 中,可以托管 800 多个视图中的任何一个.这些视图可以包含数据网格、列表、由文本框组成的大型表单......任何东西.如果不走视觉树,就无法判断内部"是什么.

The screenshot button is on an outer shell that only holds the menu. Inside this shell any of 800+ views can be hosted. These views could contain datagrids, lists, large forms consisting of textboxes... anything. There is no way to tell what is 'inside' without walking the visual tree.

请求的功能类似于将浏览器中的网页打印为 PDF.它还会为您提供整个 DOM,而不仅仅是您在浏览器的有限视图中看到的内容.

The functionality requested is similar to printing a webpage in your browser to PDF. It will also give you the entire DOM instead of just what you see in the limited view of the browser.

推荐答案

XAML:

<Grid>
    <Button
        x:Name="btnPrint"
        Width="50"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Click="BtnPrint_Click"
        Content="Print" />
    <ScrollViewer Height="500" HorizontalAlignment="Center">
        <Grid x:Name="toPrint">
           <!--your code goes here-->
        </Grid>
    </ScrollViewer>
</Grid>

C#:

private void BtnPrint_Click(object sender, RoutedEventArgs e)
    {
        var pdialog = new PrintDialog();
        if (pdialog.ShowDialog() == true)
        {

            System.Windows.Size pageSize = new System.Windows.Size { Height = pdialog.PrintableAreaHeight, Width = pdialog.PrintableAreaWidth };
            toPrint.Measure(pageSize);
            toPrint.UpdateLayout();
            pdialog.PrintVisual(toPrint, "Print");

        }
    }

这篇关于WPF 截取包含隐藏内容的应用程序截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆