WPF屏幕截图不包含标题栏

时间:2019-12-11 12:53:09

标签: wpf screenshot

我在对此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足以提供帮助。我还在此链接Generating a screenshot of a WPF window上尝试了该示例,但是得到了相同的结果:没有标题栏。

1 个答案:

答案 0 :(得分:1)

由于@Clemens发送给我的link,我得以使用该页面上的一些代码来提出这种工作方法。它比活动窗口多抓几个像素,但是对我有用!

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