WPF 屏幕截图不包括标题栏

标签 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 ,但得到了相同的结果:没有标题栏。

最佳答案

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

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

关于WPF 屏幕截图不包括标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59286289/

相关文章:

c# - 如何在 Xaml 中使用 SortDescriptions 对 TreeView 项目进行排序?

C# WPF - 拖动图像

javascript - 带有外部脚本的 PhantomJS Node

c# - 通用Windows库(UWP)截屏方式?

Python截取屏幕截图并将其保存到缓冲区

wpf - 是什么使WPF应用程序启动缓慢?

C# WPF DataGrid 删除每个选定的项目

c# - 将数据表中选定的列绑定(bind)到数据网格中

c++ - 在 WIndows 8.1 中捕获 PC 的整个屏幕截图

java - 用java截屏