WPF Screen Capture 不包含标题栏

WPF Screen Capture doesn't include title bar

我在这个 question 的第一个答案中实现了代码,它工作得很好,除了它不包含标题栏,这是我需要的。有人知道我做错了什么吗?

我更新了示例以显示所有其他已注释掉的失败尝试。 这是我的代码:

Window shellView = Application.Current.MainWindow;

Rect bounds = VisualTreeHelper.GetDescendantBounds(shellView);
//Rect bounds = new Rect(new Size(shellView.ActualWidth, shellView.ActualHeight));
//Rect bounds = shellView.RestoreBounds;
//Rect bounds = VisualTreeHelper.GetContentBounds(shellView);
//Rect bounds = new Rect(new Size(VisualTreeHelperEx.FindDescendantByType<Window>(shellView).ActualWidth,
//    VisualTreeHelperEx.FindDescendantByType<Window>(shellView).ActualHeight));

RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();

string fileName = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\Screen_Capture.png";
if (_dialogService.ShowSaveFileDialog(ref fileName, "PNG Files | *.png"))
{
    using (DrawingContext context = visual.RenderOpen())
    {
        VisualBrush visualBrush = new VisualBrush(shellView);
        context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
    }

    renderTarget.Render(visual);
    PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
    bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));

    using (Stream stm = File.Create(fileName))
    {
        bitmapEncoder.Save(stm);
    }
}

并且应 XAMIMAX 的要求,我可以分享的 XAML 名称已更改:

<Window x:Class="MyProject.Shell.Views.Shell.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ap="clr-namespace:MyProject.Common.Support.AttachedProperties;assembly=MyProject.Common.Support"
        xmlns:controls="clr-namespace:MyProject.Common.Support.Controls;assembly=MyProject.Common.Support"
        xmlns:converters="clr-namespace:MyProject.Common.Support.Converters;assembly=MyProject.Common.Support"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:dts="clr-namespace:MyProject.Common.Support.DataTemplateSelectors;assembly=MyProject.Common.Support"
        xmlns:enums="clr-namespace:MyProject.Shell.Enums"
        xmlns:enums1="clr-namespace:MyProject.Common.Support.Enums;assembly=MyProject.Common.Support"
        xmlns:ikriv="clr-namespace:MyProject.Common.Support.AttachedProperties;assembly=MyProject.Common.Support"
        xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:m="clr-namespace:MyProject.Common.Support.MarkupExtensions;assembly=MyProject.Common.Support"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:resources="clr-namespace:MyProject.Shell.Views.Shell.Resources"
        xmlns:ss="clr-namespace:MyProject.Common.Support.StyleSelectors;assembly=MyProject.Common.Support"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:views="clr-namespace:MyProject.NotificationModule.Client.Views;assembly=MyProject.NotificationModule.Client"
        xmlns:constants="clr-namespace:MyProject.Shell"
        x:Name="shell"
        Title="{Binding MyProjectApplicationTitle}"
        Width="1024"
        Height="768"
        MinWidth="800"
        MinHeight="600"
        Background="{DynamicResource PrimarySolidColorBrush}"
        Icon="/Resources/Embedded/MyProject.ico"
        mc:Ignorable="d">

        <controls:LocalizationScope.Content>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Grid.Children>

                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="{Binding NavigationBarWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                              MinWidth="38"
                                              MaxWidth="400" />
                    </Grid>

                </Grid.Children>
            </Grid>
        </controls:LocalizationScope.Content>
</Window>

我希望 XAML 对您有所帮助。我也尝试了这个 link Generating a screenshot of a WPF window 的示例,但得到了相同的结果:没有标题栏。

感谢@Clemens 送我的 link,我能够使用该页面上的一些代码来提出这种工作方法。它比活动 window 多抓取几个像素,但这对我有用!

    private void TakeScreenshot()
    {
        var rect = new Rect();
        GetWindowRect(GetForegroundWindow(), ref rect);

        var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
        var result = new Bitmap(bounds.Width, bounds.Height);
        using (var graphics = Graphics.FromImage(result))
        {
            graphics.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), System.Drawing.Point.Empty, bounds.Size);
        }

        string fileName = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\Screen_Capture.png";
        if (_dialogService.ShowSaveFileDialog(ref fileName, "PNG Files | *.png"))
        {

            result.Save(fileName, ImageFormat.Png);

            _dialogService.ShowInfoDialog(_localizationService["Shell.Views.Shell.HelpMenu.ScreenshotSaved"] + $"{fileName}");
        }
    }